Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Refreshing a DataSet
Message
General information
Forum:
ASP.NET
Category:
ADO.NET
Miscellaneous
Thread ID:
00702106
Message ID:
00702335
Views:
19
Hi Eric and Bonnie,

You can update/add/delete DataRows using two approaches, either by writing your own statements or using stored procedures. One misconception in ADO.NET is that updates are batch processed. In actuality, each row is being sent separately to the backend when you call the DataAdapter's Update method.

If your backend database supports batch queries (like SQL Server), then you can return from either a command statement or from a stored procedure, your updated row information. For a command statement, just append a SELECT statement to the end of your command statement, separating the two statements with a ";". Here is an example of an INSERT statement:
MySqlInsertCommand.CommandText = "INSERT INTO Employees(LastName, FirstName) VALUES (@LastName, @FirstName); SELECT EmployeeID, LastName, FirstName FROM Employees WHERE (EmployeeID = @@IDENTITY)"
For a Stored Procedure, you can use the following comparable code, still adding a Select statement:
ALTER PROCEDURE dbo.SPInsertCommand
(
	@LastName nvarchar(20),
	@FirstName nvarchar(10)
)
AS
	SET NOCOUNT OFF;
INSERT INTO Employees(LastName, FirstName) VALUES (@LastName, @FirstName);
	SELECT EmployeeID, LastName, FirstName FROM Employees WHERE (EmployeeID = @@IDENTITY)
When you run this, the DataAdapter knows to look for the results from the InsertCommand and the UpdateCommand because of its UpdatedRowSource property. This property accepts a value from the UpdateRowSource enumeration. By default, the Command will fetch new data from the modified row by checking for output parameters and the first row returned by the query.

The DataSet will contain the updated information. Hope this helps.

>Eric,
>
>Someone else may have to chime in here and help. We haven't been using the DataAdapter.Update() methods. We do all our data access through SPs so we have to do things a little differently. Working through SPs is easy enough, but from what I understand of the DataAdapter.Update(), it does a lot of things for you automatically (like determining which rows have changes) which we have to do manually in order to use a SP. So, in your case, maybe that's not the way to go. Unfortunately, I can't think of an easier way to get your identity column back after you update your row, so unless anyone else has any other suggestions, maybe you'll have to think about doing it my way.
>
>Lemme know if you need more help on using the SPs, if you decide to go that route.
>
>~~Bonnie
>
>
>>The data adapter update is a kind of batch update. I could give a stored proc to my UpdateCommand but will it update the dataset and refresh the grid?
>>
>>
>>>Eric,
>>>
>>>If you update your SQL table with a SP, you can have the SP return the identity field for you as an output parameter.
>>>
>>>HTH,
>>>~~Bonnie
>>>
>>>
>>>>I have a small table in SQL Server which has an Identity field.
>>>>
>>>>I retreive this table into a data adapter and fill a dataset from it. This data set is then bound to a data grid. Up to this point, everything is OK.
>>>>
>>>>I have then added a Save button in which I update the data adapter (dataadapter.update(dataset, ...)).
>>>>
>>>>My question is the following:
>>>>If I add new rows to my datagrid, what is the best method to update the grid to have it show the value of the identity field after I have clicked the save button?
-----------------------------------------

Cathi Gero, CPA
Prenia Software & Consulting Services
Microsoft C# / .NET MVP
Mere Mortals for .NET MVP
cgero@prenia.com
www.prenia.com
Weblog: blogs.prenia.com/cathi
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform