We help IT Professionals succeed at work.

Getting RowID after an INSERT using scope_identity()

Leo_Nel
Leo_Nel asked
on
Medium Priority
3,294 Views
Last Modified: 2010-04-21
Hi,
I was wondering if someone could please help me with getting the row id after doing an insert.
what i am trying to get at is, when a user clicks on the New Entry button on the aspx page, it does an insert, and write's back the row id in the div tag. I do the insert on the onclick event in the code behind file. I am able to do the insert, but the value that write's back on the div tag is '0' instead of the row id.
I read up on the direction parameter but am still not able to get it.
I would really appreciate if someone could look at this, and help me understand what i am missing...
Thank you!
--------------
.aspx file
--------------
 
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myDatabaseConnection %>"></asp:SqlDataSource>
<form id="form1" runat="server">
<asp:LinkButton ID="NewEntry" runat="server">New Entry</asp:LinkButton>
<div id="NewEntryID" runat="server"></div
</form>
 
--------------
.aspx.vb file
--------------
 
Protected Sub NewEntry_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles NewEntry.Click
 
	SqlDataSource1.InsertCommand = "INSERT INTO tableLog (UserName) VALUES ('" & Session("UserName") & "'); SELECT SCOPE_IDENTITY() AS NewRowID;"
	SqlDataSource1.InsertCommandType = SqlDataSourceCommandType.Text
	NewEntryID.InnerText = SqlDataSource1.InsertParameters.Add("NewRowID", Data.ParameterDirection.ReturnValue)
	SqlDataSource1.Insert()
	'NewEntryID.InnerText = SqlDataSource1.InsertParameters.Add("NewRowID", Data.ParameterDirection.ReturnValue)
End Sub

Open in new window

Comment
Watch Question

Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
check out this article, will reveil how to do it:
http://davidhayden.com/blog/dave/archive/2006/02/16/2803.aspx

Author

Commented:
Thanks! but, is it possible to get it the way Iam trying to like, is my syntax wrong? Or it's just not possible to get it the way am tryin to? The update statement works and i get the NewRowID when i execute it on SQLServer query analyser.
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
well.... what about this:

Protected Sub NewEntry_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles NewEntry.Click
 
        SqlDataSource1.InsertCommand = "INSERT INTO tableLog (UserName) VALUES (@UserName); SELECT @NewRowID = SCOPE_IDENTITY();"
        SqlDataSource1.InsertCommandType = SqlDataSourceCommandType.Text
        SqlDataSource1.InsertParameters.Add("@UserName", Session("UserName") 
        SqlDataSource1.InsertParameters.Add("@NewRowID").Direction = ParameterDirection.OutPut
 
        SqlDataSource1.Insert()
        NewEntryID.InnerText = SqlDataSource1.InsertParameters("@NewRowID").Value
End Sub

Open in new window

Author

Commented:
that didn't work either, i got syntax errors on some lines
Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
oops....
Protected Sub NewEntry_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles NewEntry.Click
 
        SqlDataSource1.InsertCommand = New SqlCommand("INSERT INTO tableLog (UserName) VALUES (@UserName); SELECT @NewRowID = SCOPE_IDENTITY();", yoursqlconnection)
        SqlDataSource1.InsertCommand.CommandType = CommandType.Text
        SqlDataSource1.InsertCommand.Parameters.Add("@UserName", Session("UserName") 
        SqlDataSource1.InsertCommand.Parameters.Add("@NewRowID").Direction = ParameterDirection.OutPut
 
        SqlDataSource1.Insert()
        NewEntryID.InnerText = SqlDataSource1.InsertCommand.Parameters("@NewRowID").Value
End Sub

Open in new window

Author

Commented:
Thank you!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.