我是新的vb.net,目前我正在開發一個條碼掃描器系統,用戶將在其上掃描文本框上的條形碼。如果它是新的條形碼,它將被插入數據庫並顯示在gridview中,否則recheckIn列將更新爲當前日期,並且CheckOut列將清空。問題是我做的後端代碼是OnButtonclick事件。無需點擊按鈕,如何使用條碼掃描器執行功能?例如,如果用戶點擊兩次條形碼掃描器,它會在網格視圖中顯示插入的條形碼列表,如果三次它將更改結帳按鈕的顏色並將結帳列更新爲gridview上的當前日期。vb.net中的條碼掃描器點擊功能
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 [email protected]"
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 [email protected]"
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