title: Barcode scanner click function in vb.net
body: <p>I'm new vb.net and currently I'm developing a barcode scanner system where user will scan barcode on textbox. If it is a new barcode, it will be inserted into database and shown in the gridview else the recheckIn column will be updated to current date and CheckOut column will empty. The problem is the backend code that I did is OnButtonclick event. How it is possible to perform functions using barcode scanner without button click ? For example, if user click the barcode scanner twice it will show inserted barcode list on a gridview and if three times it will change the color of checkout button and update the checkout column to current date on the gridview. </p>
  
  <pre><code>Imports System.Data
  Imports System.Data.SqlClient
  Imports System.Text
  
  Public Class _Default
      Inherits Page
  
      Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
  
      End Sub
  
      Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  
          If Label1.Visible = True Then
              Label1.Visible = False
          ElseIf Label1.Visible = False Then
              Label1.Visible = True
          End If
  
          If TextBox2.Visible = True Then
              TextBox2.Visible = False
          ElseIf TextBox2.Visible = False
              TextBox2.Visible = True
          End If
  
          Button1.Style.Add(HtmlTextWriterStyle.Color, "green")
      End Sub
  
      Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  
          Dim con As SqlConnection = New SqlConnection("Data Source=EQUALIZER;Initial Catalog=Barcodeapp;Integrated Security=True")
          Dim cmd1 As SqlCommand = New SqlCommand("Select * from barscan", con)
          con.Open()
          If TextBox1.Text = "" Then
              MsgBox("Please fill-up fields!", MsgBoxStyle.Exclamation, "Add LOTID!")
          Else
              Dim theQuery As String = "SELECT Barcode FROM barscan WHERE Barcode=@Barcode"
              Dim cmd2 As SqlCommand = New SqlCommand(theQuery, con)
              cmd2.Parameters.AddWithValue("@Barcode", TextBox1.Text)
  
  
              Using reader As SqlDataReader = cmd2.ExecuteReader()
  
                  If reader.HasRows Then
                      Dim theQuery2 As String = "Update barscan SET RecheckIn=GETDATE() WHERE Barcode=@Barcode"
                      Dim cmd3 As SqlCommand = New SqlCommand(theQuery, con)
                      cmd3.Parameters.AddWithValue("@Barcode", TextBox1.Text)
                  Else
  
                      Dim connectionString As String = "Data Source=EQUALIZER;Initial Catalog=Barcodeapp;Integrated Security=True"
                      Using cn As New SqlConnection(connectionString)
                          cn.Open()
                          Dim cmd As New SqlCommand()
                          cmd.CommandText = "INSERT INTO barscan (Date,Barcode,Location,CheckIn) VALUES(@Date,@Barcode,@Location,@CheckIn)"
  
                          Dim param3 As New SqlParameter()
                          param3.ParameterName = "@Date"
                          param3.Value = Date.Today()
                          cmd.Parameters.Add(param3)
  
                          Dim param1 As New SqlParameter()
                          param1.ParameterName = "@Barcode"
                          param1.Value = TextBox1.Text.Trim()
                          cmd.Parameters.Add(param1)
  
                          Dim param2 As New SqlParameter()
                          param2.ParameterName = "@Location"
                          param2.Value = TextBox2.Text.Trim()
                          cmd.Parameters.Add(param2)
  
                          Dim param4 As New SqlParameter()
                          param4.ParameterName = "@CheckIn"
                          param4.Value = Date.Today()
                          cmd.Parameters.Add(param4)
  
                          cmd.Connection = cn
                          cmd.ExecuteNonQuery()
                          cn.Close()
                      End Using
  
                      If barapp.Visible = True Then
                          barapp.Visible = False
                      ElseIf barapp.Visible = False Then
                          barapp.Visible = True
                      End If
                  End If
              End Using
  
              con.Close()
          End If
      End Sub
  End Class
  </code></pre>
  
  <p><a href="http://i.stack.imgur.com/BWEee.png" rel="nofollow">User Interface</a></p>