HighTechTalks DotNet Forums  

Get back uniqueid?

Dotnet Framework (ADO.net) microsoft.public.dotnet.framework.adonet


Discuss Get back uniqueid? in the Dotnet Framework (ADO.net) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
benji
 
Posts: n/a

Default Get back uniqueid? - 12-04-2007 , 02:37 PM






Hi,

I'm using VS2008, and the dataset designer to access my SQL Server 2005 DB.
I can insert a row into a table and then call the adapter to update. No
problem, except I want to get back the ID field of the newly created row.
I've tried the following:

DataSet1.CustomersRow myrow = myCustomersTable.NewCustomersRow();
myrow.field1 = "xyz"

myCustomersTable.AddCustomersRow(myrow);
myCustomersAdapter.Update(myCustomersTable);

Label1.Text = myrow.NonGroupChellengeID.ToString()

This returns -1. I can get the right value if I access the row directly on
the datatable after calling update, but as it stands I just access the last
row, and that might not be clean. ANy suggestions for this? I have to imagine
this is a common scenario?

Thanks...

-Ben

Reply With Quote
  #2  
Old   
Miha Markic
 
Posts: n/a

Default Re: Get back uniqueid? - 12-04-2007 , 03:58 PM






You should check out the definition of myCustomersAdapter.InsertCommand - it
should have ;SELECT Scope_Identity() at the end or something like that.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

"benji" <benji (AT) discussions (DOT) microsoft.com> wrote

Quote:
Hi,

I'm using VS2008, and the dataset designer to access my SQL Server 2005
DB.
I can insert a row into a table and then call the adapter to update. No
problem, except I want to get back the ID field of the newly created row.
I've tried the following:

DataSet1.CustomersRow myrow = myCustomersTable.NewCustomersRow();
myrow.field1 = "xyz"

myCustomersTable.AddCustomersRow(myrow);
myCustomersAdapter.Update(myCustomersTable);

Label1.Text = myrow.NonGroupChellengeID.ToString()

This returns -1. I can get the right value if I access the row directly on
the datatable after calling update, but as it stands I just access the
last
row, and that might not be clean. ANy suggestions for this? I have to
imagine
this is a common scenario?

Thanks...

-Ben


Reply With Quote
  #3  
Old   
benji
 
Posts: n/a

Default Re: Get back uniqueid? - 12-04-2007 , 04:19 PM



Hi Miha,

2 followup questions:

1) Would I not need a stored procedure for a compound SQL stamement?
2) After I have that in place, where would I retrieve the value in the data
access layer?

Thanks..

-Ben


"Miha Markic" wrote:

Quote:
You should check out the definition of myCustomersAdapter.InsertCommand - it
should have ;SELECT Scope_Identity() at the end or something like that.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

"benji" <benji (AT) discussions (DOT) microsoft.com> wrote in message
news:0C418196-09A0-45F5-897D-0A9EFC0A4C87 (AT) microsoft (DOT) com...
Hi,

I'm using VS2008, and the dataset designer to access my SQL Server 2005
DB.
I can insert a row into a table and then call the adapter to update. No
problem, except I want to get back the ID field of the newly created row.
I've tried the following:

DataSet1.CustomersRow myrow = myCustomersTable.NewCustomersRow();
myrow.field1 = "xyz"

myCustomersTable.AddCustomersRow(myrow);
myCustomersAdapter.Update(myCustomersTable);

Label1.Text = myrow.NonGroupChellengeID.ToString()

This returns -1. I can get the right value if I access the row directly on
the datatable after calling update, but as it stands I just access the
last
row, and that might not be clean. ANy suggestions for this? I have to
imagine
this is a common scenario?

Thanks...

-Ben



Reply With Quote
  #4  
Old   
Miha Markic
 
Posts: n/a

Default Re: Get back uniqueid? - 12-04-2007 , 04:54 PM




"benji" <benji (AT) discussions (DOT) microsoft.com> wrote

Quote:
Hi Miha,

2 followup questions:

1) Would I not need a stored procedure for a compound SQL stamement?
No, you can have more than one statement in commandtext (depending on the
provider, sql can handle multiple statements)

Quote:
2) After I have that in place, where would I retrieve the value in the
data
access layer?
Depends on the mappings associated with DataAdapter.

HTH
--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Quote:
Thanks..

-Ben


"Miha Markic" wrote:

You should check out the definition of myCustomersAdapter.InsertCommand -
it
should have ;SELECT Scope_Identity() at the end or something like that.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

"benji" <benji (AT) discussions (DOT) microsoft.com> wrote in message
news:0C418196-09A0-45F5-897D-0A9EFC0A4C87 (AT) microsoft (DOT) com...
Hi,

I'm using VS2008, and the dataset designer to access my SQL Server 2005
DB.
I can insert a row into a table and then call the adapter to update. No
problem, except I want to get back the ID field of the newly created
row.
I've tried the following:

DataSet1.CustomersRow myrow = myCustomersTable.NewCustomersRow();
myrow.field1 = "xyz"

myCustomersTable.AddCustomersRow(myrow);
myCustomersAdapter.Update(myCustomersTable);

Label1.Text = myrow.NonGroupChellengeID.ToString()

This returns -1. I can get the right value if I access the row directly
on
the datatable after calling update, but as it stands I just access the
last
row, and that might not be clean. ANy suggestions for this? I have to
imagine
this is a common scenario?

Thanks...

-Ben




Reply With Quote
  #5  
Old   
benji
 
Posts: n/a

Default Re: Get back uniqueid? - 12-04-2007 , 10:04 PM



Hi Miha,

I added another query to the table, which was an insert query. I then
appended:

;SELECT Scope_Identity()

it compiled fine. It generated the expected insert method in the
tableadapter class, and the signature had it returning an int. However, the
int always returns 1 (probably indicating success). Could you go into more
detail regarding what you mean by " Depends on the mappings associated with
DataAdapter."? Thanks...

-Ben



"Miha Markic" wrote:

Quote:
"benji" <benji (AT) discussions (DOT) microsoft.com> wrote in message
news:0D4D5A82-0271-4E4E-AADC-282B662E4129 (AT) microsoft (DOT) com...
Hi Miha,

2 followup questions:

1) Would I not need a stored procedure for a compound SQL stamement?

No, you can have more than one statement in commandtext (depending on the
provider, sql can handle multiple statements)

2) After I have that in place, where would I retrieve the value in the
data
access layer?

Depends on the mappings associated with DataAdapter.

HTH
--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Thanks..

-Ben


"Miha Markic" wrote:

You should check out the definition of myCustomersAdapter.InsertCommand -
it
should have ;SELECT Scope_Identity() at the end or something like that.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

"benji" <benji (AT) discussions (DOT) microsoft.com> wrote in message
news:0C418196-09A0-45F5-897D-0A9EFC0A4C87 (AT) microsoft (DOT) com...
Hi,

I'm using VS2008, and the dataset designer to access my SQL Server 2005
DB.
I can insert a row into a table and then call the adapter to update. No
problem, except I want to get back the ID field of the newly created
row.
I've tried the following:

DataSet1.CustomersRow myrow = myCustomersTable.NewCustomersRow();
myrow.field1 = "xyz"

myCustomersTable.AddCustomersRow(myrow);
myCustomersAdapter.Update(myCustomersTable);

Label1.Text = myrow.NonGroupChellengeID.ToString()

This returns -1. I can get the right value if I access the row directly
on
the datatable after calling update, but as it stands I just access the
last
row, and that might not be clean. ANy suggestions for this? I have to
imagine
this is a common scenario?

Thanks...

-Ben





Reply With Quote
  #6  
Old   
Miha Markic
 
Posts: n/a

Default Re: Get back uniqueid? - 12-05-2007 , 03:31 AM



Here is the idea

a) you'd need a mapping between database column and your column, i.e. (Id
<=> Id):
adapter.TableMapping.ColumnMappings.Add("Id", "Id");
b) you'd append this statement after the insert:
SELECT Id = SCOPE_IDENTITY()

BTW all this code is generated for you, if you drag & drop a table from
Server Explorer to dataset designer - you could try and see what code it
generates.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

"benji" <benji (AT) discussions (DOT) microsoft.com> wrote

Quote:
Hi Miha,

I added another query to the table, which was an insert query. I then
appended:

;SELECT Scope_Identity()

it compiled fine. It generated the expected insert method in the
tableadapter class, and the signature had it returning an int. However,
the
int always returns 1 (probably indicating success). Could you go into more
detail regarding what you mean by " Depends on the mappings associated
with
DataAdapter."? Thanks...


Reply With Quote
  #7  
Old   
benji
 
Posts: n/a

Default Re: Get back uniqueid? - 12-05-2007 , 10:09 AM



Hi Miha,

I already have this mapping in place for the uniqueid column. When I do a
fill, I can access the value for every row through the generated dataset
class. But it seems I can't access it through my generated Query, despite
appending
;SELECT ID = Scope_Identity()
The method generated simply returns an int for success or failure of
insertion. I'm not sure how having the UniqueID mapped into the table helps
in this way?

"Miha Markic" wrote:

Quote:
Here is the idea

a) you'd need a mapping between database column and your column, i.e. (Id
=> Id):
adapter.TableMapping.ColumnMappings.Add("Id", "Id");
b) you'd append this statement after the insert:
SELECT Id = SCOPE_IDENTITY()

BTW all this code is generated for you, if you drag & drop a table from
Server Explorer to dataset designer - you could try and see what code it
generates.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

"benji" <benji (AT) discussions (DOT) microsoft.com> wrote in message
news:1F0FCA65-3644-4A5A-9DC8-7654D2A23BC9 (AT) microsoft (DOT) com...
Hi Miha,

I added another query to the table, which was an insert query. I then
appended:

;SELECT Scope_Identity()

it compiled fine. It generated the expected insert method in the
tableadapter class, and the signature had it returning an int. However,
the
int always returns 1 (probably indicating success). Could you go into more
detail regarding what you mean by " Depends on the mappings associated
with
DataAdapter."? Thanks...



Reply With Quote
  #8  
Old   
benji
 
Posts: n/a

Default Re: Get back uniqueid? - 12-05-2007 , 10:31 AM



Hi Miha,

I found the solution. After creating the query, I had to go to its
properties and change "ExecuteMode" from nonquery to scalar.

Best,

-Ben

"benji" wrote:

Quote:
Hi Miha,

I already have this mapping in place for the uniqueid column. When I do a
fill, I can access the value for every row through the generated dataset
class. But it seems I can't access it through my generated Query, despite
appending
;SELECT ID = Scope_Identity()
The method generated simply returns an int for success or failure of
insertion. I'm not sure how having the UniqueID mapped into the table helps
in this way?

"Miha Markic" wrote:

Here is the idea

a) you'd need a mapping between database column and your column, i.e. (Id
=> Id):
adapter.TableMapping.ColumnMappings.Add("Id", "Id");
b) you'd append this statement after the insert:
SELECT Id = SCOPE_IDENTITY()

BTW all this code is generated for you, if you drag & drop a table from
Server Explorer to dataset designer - you could try and see what code it
generates.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

"benji" <benji (AT) discussions (DOT) microsoft.com> wrote in message
news:1F0FCA65-3644-4A5A-9DC8-7654D2A23BC9 (AT) microsoft (DOT) com...
Hi Miha,

I added another query to the table, which was an insert query. I then
appended:

;SELECT Scope_Identity()

it compiled fine. It generated the expected insert method in the
tableadapter class, and the signature had it returning an int. However,
the
int always returns 1 (probably indicating success). Could you go into more
detail regarding what you mean by " Depends on the mappings associated
with
DataAdapter."? Thanks...



Reply With Quote
  #9  
Old   
Cor Ligthert[MVP]
 
Posts: n/a

Default Re: Get back uniqueid? - 12-05-2007 , 11:46 PM



Benji,

Are you sure you are talking about a Global Unique ID or are you just
talking about an integer that is set by SQL automaticly to the next
available free number accoording its seed (I am almost sure you are talking
about the latter)

As you use a GUID, then set it in advance in your program, its is globaly
unique so there should not be any problem.

Cor

"benji" <benji (AT) discussions (DOT) microsoft.com> schreef in bericht
news:0C418196-09A0-45F5-897D-0A9EFC0A4C87 (AT) microsoft (DOT) com...
Quote:
Hi,

I'm using VS2008, and the dataset designer to access my SQL Server 2005
DB.
I can insert a row into a table and then call the adapter to update. No
problem, except I want to get back the ID field of the newly created row.
I've tried the following:

DataSet1.CustomersRow myrow = myCustomersTable.NewCustomersRow();
myrow.field1 = "xyz"

myCustomersTable.AddCustomersRow(myrow);
myCustomersAdapter.Update(myCustomersTable);

Label1.Text = myrow.NonGroupChellengeID.ToString()

This returns -1. I can get the right value if I access the row directly on
the datatable after calling update, but as it stands I just access the
last
row, and that might not be clean. ANy suggestions for this? I have to
imagine
this is a common scenario?

Thanks...

-Ben


Reply With Quote
  #10  
Old   
benji
 
Posts: n/a

Default Re: Get back uniqueid? - 12-14-2007 , 06:23 PM



Hi Cor,

You are right, I want the integer set by SQL. Everything appears to be
functioning, what in my code suggests I'm using a GUID?

Thanks...

-Ben

"Cor Ligthert[MVP]" wrote:

Quote:
Benji,

Are you sure you are talking about a Global Unique ID or are you just
talking about an integer that is set by SQL automaticly to the next
available free number accoording its seed (I am almost sure you are talking
about the latter)

As you use a GUID, then set it in advance in your program, its is globaly
unique so there should not be any problem.

Cor

"benji" <benji (AT) discussions (DOT) microsoft.com> schreef in bericht
news:0C418196-09A0-45F5-897D-0A9EFC0A4C87 (AT) microsoft (DOT) com...
Hi,

I'm using VS2008, and the dataset designer to access my SQL Server 2005
DB.
I can insert a row into a table and then call the adapter to update. No
problem, except I want to get back the ID field of the newly created row.
I've tried the following:

DataSet1.CustomersRow myrow = myCustomersTable.NewCustomersRow();
myrow.field1 = "xyz"

myCustomersTable.AddCustomersRow(myrow);
myCustomersAdapter.Update(myCustomersTable);

Label1.Text = myrow.NonGroupChellengeID.ToString()

This returns -1. I can get the right value if I access the row directly on
the datatable after calling update, but as it stands I just access the
last
row, and that might not be clean. ANy suggestions for this? I have to
imagine
this is a common scenario?

Thanks...

-Ben


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.