Solved

Updating Dataset

Posted on 2002-05-16
5
395 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
  • 2
5 Comments
 
LVL 23

Expert Comment

by:naveenkohli
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now