title: How to get the primary key value while executing the query?
body: <p>This is the Login Button code and <code>EditUser()</code> function:</p>
  
  <pre><code>Private Sub BtnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
  
          If (isformvalid()) Then
              qr = "Select * from userlogin where UserName='" &amp; txtUser.Text &amp; "' and Password='" &amp; txtPassword.Text &amp; "' and UserType ='" &amp; ComboBox1.Text &amp; "' "
  
              ds = searchdata(qr)
              If (ds.Tables(0).Rows.Count &gt; 0) Then
                  LoginUser = txtUser.Text
                  LoginPass = txtPassword.Text
                  UserType = ComboBox1.Text
  
                  EditUser()
                  Dashbord.Show()
                  Me.Close()
              Else
                  MsgBox("UserName or Password is Incorrect. Please Cheak And Try Again")
                  txtUser.ResetText()
                  txtPassword.ResetText()
  
              End If
          End If
  End Sub
  
  Private Function EditUser()
          If (isformvalid()) Then
              q = "Insert into UserLoginAudit (UserName, Password, UserType, ActivityLogin) Values('" &amp; txtUser.Text &amp; "','" &amp; txtPassword.Text &amp; "','" &amp; ComboBox1.Text &amp; "','" &amp; DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") &amp; "')"
              ProID = lastProID(q)
          End If
  End Function
  </code></pre>
  
  <p>This is the module code:</p>
  
  <pre><code>Module DBSQLServer
      Public con As New SqlConnection("Data Source=JOYALXDESKTOP\SQLEXPRESS;Initial Catalog=SaleInventory;Integrated Security=True")
      Public cmd As New SqlCommand
      Public da As New SqlDataAdapter
      Public ds As New DataSet
      Public dt As DataTable
      Public qr As String
      Public i As Integer
      Public newProID As Integer
  
      Public Function searchdata(ByVal qr As String) As DataSet
          da = New SqlDataAdapter(qr, con)
          ds = New DataSet
          da.Fill(ds)
          Return ds
  
      End Function
  
      Public Function insertdata(ByVal qr As String) As Integer
  
          cmd = New SqlCommand(qr, con)
          con.Open()
          i = cmd.ExecuteNonQuery()
          con.Close()
          Return i
  
      End Function
  
      Public Function lastProID(ByVal qr As String) As Integer
  
          cmd = New SqlCommand(qr, con)
          con.Open()
          newProID = cmd.ExecuteScalar
          con.Close()
          Return newProID
  
      End Function
  End Module
  </code></pre>
  
  <p>And this is my Logout code and <code>EditUser()</code> function:</p>
  
  <pre><code>Private Sub Label1_Click_1(sender As Object, e As EventArgs) Handles Label1.Click
          Application.Exit()
          EditUser()
  End Sub
  
      Private Function EditUser()
          q0 = "UPDATE UserLoginAudit SET ActivityOffline = '" &amp; DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") &amp; "' WHERE AuditID = '" &amp; My.Forms.login.ProID &amp; "';"
          Dim logincorrect As Boolean = Convert.ToBoolean(insertdata(q0))
          If (logincorrect) Then
              MsgBox("LogOut Successful ...", MsgBoxStyle.Information)
          Else
              MsgBox("Something Wrong. LogOut Failed. Please Check and Try Again...", MsgBoxStyle.Critical)
          End If
  End Function
  </code></pre>
  
  <p>When I run the program, after clicking the login button the data (UserName, Password, UserType, ActivityLogin) is inserted perfectly.</p>
  
  <p>But when I click the Logout button in the database <code>ActivityOffline</code> column value remains null.</p>
  
  <p>Then I create a Form Load event to check the value of ProID in the main form where I created the logout button:</p>
  
  <pre><code>Private Sub Dashbord_Load(sender As Object, e As EventArgs) Handles MyBase.Load
          hi.Text = My.Forms.login.ProID
  End Sub
  </code></pre>
  
  <p>It always shows 0</p>