| 
                
Please start any new threads on our new 
    site at https://forums.sqlteam.com.  We've got lots of great SQL Server
    experts to answer whatever question you can come up with. 
    
        | 
                
                    | 
                            
                                | Author | Topic |  
                                    | programerPosting Yak  Master
 
 
                                        221 Posts | 
                                            
                                            |  Posted - 2010-08-03 : 19:13:08 
 |  
                                            | Hi,How to get ID?My table:UserIdCardId - Primarykey - autoincrementAttibuteNameId save the CardId. Why will not get my ID, which I have saved?Using SCORE_IDENTITYMy code:            string queryString = "SELECT SCOPE_IDENTITY()";            SqlCommand command = new SqlCommand(queryString, connection);            connection.Open();            command.Connection = connection;            command.ExecuteNonQuery();            command.CommandText = "SELECT SCOPE_IDENTITY()";            object o = command.ExecuteScalar();           // System.Diagnostics.Debug.WriteLine("SCOPE_IDENTIY TYPE: " + o.GetType().FullName);           // System.Diagnostics.Debug.WriteLine("SCOPE_IDENTIY VALUE: " + o.ToString());            Label3.Text = ("SCOPE_IDENTIY VALUE: " + o.ToString());            if (o != DBNull.Value)            {                // Do something                              }            }        } |  |  
                                    | tkizerAlmighty SQL Goddess
 
 
                                    38200 Posts |  |  
                                    | programerPosting Yak  Master
 
 
                                    221 Posts | 
                                        
                                          |  Posted - 2010-08-03 : 19:39:31 
 |  
                                          | quote:You mean like this:                  InsertCommand = "INSERT INTO [tbl_paymentDetails] ([UserId], [Value], [AttributeName] [CardID]) VALUES (@ UserId, @ Value, @ AttributeName, @ CardId) SELECT SCOPE_IDENTITY ()"Originally posted by tkizer
 You have to run your INSERT statement right before you get it.  It seems you are opening a new connection to get SCOPE_IDENTITY(), so you can't get the id value inserted.  Use the same connection and grab the id directly after the INSERT command.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog
 
 |  
                                          |  |  |  
                                    | tkizerAlmighty SQL Goddess
 
 
                                    38200 Posts |  |  
                                    | robvolkMost Valuable Yak
 
 
                                    15732 Posts | 
                                        
                                          |  Posted - 2010-08-04 : 11:29:02 
 |  
                                          | If you're INSERTing to a SQL Server 2005 or higher database, use the OUTPUT clause of the INSERT command: INSERT INTO [tbl_paymentDetails] ([UserId], [Value], [AttributeName]) OUTPUT inserted.IDENTITYCOL AS CardID VALUES (@UserId, @Value, @AttributeName) |  
                                          |  |  |  
                                    | programerPosting Yak  Master
 
 
                                    221 Posts | 
                                        
                                          |  Posted - 2010-08-04 : 11:56:13 
 |  
                                          | quote:thanks this is work:                  InsertCommand="INSERT INTO [tbl_paymentDetails] ([UserId], [Value],[CardId], [AttributeName]) OUTPUT inserted.CardId AS CardId VALUES (@UserId, @Value, @AttributeName,@CardId)"I wonder how the last stored record show (CardId)?thanks.Originally posted by robvolk
 If you're INSERTing to a SQL Server 2005 or higher database, use the OUTPUT clause of the INSERT command:
 INSERT INTO [tbl_paymentDetails] ([UserId], [Value], [AttributeName]) OUTPUT inserted.IDENTITYCOL AS CardID VALUES (@UserId, @Value, @AttributeName) 
 |  
                                          |  |  |  
                                    | robvolkMost Valuable Yak
 
 
                                    15732 Posts | 
                                        
                                          |  Posted - 2010-08-04 : 12:40:00 
 |  
                                          | Your column list is incorrect:InsertCommand="INSERT INTO [tbl_paymentDetails] ([UserId], [Value],[CardId], [AttributeName]) OUTPUT inserted.CardId AS CardId VALUES (@UserId, @Value, @AttributeName,@CardId)"Should be:InsertCommand="INSERT INTO [tbl_paymentDetails] ([UserId], [Value], [AttributeName],[CardId])OUTPUT inserted.CardId AS CardId VALUES (@UserId, @Value, @AttributeName,@CardId)"And why are you supplying a @CardID parameter when you want to retrieve the auto-generated CardID? |  
                                          |  |  |  
                                    | programerPosting Yak  Master
 
 
                                    221 Posts | 
                                        
                                          |  Posted - 2010-08-04 : 12:43:56 
 |  
                                          | quote:Because the record CardId need to save data in another table.Originally posted by robvolk
 Your column list is incorrect:InsertCommand="INSERT INTO [tbl_paymentDetails] ([UserId], [Value],[CardId], [AttributeName]) OUTPUT inserted.CardId AS CardId VALUES (@UserId, @Value, @AttributeName,@CardId)"Should be:InsertCommand="INSERT INTO [tbl_paymentDetails] ([UserId], [Value], [AttributeName],[CardId])OUTPUT inserted.CardId AS CardId VALUES (@UserId, @Value, @AttributeName,@CardId)"And why are you supplying a @CardID parameter when you want to retrieve the auto-generated CardID?
 
 |  
                                          |  |  |  
                                    | robvolkMost Valuable Yak
 
 
                                    15732 Posts | 
                                        
                                          |  Posted - 2010-08-04 : 12:52:03 
 |  
                                          | You won't get the auto-generated CardID in the variable, it will be returned as a result.  Passing it to the VALUES clause indicates you want to INSERT that value into the table, not return the value from it. |  
                                          |  |  |  
                                    | KristenTest
 
 
                                    22859 Posts | 
                                        
                                          |  Posted - 2010-08-04 : 13:07:19 
 |  
                                          | "INSERT INTO MyTable (Col1, Col2, ...) VALUES (@Param1, @param2, ...) SELECT SCOPE_IDENTITY()"I reckon that should be fine. Not sure about having a blank space between "@" and "Name", or space between "SCOPE_IDENTITY" and "()" though ... |  
                                          |  |  |  
                                    | programerPosting Yak  Master
 
 
                                    221 Posts | 
                                        
                                          |  Posted - 2010-08-04 : 16:14:10 
 |  
                                          | quote:This is my code:                   InsertCommand="INSERT INTO [tbl_payments] ([UserId], [PaymentType]) VALUES (@UserId, @PaymentType);SELECT SCOPE_IDENTITY()"Works!Now I want to know:Label1.Text -  show how the last recorded information?Please helpOriginally posted by Kristen
 "INSERT INTO MyTable (Col1, Col2, ...) VALUES (@Param1, @param2, ...) SELECT SCOPE_IDENTITY()"I reckon that should be fine. Not sure about having a blank space between "@" and "Name", or space between "SCOPE_IDENTITY" and "()" though ...
 
 |  
                                          |  |  |  
                                    | KristenTest
 
 
                                    22859 Posts | 
                                        
                                          |  Posted - 2010-08-04 : 16:28:14 
 |  
                                          | SELECT SCOPE_IDENTITY() is returned as a resultset, so you need to process it as a resultsetI don't know anything about DotNet, but my understanding is that you cannot usecommand.ExecuteNonQuery()for this type of query, because it IS a Query and returns a ResultSetYou may need to do:SET NOCOUNT ON INSERT INTO [tbl_payments] ([UserId], [PaymentType]) VALUES (@UserId, @PaymentType);SELECT SCOPE_IDENTITY()in order not to get an informational message after the INSERT statement |  
                                          |  |  |  
                                |  |  |  |  |  |