How to output ID no.?http://forums.asp.net/t/1800838.aspx/1?How+to+output+ID+no+Mon, 07 May 2012 12:46:10 -040018008384968438http://forums.asp.net/p/1800838/4968438.aspx/1?How+to+output+ID+no+How to output ID no.? <p>Hi,</p> <p>When I insert form details in the database table, I also have an id no. in the database, which auto generated.</p> <p>I want to fetch i.e output&nbsp;the auto id no. so that I can use it for further insertions/other transactions. How to do that?</p> 2012-05-07T07:14:21-04:004968459http://forums.asp.net/p/1800838/4968459.aspx/1?Re+How+to+output+ID+no+Re: How to output ID no.? <p>USe Output parameter and assign Scope_IDentity&nbsp; after insertion if you use stored proc.</p> <p>For query:</p> <pre class="prettyprint">strSQL = &quot;INSERT INTO tablename (name) VALUES (@name);SELECT SCOPE_IDENTITY()&quot; SQLCommand.CommandText = strSQL Id = SQLCommand.ExecuteScalar()</pre> <p></p> <p><a href="http://stackoverflow.com/questions/3913115/scope-identity-in-c-sharp-range">http://stackoverflow.com/questions/3913115/scope-identity-in-c-sharp-range</a></p> 2012-05-07T07:20:50-04:004968461http://forums.asp.net/p/1800838/4968461.aspx/1?Re+How+to+output+ID+no+Re: How to output ID no.? <pre class="prettyprint">string query = &quot;Insert Into Categories (CategoryName) Values (?)&quot;; string query2 = &quot;Select @@Identity&quot;; int ID; string connect = &quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Northwind.mdb&quot;; using (OleDbConnection conn = new OleDbConnection(connect)) { using (OleDbCommand cmd = new OleDbCommand(query, conn)) { cmd.Parameters.AddWithValue(&quot;&quot;, Category.Text); conn.Open(); cmd.ExecuteNonQuery(); cmd.CommandText = query2; ID = (int)cmd.ExecuteScalar(); } }</pre> <p>&nbsp;</p> <p>also refer to this link</p> <p><a href="http://www.mikesdotnetting.com/Article/54/Getting-the-identity-of-the-most-recently-added-record">http://www.mikesdotnetting.com/Article/54/Getting-the-identity-of-the-most-recently-added-record</a></p> 2012-05-07T07:22:01-04:004968465http://forums.asp.net/p/1800838/4968465.aspx/1?Re+How+to+output+ID+no+Re: How to output ID no.? <p>The problem is that since it is auto generated,&nbsp;I don't explicitly insert it in the database. Even in my SP, I have not used it in the parameters. Then how do you suggest me to add Output parameter?</p> <p>Pl guide me...</p> 2012-05-07T07:24:32-04:004968477http://forums.asp.net/p/1800838/4968477.aspx/1?Re+How+to+output+ID+no+Re: How to output ID no.? <p>You need not to insert in autogenerated column, when you insert other fields it is autogenerated and scope_identity will return the auto generated value.</p> <p>For SP:</p> <pre class="prettyprint">CREATE PROCEDURE dbo.DoSomething ( @var1 INT = NULL, @var2 INT = NULL, @var3 DATE = NULL ) AS BEGIN INSERT INTO atable ( vara, varb, varc ) VALUES ( @var1, @var2, @var3 ) SELECT SCOPE_IDENTITY() END</pre> <p><br /><br /></p> <p>C#</p> <p></p> <pre class="prettyprint">int result = 0; /// create command SqlCommand cmd = new SqlCommand("DoSomething", this.OpenSqlConnection()); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@var1", thing.value1); cmd.Parameters.AddWithValue("@var2", thing.value2); cmd.Parameters.AddWithValue("@var3", thing.value3); /// send data to db result = (int)cmd.ExecuteScalar(); </pre> <p><br> <br> </p> 2012-05-07T07:32:38-04:004968485http://forums.asp.net/p/1800838/4968485.aspx/1?Re+How+to+output+ID+no+Re: How to output ID no.? <p>hai</p> <p><a href="http://stackoverflow.com/questions/3142444/stored-procedure-return-identity-as-output-parameter-or-scalar">http://stackoverflow.com/questions/3142444/stored-procedure-return-identity-as-output-parameter-or-scalar</a></p> <p><a href="http://www.objectreference.net/post/SCOPE_IDENTITY%28%29-return-the-id-from-the-database-on-insert.aspx">http://www.objectreference.net/post/SCOPE_IDENTITY()-return-the-id-from-the-database-on-insert.aspx</a></p> <p>thank u</p> <p></p> 2012-05-07T07:33:41-04:004968519http://forums.asp.net/p/1800838/4968519.aspx/1?Re+How+to+output+ID+no+Re: How to output ID no.? <p>@urenjoy @sriramabi</p> <p>Thanks!</p> <p>One part is done. For the second part, pl tell me how to incorporate this Scope_Identity() into the code behind file, i.e where I insert the form values?</p> <p>I have inserted all fields as parameters which go into a function where a SP is called.</p> 2012-05-07T07:45:45-04:004968545http://forums.asp.net/p/1800838/4968545.aspx/1?Re+How+to+output+ID+no+Re: How to output ID no.? <p></p> <blockquote><span class="icon-blockquote"></span> <h4>coolpal9</h4> <p></p> <p>@urenjoy @sriramabi</p> <p>Thanks!</p> <p>One part is done. For the second part, pl tell me how to incorporate this Scope_Identity() into the code behind file, i.e where I insert the form values?</p> <p>I have inserted all fields as parameters which go into a function where a SP is called.</p> <p></p> </blockquote> <p></p> <p>hey.i have one examble pls try this way..</p> <p>its code behond side code</p> <pre class="prettyprint">public <em><strong>int</strong> </em>InsertBatchResponseStatus(string BatchId, int ResponseStatusCode, string LABatchId, string AddedBy, DateTime AddedDate) { using (SqlConnection con = new SqlConnection(connection)) { SqlCommand cmd = new SqlCommand(&quot;[uspInsertBatchResponseStatus]&quot;, con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(&quot;@BatchId&quot;, SqlDbType.VarChar, (500)).Value = BatchId; cmd.Parameters.Add(&quot;@ResponseStatusCode&quot;, SqlDbType.Int).Value = ResponseStatusCode; cmd.Parameters.Add(&quot;@LABatchId&quot;, SqlDbType.VarChar, (500)).Value = LABatchId; cmd.Parameters.Add(&quot;@AddedBy&quot;, SqlDbType.VarChar).Value = AddedBy; cmd.Parameters.Add(&quot;@AddedDate&quot;, SqlDbType.DateTime).Value = AddedDate; con.Open(); <em><strong>int DetailId = Convert.ToInt32(cmd.ExecuteScalar());</strong></em> return DetailId; } }</pre> <p><br /><br /></p> <p></p> <p>and here my Stored Procedure</p> <pre class="prettyprint">CREATE PROCEDURE [dbo].[uspInsertBatchResponseStatus] ( @BatchId varchar(500), @ResponseStatusCode int, @LABatchId varchar(500), @AddedBy varchar(200), @AddedDate datetime ) AS BEGIN SET NOCOUNT ON INSERT INTO BatchDetails(BatchId,ResponseStatusCode,BatchQueryStatusCode,BatchStatusCode,DeleteBatchResponseCode,LABatchId,AddedBy,AddedDate) VALUES(@BatchId,@ResponseStatusCode,1,1,2,@LABatchId,@AddedBy,@AddedDate) SELECT SCOPE_IDENTITY() AS [@@DetailId]; END </pre> <p><br> <br> </p> <p></p> <p>thank u</p> 2012-05-07T07:56:27-04:004968601http://forums.asp.net/p/1800838/4968601.aspx/1?Re+How+to+output+ID+no+Re: How to output ID no.? <p>The way I insert the values into the database is:</p> <p>InsertDetails= new InsertDetails(); //InsertDetails is a .cs file</p> <p>bool ret = db.AddDetails(Batchid, ResponseStatusCode, LABatchId, AddedBy, AddedDate) //AddDetails() is a method in InsertDetails()</p> <p>In the .cs file:</p> <pre class="prettyprint">public bool AddDetails(string Batchid, string ResponseStatusCode, string LABatchId, string AddedBy, string AddedDate) { try { SqlParameter[] myparams = BuildSPParamsUnicode(&quot;Batchid, ResponseStatusCode, LABatchId, AddedBy, AddedDate&quot;, Batchid, ResponseStatusCode, LABatchId, AddedBy, AddedDate); Object obj = SqlHelper.ExecuteScalar(ConnStr, CommandType.StoredProcedure, &quot;uspInsertBatchResponseStatus&quot;, myparams); return true; } catch (Exception ex) { return false; } }</pre> <p><br> &nbsp;</p> <p>&nbsp;</p> <p>How shall I do that in this syntax?</p> 2012-05-07T08:25:15-04:004968672http://forums.asp.net/p/1800838/4968672.aspx/1?Re+How+to+output+ID+no+Re: How to output ID no.? <p>try this way</p> <pre class="prettyprint">Public Sub InsertAccountDetail(CompanyName As String, UserName As String, Password As String, EmailAddress As String, dtCreate As DateTime) Dim arParms() As SqlParameter = New SqlParameter(4) {} arParms(0) = New SqlParameter(&quot;@coName&quot;, SqlDbType.NVarChar, 12) arParms(0).Value = CompanyName arParms(1) = New SqlParameter(&quot;@username&quot;, SqlDbType.NVarChar, 12) arParms(1).Value = UserName arParms(2) = New SqlParameter(&quot;@Password&quot;, SqlDbType.NVarChar, 50) arParms(2).Value = Password arParms(3) = New SqlParameter(&quot;@EmailAddress&quot;, SqlDbType.NVarChar, 60) arParms(3).Value = EmailAddress arParms(4) = New SqlParameter(&quot;@dtCreate&quot;, SqlDbType.DateTime) arParms(4).Value = dtCreate SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, &quot;usp_Account_InsertAccountDetails&quot;, arParms) End Sub</pre> <p><br> <br> </p> <p>thank u</p> <p></p> 2012-05-07T09:04:53-04:004969118http://forums.asp.net/p/1800838/4969118.aspx/1?Re+How+to+output+ID+no+Re: How to output ID no.? <p>I've done what I wanted!!</p> <p>Thanks all those who helped me in this task!&nbsp;</p> 2012-05-07T12:46:10-04:00