?
Solved

Updating Dataset

Posted on 2002-05-16
5
Medium Priority
?
406 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 200 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

719 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