tags:

views:

14

answers:

1

I'm using vb.net 2008 and ADO.NET. When using the ADO CommandBuilder and the Insert Command how can I get the newly created Key? (my Table as an Identity column and this is the key.)

A: 

Try your insert command somethign like this

insert into table_name values('first value','second value') set @id = SCOPE_IDENTITY()

And then you can set @id Parameter as below

SqlParameter IDParameter =
                     new SqlParameter("@id",
                                    SqlDbType.Int);

 IDParameter.Direction =  ParameterDirection.Output;
            insertCommand.Parameters.Add(IDParameter);

Finally you can get the value of id parameter in an int variable

int id = (int)IDParameter.Value;
Nadeem
you are not using CommandBuilder. My question was "When using the ADO CommandBuilder and the Insert Command"
bochur1