Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Updating Dataset

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
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 summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Integration Management Part 2

877 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