Results 1 to 8 of 8

Thread: [RESOLVED] Display @id=scope_identity()"

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2003
    Location
    Newark-on-trent, Nottingham
    Posts
    243

    Resolved [RESOLVED] Display @id=scope_identity()"

    Code:
                Dim SQLData As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("FixedLineProvisioningConnectionString").ToString())
                Dim cmdSelect As New System.Data.SqlClient.SqlCommand("INSERT INTO tblTotalComms_custinfo SET nt_username=@nt_username; SELECT @ID=SCOPE_IDENTITY()", SQLData)
                cmdSelect.Parameters.AddWithValue("@nt_username", Page.User.Identity.Name)
                cmdSelect.Parameters.Add("@ID", System.Data.SqlDbType.Int, 4, "ID").Direction = Data.ParameterDirection.Output
                SQLData.Open()
    
                SQLData.Close()
                w.HeaderText = "Order Number " & Session("OrderNo")
    I want to return the value @ID from the insert statement. Does anybody know how i would achieve this?

  2. #2
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: Display @id=scope_identity()"

    Code:
    ;select scope_identity()
    example:

    Code:
         Dim query As String = "INSERT INTO  tbl_items (item) Values (?);Select scope_identity()"
    
              Dim ID As Integer
            Dim connect As String = "sql connection string"
            Using conn As New SqlConnection(connect)
                Using cmd As New SqlCommand(query, conn)
                    cmd.Parameters.AddWithValue("", YOURVALUE)
                    conn.Open()
                    ID = cmd.ExecuteScalar()
                End Using
            End Using
    i didn't test it but it should work, if not post back
    Last edited by motil; Aug 12th, 2009 at 08:08 AM.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2003
    Location
    Newark-on-trent, Nottingham
    Posts
    243

    Re: Display @id=scope_identity()"

    "Incorrect syntax near the keyword 'SET'."

    Pointing at line... ID = cmd.ExecuteScalar()

  4. #4
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: Display @id=scope_identity()"

    can you post your code ?
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2003
    Location
    Newark-on-trent, Nottingham
    Posts
    243

    Re: Display @id=scope_identity()"

    Code:
                Dim query As String = "INSERT INTO tblTotalComms_custinfo SET nt_username=@nt_username; SELECT SCOPE_IDENTITY()"
                Dim ID As Integer
                Dim connect As String = ConfigurationManager.ConnectionStrings("FixedLineProvisioningConnectionString").ToString()
                Using conn As New System.Data.SqlClient.SqlConnection(connect)
                    Using cmd As New System.Data.SqlClient.SqlCommand(query, conn)
                        cmd.Parameters.AddWithValue("", ID)
                        conn.Open()
                        ID = cmd.ExecuteScalar()
                    End Using
                End Using

  6. #6
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: Display @id=scope_identity()"

    Code:
     cmd.Parameters.AddWithValue("@nt_username", UserNameVar)
    replace the UserNameVar with the DIM that contains the user name.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  7. #7
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: Display @id=scope_identity()"

    one sec..
    you have the whole statement wrong

    your new Statement:

    Code:
        Dim query As String = "INSERT INTO tblTotalComms_custinfo (nt_username) VALUES(@nt_username); SELECT SCOPE_IDENTITY()"
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Oct 2003
    Location
    Newark-on-trent, Nottingham
    Posts
    243

    Re: Display @id=scope_identity()"

    Brilliant thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width