Tuesday, February 21, 2012

SCOPE_IDENTITY Problem I dont know how to use it.

Hi,

I am using following code to insert some record in to database and after that i want the id of new added record , so what kind of change i have to with my code plz anyone can do some change my code as it return id by using SCOPE_IDENTITY in my code

--------------------
Dim strconn As String = "server=xxx.xxx.xx; initial catalog=xxx;uid=xxx;pwd=xxx"
'Create a connection
Dim MyConn_member As New SqlConnection(strconn)
MyConn_member.Open()

'Start the transaction
Dim myTrans As SqlTransaction = MyConn_member.BeginTransaction()

Try
'Specify the first statement to run...
Dim MySQL_member As String = "Insert Into article ([articleCategoryId],[articleTitle],[articleDescription],[articleContent],[articlePostBy],[articleStatus],[addDate],[lastUpdate]) Values (@.category_id, @.article_title, @.article_description,@.article_content,@.article_postby,@.article_status,@.add_date, @.last_update)"

'Create the SqlCommand object, specifying the transaction through
Dim cmd_member As New SqlCommand(MySQL_member, MyConn_member, myTrans)
cmd_member.Parameters.Add(New SqlParameter("@.category_id", article_category.SelectedValue))
cmd_member.Parameters.Add(New SqlParameter("@.article_title", article_title.Text))
cmd_member.Parameters.Add(New SqlParameter("@.article_description", article_description.Text))
cmd_member.Parameters.Add(New SqlParameter("@.article_content", article_content.Text))
cmd_member.Parameters.Add(New SqlParameter("@.article_postby", post_by))
cmd_member.Parameters.Add(New SqlParameter("@.article_status", article_status))
cmd_member.Parameters.Add(New SqlParameter("@.add_date", last_update))
cmd_member.Parameters.Add(New SqlParameter("@.last_update", last_update))

cmd_member.ExecuteNonQuery()
myTrans.Commit()

Catch ex As Exception
'Something went wrong, so rollback the transaction
myTrans.Rollback()
MyConn_member.Close()
Throw 'Bubble up the exception
Finally
'Finally, close the connection
MyConn_member.Close()
End Try

--------------------

PLease help me
Thanks in advance

Create an additional parameter, set the parameter direction to Output.

Append this to the end of your SQL statement:

SELECT <Your Parameter Name> = SCOPE_IDENTITY;

Then read the vale of the output parameter.

|||Sorry , But I really don't understand where and how to append it

Can you please do for me...

Very Very Thanks in Advance|||

Kunal Mehta:

Sorry , But I really don't understand where and how to append it

Can you please do for me...

We're not here to do your work for you. The previous poster has demonstrated what you need to do. If you have a question that is more specific to an issue you are having vs. please do my work for me, then feel free to resubmit your question.

|||

I would be very happy to do it for you. Please send me a contract for employment. I will charge you $200.00 US per hour, minimum of 1 hour.

Maybe you could send me your client's contact information and I will do the whole project for you?

No comments:

Post a Comment