title: Identity of recently added record and insert from gridview?
body: <p>I am developing an ASP.Net VB Web Application</p>
  
  <p>The application contains a <code>GridView</code> which displays the records of a user table from my created datable. The database is an Sql server database. </p>
  
  <p>The code below inserts data into one table and through the built in function <code>@@Identity</code> to insert the most recently added record id (<code>tt_id</code>) from the <code>trainingTbl</code> table and inserting that record id into the <code>userAssessmentTbl</code>. Adding the identity to the second <code>userAssessmentTbl</code> table works fine.</p>
  
  <pre><code>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
  </code></pre>
  
  <p>The issue I’m having seems to be centered on how I insert a <code>uniqueidenifier</code> from a <code>GridView</code> into a <code>userAssessmentTbl</code> database! </p>
  
  <p>And how, I guess using a loop I can insert the UserId records from that Gridview (<code>GridView1</code>) into the <code>userAssessmentTbl</code> table along with the looped id from the <code>@@Identity</code>. </p>
  
  <p>This part of the insert parameter seems to be incorrect: </p>
  
  <pre><code>cmd.Parameters.AddWithValue("@UserId", lblUsr2.UniqueID().ToString)
  </code></pre>
  
  <p>And the error I’m met with is: '<em>Conversion failed when converting from a character string to uniqueidentifier.</em>'</p>
  
  <p>I’ve also tried it like this: </p>
  
  <pre><code>cmd.Parameters.AddWithValue("@UserId", SqlDbType.UniqueIdentifier).Value().ToString()
  </code></pre>
  
  <p>And im met with the error: <em>'Operand type clash: int is incompatible with uniqueidentifier'</em></p>
  
  <p><strong>The qusetion has slightly changed to how do I Insert a String into SQL DB Where DataType Is Uniqueidentifier?</strong></p>
  
  <p>Any help will be really appreciated.</p>