Getting RowID after an INSERT using scope_identity()

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

Leo_NelAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
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

0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
check out this article, will reveil how to do it:
http://davidhayden.com/blog/dave/archive/2006/02/16/2803.aspx
0
 
Leo_NelAuthor 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.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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

0
 
Leo_NelAuthor Commented:
that didn't work either, i got syntax errors on some lines
0
 
Leo_NelAuthor Commented:
Thank you!
0
All Courses

From novice to tech pro — start learning today.