Solved

Updating Dataset

Posted on 2002-05-16
5
399 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
Writing lines of code from a form output in script within a results page 2 46
innerHTML 7 46
VBScript on Html 15 65
Prevent certain words from being typed in a form 6 55
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…
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

738 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