Solved

Saving Dataset changes back to database using vb.net

Posted on 2014-01-28
5
1,471 Views
Last Modified: 2014-01-28
I am trying to save changes that I have made to a dataset/data table back to the database.  I am populating the data table as follows:

  Dim cn As New SqlConnection(Global_Values.sConn)
  Dim da As New SqlDataAdapter(sSQL, cn)
  Dim ds As New DataSet

  If cn.State = System.Data.ConnectionState.Closed Then
           cn.Open()
  End If

  Dim x_Age As String

  da.Fill(ds, "search_result")

  For Each row As DataRow In ds.Tables("search_result").Rows
          row("Age") = Calc_Age(row("Date_Of_Birth")) <----- calls function to calc age
           row.AcceptChanges()
  Next row

 now how do I update the records in the database with the changes I just made?  Each row has a unique column named Rec_ID.  The column in the database that I want to update for each record is Age.

Ant help would be appreciated as I am new to this.
0
Comment
Question by:Mike_Stevens
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 39814884
Here is one method: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommandbuilder(v=vs.110).aspx

It uses a SqlCommandBuilder object that builds the insert, update and delete SQL statements based on the query used to build the select statement.
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 39814892
Another option is to create a SQLCommand object for your update statement and attach it to the UpdateCommand property of your SQLDataAdapter. Example here: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.updatecommand(v=vs.110).aspx
0
 

Author Comment

by:Mike_Stevens
ID: 39814920
Where do I call da.UpdateCommand?  From within the loop or after the loop when all of the records have been updated?
0
 
LVL 26

Accepted Solution

by:
Shaun Kline earned 500 total points
ID: 39815010
You should do it after the loop. However, do not use AcceptChanges, as it sets the DataRowState flag for each record as unchanged. This flag is what is used by both methods mentioned above to determine which rows should be updated in the underlying datasource.
0
 

Author Comment

by:Mike_Stevens
ID: 39815111
got it!    Thanks for the help
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question