SCOPE_IDENTITY issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Barno77
    New Member
    • Oct 2007
    • 22

    SCOPE_IDENTITY issue

    I want to return the value of Identity Key which in this case is an AutoNumber field in my table and place the value of that field of the newly added record into a textbox. Here's what I got so far, my insert works, but no return as of yet:

    Protected Sub Button2_Click(B yVal sender As Object, ByVal e As System.EventArg s) Handles Button2.Click
    Dim sql As New SqlCommand
    'Dim myconn As Data.SqlClient. SqlConnection
    Dim mycmd As Data.SqlClient. SqlCommand
    Dim ResultReader As SqlDataReader
    Dim result As String
    Dim mySqlConnection As SqlConnection = New SqlConnection(C onfigurationMan ager.Connection Strings("MAS_XC MConnectionStri ng").Connection String)

    mySqlConnection .Open()
    result = ""
    ResultReader = Nothing

    mycmd = New SqlCommand("SEL ECT * FROM CA_IT_STORYBOOK ", mySqlConnection )

    mycmd.CommandTe xt = "INSERT INTO CA_IT_STORYBOOK (SalesOrderNumb er, InvoiceNumber, StoryBook) VALUES (012,334,'NewTe st3');Select @OutVar = SCOPE_IDENTITY( )"
    mycmd.ExecuteNo nQuery()
    ' mycmd.ExecuteSc alar()


    If ResultReader.Ha sRows Then
    result = result & vbCrLf & ResultReader.It em(0).ToString( )

    TextBox1.Text = result
    End If

    mySqlConnection .Close()
    End Sub
  • deric
    New Member
    • Dec 2007
    • 92

    #2
    I think it's because "Select @OutVar = SCOPE_IDENTITY( )" is just an assignment statement. If you really need that statement there, select @OutVar again to get its value. If not, you can select SCOPE_IDENTITY( ) directly instead.

    You can do it like this:
    Code:
    mycmd.CommandText = "INSERT INTO CA_IT_STORYBOOK(SalesOrderNumber, InvoiceNumber, StoryBook) VALUES (012,334,'NewTest3');Select @@IDENTITY"
    Visit this to know more about it.

    Also, is your TextBox1 a multiline control? You are using a carriage return (vbCrLf), try to take that away if your textbox is not a multiline.

    Comment

    • ganeshkumar08
      New Member
      • Jan 2008
      • 31

      #3
      The main difference between @@SCOPE_IDENTIT Y and @@IDENTITY is

      @@SCOPE_IDENTIT Y - Gets identity value for the last executed SQL statement with in the scope. Its a local variable
      @@IDENTITY - Its a global variable. For the last executed statement if you have any trigger/function then after that stmt executed, it returns that stmts executed identity value.

      Based on your requirement you use those varibles

      Comment

      • Barno77
        New Member
        • Oct 2007
        • 22

        #4
        Originally posted by ganeshkumar08
        The main difference between @@SCOPE_IDENTIT Y and @@IDENTITY is

        @@SCOPE_IDENTIT Y - Gets identity value for the last executed SQL statement with in the scope. Its a local variable
        @@IDENTITY - Its a global variable. For the last executed statement if you have any trigger/function then after that stmt executed, it returns that stmts executed identity value.

        Based on your requirement you use those varibles
        Thanks, how can I actually show the value of the Identity Id in a textbox?

        Comment

        • ck9663
          Recognized Expert Specialist
          • Jun 2007
          • 2878

          #5
          This would depend on your front-end tool that you used.

          -- CK

          Comment

          • Barno77
            New Member
            • Oct 2007
            • 22

            #6
            Originally posted by ck9663
            This would depend on your front-end tool that you used.

            -- CK
            I just have a regular asp page with a button and a textbox. When I hit the button I want my record is being inserted into the table, but then I want to retrieve the Identity Id and show it in the textbox.

            Comment

            • ck9663
              Recognized Expert Specialist
              • Jun 2007
              • 2878

              #7
              First, make sure your stored proc/function is already returning what you want. On your front-end side, create a recordset object. Execute your module. It will return the value as an object of the recordset (column). You can store that value as value property of your textbox object inside your form.

              -- CK

              Comment

              Working...