Solved

Updating Dataset

Posted on 2002-05-16
5
401 Views
Last Modified: 2012-06-27
I would like to update the dataset row. I have 5 rows in my customer table and want to update a particular row for which I will be sending the customerId(primary key)
I have
myDataSet.Tables("Customer").Rows(RowId)("ContactName")="Haritha"
Now how shd I get my RowId.
Or Is there any other method to update the dataset.
And also can I update the database imm after the above line using
myDataAdapter.Update(myDataSet, "Customer")
Thanks
0
Comment
Question by:haritha30
[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
  • 2
5 Comments
 
LVL 23

Expert Comment

by:naveenkohli
ID: 7013991
Haritha,
What do you mean by "getting RowID"? Are you talking about getting the autoincremented identity column value that is created after the record is added.

To update record, you can make changes to the record in DataSet/DataTable and use Update method to make the changes. DataColumns keep track of the values that were changed and based on that build the UPDATE query when Update command is used.

Naveen
0
 

Author Comment

by:haritha30
ID: 7015528
Yes RowId, is what you meant, autoincremented identity coloumn. But how can I trace this value for a particular customerid

Abt the update method, can u give me a sample code.
I had been using the Command object,with ExecuteNonQuery() method. This will execute the database directly. I need a code to update DataSet.
Thanks.
0
 
LVL 23

Accepted Solution

by:
naveenkohli earned 50 total points
ID: 7015559
To get value of autogenerated column value, take a look at the folowing link

http://www.pardesiservices.com/Softomatix/ADONetAuto.asp

--------------
Following is an example that I wrote for some project. It should give you idea on how to use Update command to update data in table. Ignore the statements that don't make sense.



public bool UpdateAuthorLastName (string strID, string strLastName)
{
     bool bRetVal = true;
     string strConn = "Provider = SQLOLEDB.1; Data Source = SULTAN\\ADONET;" +
     " uid=nkk; pwd=nkk; Initial Catalog = pubs";
     string strSelectCmd = "SELECT au_id, au_lname FROM AUTHORS WHERE au_id='" +
     strID + "'";
     string updateCmd = "UPDATE AUTHORS SET au_lname = '" +  strLastName +  "' WHERE au_id = '" +  strID + "'";
try
{
// Create the connection.
OleDbConnection oledbConn = new OleDbConnection(strConn);
// Create the DataAdapter call Fill method to get the record.
OleDbDataAdapter dtAdapter = new OleDbDataAdapter (strSelectCmd, oledbConn);
dtAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
dtAdapter.RowUpdated += new OleDbRowUpdatedEventHandler(OnRowUpdatedEvtHndlr);
dtAdapter.RowUpdating += new OleDbRowUpdatingEventHandler(OnRowUpdatingEvtHndlr);
DataSet dtSet = new DataSet("Authors DataSet");
dtAdapter.Fill (dtSet, "Authors");

// Update the record in the table.
DataRow authorRec = dtSet.Tables["Authors"].Rows[0];
authorRec["au_lname"] = strLastName;

// Get the original and current value for the field.
string originalValue = (string)authorRec["au_lname", DataRowVersion.Original];
string currentValue = (string)authorRec["au_lname", DataRowVersion.Current];
bool bValueChanged = (string.Compare (originalValue, currentValue) != 0);

// Attach the DataAdapter to CommandBuilder for automatic
// generation of UpdateCommand.
//OleDbCommandBuilder cmdBldr = new OleDbCommandBuilder (dtAdapter);
dtAdapter.UpdateCommand = new OleDbCommand (updateCmd, oledbConn);

// Commit the changes made to the datasource.
int iRecsUpdated = dtAdapter.Update (dtSet, "Authors");
Trace.WriteLine ("Number of records updated = " + iRecsUpdated);

}
catch (OleDbException ex)
{
bRetVal = false;
Trace.WriteLine ("OLEDBEXCEPTION: " + ex.Message);
}
catch (Exception ex)
{
bRetVal = false;
Trace.WriteLine ("EXCEPTION: " + ex.Message);
}
return bRetVal;
}
0
 
LVL 15

Expert Comment

by:gladxml
ID: 8803595
No comment has been added lately and it seems that this question have been abandoned. So it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question or invite a Moderator to close this question if there's no reply from you after seven days.

In the absence of responses, I will recommend the following:

To accept the comment and points awarded to naveenkohli

** PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER **


Just trying to help for the cleanup...
gladxml
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

615 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