473,320 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

retrieve ID of new row using dataset

jay
I am using the dataset object to add a row to a sql server database in vb.net
code, as follows:

dim drow as DataRow
dim cmdBld as new SqlCommandBuilder(mySqlDataAdapter)
ds.tables(0).NewRow()
drow("field1") = data for field 1
and so forth
ds.tables(0).rows.add(drow)
cmdBld = New SqlCommandBuilder(mySqlDataAdapter)
mySqlDataAdapter.Update(ds)

It works great, but how do I retrieve the autoincrement ID field of the row
just added???

Please help!
Nov 19 '05 #1
4 5054
Jay,

How did you create your sql command? is it a stored proc.?

When you call mySqlDataAdapter.Update it should write all the changed /
added reocrds in the data set to the database and update the dataset with the
identities. So all you need to do the get the id from the dataset row. i.e.
drow("id") if "id" is the name of the identity column.

if this doesn't work let me know.

"jay" wrote:
I am using the dataset object to add a row to a sql server database in vb.net
code, as follows:

dim drow as DataRow
dim cmdBld as new SqlCommandBuilder(mySqlDataAdapter)
ds.tables(0).NewRow()
drow("field1") = data for field 1
and so forth
ds.tables(0).rows.add(drow)
cmdBld = New SqlCommandBuilder(mySqlDataAdapter)
mySqlDataAdapter.Update(ds)

It works great, but how do I retrieve the autoincrement ID field of the row
just added???

Please help!

Nov 19 '05 #2
jay
Thanks for your help. It pointed me in the right direction.
First, the sql SELECT command was similar to: "SELECT * FROM myTable WHERE
ID = -2;" to give me an empty dataset (to lowering network traffic, increase
performance, etc). When I add the row and update the dataset, the ID column
value is returned as System.DBNull. I tried the same routine but changed the
SELECT command to "SELECT * FROM myTable;" and I got the same results, i.e.,
when I printed drow("ID") in the immediate window after the dataset update,
it still gave me a DBNull value.
The problem seems to be that the autoIncrement ID isn't assigned until the
update but the change is reflected, at this stage, only the back-end database
and NOT in the dataset. To update the dataset with the new ID, you'd have to
do a sqlDataAdapter.Fill(dataset), but how would you know what SELECT
statement to use? If you just ask for the record with the MAX ID, you might
be getting the ID of a record entered by another user right after yours!
The solution I used was to do another query in the table, after the update,
looking for different fields that, in combination, are unique to the record.
Problem is, I might not be able to do that the next time. What am I missing?
Jay

"jfleeson" wrote:
Jay,

How did you create your sql command? is it a stored proc.?

When you call mySqlDataAdapter.Update it should write all the changed /
added reocrds in the data set to the database and update the dataset with the
identities. So all you need to do the get the id from the dataset row. i.e.
drow("id") if "id" is the name of the identity column.

if this doesn't work let me know.

"jay" wrote:
I am using the dataset object to add a row to a sql server database in vb.net
code, as follows:

dim drow as DataRow
dim cmdBld as new SqlCommandBuilder(mySqlDataAdapter)
ds.tables(0).NewRow()
drow("field1") = data for field 1
and so forth
ds.tables(0).rows.add(drow)
cmdBld = New SqlCommandBuilder(mySqlDataAdapter)
mySqlDataAdapter.Update(ds)

It works great, but how do I retrieve the autoincrement ID field of the row
just added???

Please help!

Nov 19 '05 #3
Jay,

Here is an example of an insert command that ID is is an identity

this.sqlInsertCommand1.CommandText = "INSERT INTO Codeword(Codeword,
DateActivated, DateDeactivated, Status) VALUES " +
(@Codeword, @DateActivated, @DateDeactivated, @Status); SELECT CodewordID,
Codeword, " +
"DateActivated, DateDeactivated, Status FROM Codeword WHERE (CodewordID =
@@IDENTITY)";

note the second part of the statement. This should be executed as one
command.

Hope this helps.

"jay" wrote:
Thanks for your help. It pointed me in the right direction.
First, the sql SELECT command was similar to: "SELECT * FROM myTable WHERE
ID = -2;" to give me an empty dataset (to lowering network traffic, increase
performance, etc). When I add the row and update the dataset, the ID column
value is returned as System.DBNull. I tried the same routine but changed the
SELECT command to "SELECT * FROM myTable;" and I got the same results, i.e.,
when I printed drow("ID") in the immediate window after the dataset update,
it still gave me a DBNull value.
The problem seems to be that the autoIncrement ID isn't assigned until the
update but the change is reflected, at this stage, only the back-end database
and NOT in the dataset. To update the dataset with the new ID, you'd have to
do a sqlDataAdapter.Fill(dataset), but how would you know what SELECT
statement to use? If you just ask for the record with the MAX ID, you might
be getting the ID of a record entered by another user right after yours!
The solution I used was to do another query in the table, after the update,
looking for different fields that, in combination, are unique to the record.
Problem is, I might not be able to do that the next time. What am I missing?
Jay

"jfleeson" wrote:
Jay,

How did you create your sql command? is it a stored proc.?

When you call mySqlDataAdapter.Update it should write all the changed /
added reocrds in the data set to the database and update the dataset with the
identities. So all you need to do the get the id from the dataset row. i.e.
drow("id") if "id" is the name of the identity column.

if this doesn't work let me know.

"jay" wrote:
I am using the dataset object to add a row to a sql server database in vb.net
code, as follows:

dim drow as DataRow
dim cmdBld as new SqlCommandBuilder(mySqlDataAdapter)
ds.tables(0).NewRow()
drow("field1") = data for field 1
and so forth
ds.tables(0).rows.add(drow)
cmdBld = New SqlCommandBuilder(mySqlDataAdapter)
mySqlDataAdapter.Update(ds)

It works great, but how do I retrieve the autoincrement ID field of the row
just added???

Please help!

Nov 19 '05 #4
jay
Thanks! It worked like a champ. I used CommandSql.ExecuteScalar since all I
wanted back was the ID. I didn't know that you could stack up SQL statements
like that.
Jay

"jfleeson" wrote:
Jay,

Here is an example of an insert command that ID is is an identity

this.sqlInsertCommand1.CommandText = "INSERT INTO Codeword(Codeword,
DateActivated, DateDeactivated, Status) VALUES " +
(@Codeword, @DateActivated, @DateDeactivated, @Status); SELECT CodewordID,
Codeword, " +
"DateActivated, DateDeactivated, Status FROM Codeword WHERE (CodewordID =
@@IDENTITY)";

note the second part of the statement. This should be executed as one
command.

Hope this helps.

"jay" wrote:
Thanks for your help. It pointed me in the right direction.
First, the sql SELECT command was similar to: "SELECT * FROM myTable WHERE
ID = -2;" to give me an empty dataset (to lowering network traffic, increase
performance, etc). When I add the row and update the dataset, the ID column
value is returned as System.DBNull. I tried the same routine but changed the
SELECT command to "SELECT * FROM myTable;" and I got the same results, i.e.,
when I printed drow("ID") in the immediate window after the dataset update,
it still gave me a DBNull value.
The problem seems to be that the autoIncrement ID isn't assigned until the
update but the change is reflected, at this stage, only the back-end database
and NOT in the dataset. To update the dataset with the new ID, you'd have to
do a sqlDataAdapter.Fill(dataset), but how would you know what SELECT
statement to use? If you just ask for the record with the MAX ID, you might
be getting the ID of a record entered by another user right after yours!
The solution I used was to do another query in the table, after the update,
looking for different fields that, in combination, are unique to the record.
Problem is, I might not be able to do that the next time. What am I missing?
Jay

"jfleeson" wrote:
Jay,

How did you create your sql command? is it a stored proc.?

When you call mySqlDataAdapter.Update it should write all the changed /
added reocrds in the data set to the database and update the dataset with the
identities. So all you need to do the get the id from the dataset row. i.e.
drow("id") if "id" is the name of the identity column.

if this doesn't work let me know.

"jay" wrote:

> I am using the dataset object to add a row to a sql server database in vb.net
> code, as follows:
>
> dim drow as DataRow
> dim cmdBld as new SqlCommandBuilder(mySqlDataAdapter)
> ds.tables(0).NewRow()
> drow("field1") = data for field 1
> and so forth
> ds.tables(0).rows.add(drow)
> cmdBld = New SqlCommandBuilder(mySqlDataAdapter)
> mySqlDataAdapter.Update(ds)
>
> It works great, but how do I retrieve the autoincrement ID field of the row
> just added???
>
> Please help!

Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jerry Orr | last post by:
I am attempting to retrieve various MVS dataset allocation parameters (lrecl, block size, etc) from a C/C++ program. The C/C++ Run-time Library Reference states that the function svc99() can be...
3
by: John Francisco Williams | last post by:
Hi, I searched through this newsgroup looking for an answer for this, but didn't find any (it probably is here. Just didn't find it). I am trying to retrieve information from two related tables,...
5
by: Roy Gourgi | last post by:
Hi, I am used to working in Visual FoxPro and I would like to be able to create a database and store and retrieve information from it. What is the simplest way to do it and what should I be...
1
by: DaveF | last post by:
How can I retrieve display_names Dim custDS As DataSet = New DataSet Dim mySQL, mySQL2, mySQL3 As String
6
by: enrique | last post by:
Hello, I simpy want to place a record ID into a session variable. I know how to create session variables, I'm just can't figure out the syntax for the record item when I'm working with a...
12
by: jaYPee | last post by:
I have currently using a dataset to access my data from sql server 2000. The dataset contains 3 tables that is related to each other. parent/child/grandchild relationship. My problem is it's very...
0
by: r1 | last post by:
I am relatively inexperienced in using delegates and asynchronous methods. I read several articles, and then I developed my own code with a mission to improve the performance. Wow! I cannot...
1
by: whidbey | last post by:
Hello friends, I am whidbey, new to thescripts and dot net as well.I am working over Online Shopping Cart,web application.I design a page (webform5.aspx) where user search books then select the books...
0
by: whidbey | last post by:
Hello friends, I am whidbey, new to thescripts and dot net as well.I am working over Online Shopping Cart,web application.I design a page (webform5.aspx) where user search books then select the books...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.