【发布时间】:2018-01-20 01:08:22
【问题描述】:
If timeCounter = 1 And stat = "ACTIVE" Then
stat_in = "IN"
Using SQLConnection As New MySqlConnection(ServerString)
Using command As New MySqlCommand()
With command
.CommandText = "INSERT into time_table(id,student_id,tag,date,time,status) VALUES(@id,@student_id,@tag,@date,@time,@status); UPDATE rfid_stud SET timeCounter = 2 WHERE tag = '" & txtReceived.Text & "'"
.Connection = SQLConnection
.CommandType = CommandType.Text
.Parameters.AddWithValue("@id", idnum)
.Parameters.AddWithValue("@student_id", txtStudNo.Text)
.Parameters.AddWithValue("@tag", txtReceived.Text)
.Parameters.AddWithValue("@date", txtDate.Text)
.Parameters.AddWithValue("@time", txtTime.Text)
.Parameters.AddWithValue("@status", stat_in)
End With
Try
SQLConnection.Open()
command.ExecuteNonQuery()
Catch ex As Exception
Finally
SQLConnection.Close()
End Try
End Using
End Using
ElseIf timeCounter > 1 And stat = "ACTIVE" Then
stat_in = "OUT"
Using SQLConnection As New MySqlConnection(ServerString)
Using command As New MySqlCommand()
With command
.CommandText = "INSERT into time_table(id,student_id,tag,date,time,status) VALUES(@id,@student_id,@tag,@date,@time,@status); UPDATE rfid_stud SET timeCounter = 1 WHERE tag = '" & txtReceived.Text & "'"
.Connection = SQLConnection
.CommandType = CommandType.Text
.Parameters.AddWithValue("@id", idnum)
.Parameters.AddWithValue("@student_id", txtStudNo.Text)
.Parameters.AddWithValue("@tag", txtReceived.Text)
.Parameters.AddWithValue("@date", txtDate.Text)
.Parameters.AddWithValue("@time", txtTime.Text)
.Parameters.AddWithValue("@status", stat_in)
End With
Try
SQLConnection.Open()
command.ExecuteNonQuery()
Catch ex As Exception
Finally
SQLConnection.Close()
End Try
End Using
End Using
Else
MsgBox("RFID Card is INACTIVE! Please Contact System Administrator for assisstance.", vbExclamation, "Warning!")
End If
For i = 0 To (grid.Rows.Count - 1)
stringfix = grid.Rows.Item(i).Cells(0).Value
string1 = Microsoft.VisualBasic.Mid(stringfix, 1, 12)
string2 = Microsoft.VisualBasic.Mid(stringfix, 2, 12)
If string1 = string2 Then
newtag = False
Exit For
Else
newtag = True
End If
Next
If newtag = True And stat = "ACTIVE" Then
Dim dr As Integer
dr = grid.Rows.Add()
grid.Rows.Item(dr).Cells.Item(0).Value = txtReceived.Text
grid.Rows.Item(dr).Cells(1).Value = txtStudNo.Text
grid.Rows.Item(dr).Cells(2).Value = txtFname.Text
grid.Rows.Item(dr).Cells(3).Value = txtLname.Text
grid.Rows.Item(dr).Cells.Item(4).Value = txtDate.Text + " at " + txtTime.Text
If stat_in = "IN" Then
grid.Rows.Item(dr).Cells(5).Style.BackColor = Color.Green
grid.Rows.Item(dr).Cells(5).Style.ForeColor = Color.White
grid.Rows.Item(dr).Cells.Item(5).Value = stat_in
ElseIf stat_in = "OUT" Then
grid.Rows.Item(dr).Cells(5).Style.BackColor = Color.Red
grid.Rows.Item(dr).Cells(5).Style.ForeColor = Color.White
grid.Rows.Item(dr).Cells.Item(5).Value = stat_in
End If
End If
我有一个关于“如果 stat_in = IN”的警告。 主要警告是“stat_in” 相同的代码在我的电脑上运行而没有警告,但是当我将系统传输到笔记本电脑时,它显示 2 个警告。 该程序在我的电脑上完美运行。 警告是 警告 BC42104 变量“stat_in”在被赋值之前被使用。在运行时可能会导致空引用异常,另一个是变量“stat_in”在被赋值之前被使用。运行时可能会导致空异常。 台式机 = Windows 10,笔记本电脑 = Windows,7。 我还在 Windows 10 笔记本电脑上进行了尝试。它也不起作用。 相同版本的 Visual Studio。 Visual Studio 2015 社区。 我在考虑框架?请帮我。我是 vb.net 编码的初学者。
【问题讨论】:
-
你有一个
If和ElseIf,所以可能这两个条件都不适用,然后变量保持未初始化状态。要么添加Else...以分配回退值,要么首先给它一个值。在这种情况下,我猜你只想从Else中的方法中Return -
请阅读How to Ask 并使用tour。还要根据需要使用标点符号、句子和段落,以使帖子可读。你应该想让别人帮助你容易
标签: vb.net visual-studio if-statement