user1334338
user1334338

Reputation:

Identity of recently added record and insert from gridview?

I am developing an ASP.Net VB Web Application

The application contains a GridView which displays the records of a user table from my created datable. The database is an Sql server database.

The code below inserts data into one table and through the built in function @@Identity to insert the most recently added record id (tt_id) from the trainingTbl table and inserting that record id into the userAssessmentTbl. Adding the identity to the second userAssessmentTbl table works fine.

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lblUsr2 As Control = FindControlRecursive(MultiTabs, "txtUsr")
        Dim strQuery As String
        Dim cmd As SqlCommand

        strQuery = "Insert into trainingTbl(s_id, t_date, EditorId, wa_id, st_id) values(@s_id , @t_date, @EditorId, @wa_id, @st_id  ) Insert into userAssessmentTbl(tt_id, UserId) values(@@Identity, @UserId)"

        cmd = New SqlCommand(strQuery)

        cmd.Parameters.AddWithValue("@s_id", DDLCourse.Text)
        cmd.Parameters.AddWithValue("@t_date", Convert.ToDateTime(txtDate.Text))
        cmd.Parameters.AddWithValue("@EditorId", User.Identity.Name.ToString())
        cmd.Parameters.AddWithValue("@st_id", myLblStation.Value().ToString)
        cmd.Parameters.AddWithValue("@wa_id", myLblWatch.Value().ToString)
        cmd.Parameters.AddWithValue("@UserId", lblUsr2.UniqueID.ToString)
        InsertUpdateData(cmd)
    End Sub

The issue I’m having seems to be centered on how I insert a uniqueidenifier from a GridView into a userAssessmentTbl database!

And how, I guess using a loop I can insert the UserId records from that Gridview (GridView1) into the userAssessmentTbl table along with the looped id from the @@Identity.

This part of the insert parameter seems to be incorrect:

cmd.Parameters.AddWithValue("@UserId", lblUsr2.UniqueID().ToString)

And the error I’m met with is: 'Conversion failed when converting from a character string to uniqueidentifier.'

I’ve also tried it like this:

cmd.Parameters.AddWithValue("@UserId", SqlDbType.UniqueIdentifier).Value().ToString()

And im met with the error: 'Operand type clash: int is incompatible with uniqueidentifier'

The qusetion has slightly changed to how do I Insert a String into SQL DB Where DataType Is Uniqueidentifier?

Any help will be really appreciated.

Upvotes: 1

Views: 585

Answers (1)

David East
David East

Reputation: 32604

Well first of all:

@@IDENTITY returns the most recently created identity for your current connection, not necessarily the identity for the recently added row in a table. Always use SCOPE_IDENTITY() to return the identity of the recently added row.


Secondly, to asnwer your question:

The SQL type Uniqueidentifier and the CLR type Guid match up.

So instead of passing "@UserId" in as a parameter you need to create a Guid out of the string value.

Dim userID As Guid = New Guid(lblUsr2.UniqueID.ToString)

Upvotes: 1

Related Questions