B
Barley T.
I have an SQL table with the primary key set as autoincrement.
My goal is to insert a new row using a dataset and then retrieve the
primary key of the row just created.
My code works some of the time, but the primary key I get back from the
dataset is not always in sync with the SQL server's primary key.
I have read in a number of posts that u can set the DataAdapter wizard
in VisualStudio.net to refresh the dataset upon each update. However,
I'm not nusing VS.net. -or- They say u can call SELECT @@IDENTITY
yourself. Can anyone post the raw code necessary to do this?
(I have included the post from which I read this below the code.)
Big thanks in advance to anyone who shares a solution.
Here is the code:
queryString = "SELECT * FROM MSG_Messages WHERE FromSUID = " &
intSUID
sqlCommand = New SqlCommand(queryString, sqlConnection)
dataAdapter = New SqlDataAdapter(sqlCommand)
' set data adapter parameters
dataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
Dim dsMessages As DataSet = New DataSet
dataAdapter.Fill(dsMessages, "MSG_Messages")
Dim tblMessages As DataTable
tblMessages = dsMessages.Tables("MSG_Messages")
intNumRows = tblMessages.Rows.Count
Dim drMessage As DataRow
drMessage = tblMessages.NewRow()
' Add an entrys to Messages table
drMessage("FromSUID") = intSUID
drMessage("CreateDate") = DateTime.Today
drMessage("Subject") = TextBoxSubject.Text
drMessage("Body") = TextAreaBodyCreate.Value
tblMessages.Rows.Add(drMessage)
' Send changes to sql server
Dim objCommandBuilder As New SqlCommandBuilder(dataAdapter)
dataAdapter.Update(dsMessages, "MSG_Messages")
strId = drMessage("MessageId")
-------------------------------------------------------------------------
Here is the old post:
From: David Shannon ([email protected])
Subject: Re: SqlDataAdaper.Update...
View: Complete Thread (3 articles)
Original Format
Newsgroups: microsoft.public.dotnet.framework.adonet
Date: 2002-05-14 10:23:27 PST
If you are using SQLServer, there is an option on the DataAdapter Wizard
(Advanced Options) to update or refresh the dataset row from the data
adapter after each insert or update operation. When you select that
option, the wizard creates a second SQL statement that is batched with
the insert or update statement. That second SQL statement updates your
dataset row with the fresh values from the database, including the new
autoincrement value.
If you are using MS Access -- which doesn't seem to take batched SQL
statements -- you have to call SELECT @@IDENTITY yourself to get the
most recent autoincrement value issued by the database and then you have
to apply it to your dataset row in code.
--------------------------------------
David Taylor Shannon
Tucson, Arizona
[email protected]
My goal is to insert a new row using a dataset and then retrieve the
primary key of the row just created.
My code works some of the time, but the primary key I get back from the
dataset is not always in sync with the SQL server's primary key.
I have read in a number of posts that u can set the DataAdapter wizard
in VisualStudio.net to refresh the dataset upon each update. However,
I'm not nusing VS.net. -or- They say u can call SELECT @@IDENTITY
yourself. Can anyone post the raw code necessary to do this?
(I have included the post from which I read this below the code.)
Big thanks in advance to anyone who shares a solution.
Here is the code:
queryString = "SELECT * FROM MSG_Messages WHERE FromSUID = " &
intSUID
sqlCommand = New SqlCommand(queryString, sqlConnection)
dataAdapter = New SqlDataAdapter(sqlCommand)
' set data adapter parameters
dataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
Dim dsMessages As DataSet = New DataSet
dataAdapter.Fill(dsMessages, "MSG_Messages")
Dim tblMessages As DataTable
tblMessages = dsMessages.Tables("MSG_Messages")
intNumRows = tblMessages.Rows.Count
Dim drMessage As DataRow
drMessage = tblMessages.NewRow()
' Add an entrys to Messages table
drMessage("FromSUID") = intSUID
drMessage("CreateDate") = DateTime.Today
drMessage("Subject") = TextBoxSubject.Text
drMessage("Body") = TextAreaBodyCreate.Value
tblMessages.Rows.Add(drMessage)
' Send changes to sql server
Dim objCommandBuilder As New SqlCommandBuilder(dataAdapter)
dataAdapter.Update(dsMessages, "MSG_Messages")
strId = drMessage("MessageId")
-------------------------------------------------------------------------
Here is the old post:
From: David Shannon ([email protected])
Subject: Re: SqlDataAdaper.Update...
View: Complete Thread (3 articles)
Original Format
Newsgroups: microsoft.public.dotnet.framework.adonet
Date: 2002-05-14 10:23:27 PST
If you are using SQLServer, there is an option on the DataAdapter Wizard
(Advanced Options) to update or refresh the dataset row from the data
adapter after each insert or update operation. When you select that
option, the wizard creates a second SQL statement that is batched with
the insert or update statement. That second SQL statement updates your
dataset row with the fresh values from the database, including the new
autoincrement value.
If you are using MS Access -- which doesn't seem to take batched SQL
statements -- you have to call SELECT @@IDENTITY yourself to get the
most recent autoincrement value issued by the database and then you have
to apply it to your dataset row in code.
--------------------------------------
David Taylor Shannon
Tucson, Arizona
[email protected]
Mauro said:Hi, got a problem with the 'Update' method, here is my situation:
ASP.NET Application
DB table Struct:
- ID: bigint, pk, AutoIncrement
- field2: char(50)
instanced:
- SqlDataAdapter;
- CommandBuilder;
- Typed DataSet (based on the DB table);
- DataView(DataSet.Tables["..."])
After filling the DataSet with the DataAdapter I instanced a new
DataRowView, correctly filled the filed2 and then successfully add the
new row to the DataView.
After calling the Update method data are correctly written to the DB.
Problems occurs when I read the AutoIncrement ID because the value of
the filed now differs from the one in the DB. I realized that there is
non sync between AutoIncrement indexes in the DB and in the DataSet,
so:
1 - Create a new row in the DataSet the ID is 1
2 - Update the DB, both ID are 1
1 --> 2 more times....
- Manually delete the last 2 row2 from the DB
- restart the application
- refill the dataset
- NOW
New row ID from the dataset will be (automatically ) the
last
row ID + 1
- BUT
When you Update the DB throught the DataAdapter the ID
will
be... not the first available but the first available
considering the ID of deleted rows too (that is the correct behavior
of the DataBase Server).
- AND
the DataSet is not correctly synchronized. he only way I
found,
at the moment, is to refill the DataSet with an obvious lost of
performance.
The other problem is that if I create a relation in the DataSet with
another table this could lead to constrain problems
Any idea..
I remain at your disposal for any further information,
Thanks in advance.