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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Leo_NelAuthor Commented:
that didn't work either, i got syntax errors on some lines
0
Guy Hengel [angelIII / a3]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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Leo_NelAuthor Commented:
Thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.