每次我从数据库中读取时,似乎都无法找到此错误。错误显示,但我的程序仍然正常运行,这很奇怪。我尝试过使用断点但错误在表单加载事件之前弹出。这是我的代码。
Private Sub DataGridView1_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
fileID = 0
If e.RowIndex >= 0 Then
Dim row As New DataGridViewRow
row = Me.DataGridView1.Rows(e.RowIndex)
fileID = row.Cells("ID").Value.ToString
KUP_DATABASE_REPORT.ShowDialog() 'Error pop out on this line
End If
End Sub
上面的代码来自主窗体。当我单击单元格时,新表单将显示数据库中的所有数据。下面的代码是第二种形式,它从中提取数据并填充空文本框等。
通过使用断点,行KUP_DATABASE_REPORT.ShowDialog()
Private Sub KUP_DATABASE_REPORT_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DateTimePicker1.MinDate = DateTime.Now()
DateTimePicker1.CustomFormat = "dddd dd MMMM yyyy"
NumericUpDown1.ResetText()
NumericUpDown2.ResetText()
LoadFileDataFromRead()
End Sub
Private Sub LoadFileDataFromRead()
Try
Connect2Database()
theID = New Integer = 0
theID = KUP_DATABASE_READ.fileID
myCommand.CommandText = "Select * from kup_table Where ID = @theIDD"
myCommand.Parameters.AddWithValue("@theIDD", theID)
reader = myCommand.ExecuteReader()
While reader.Read
'======================FILE INFO=============================
NumericUpDown1.Controls.Item(1).Text = reader.GetInt32("FILE_NO")
NumericUpDown2.Controls.Item(1).Text = reader.GetInt32("FILE_YEAR")
TextBox1.Text = reader.GetString("FILE_TYPE")
TextBox2.Text = reader.GetString("FILE_COLOR")
TextBox3.Text = reader.GetString("FILE_DESC")
'======================LOT INFO==============================
ComboBox1.Text = reader.GetString("DAERAH")
ComboBox2.Text = reader.GetString("MUKIM")
ComboBox3.Text = reader.GetString("SEKSYEN")
TextBox4.Text = reader.GetString("LOT")
ComboBox4.Text = reader.GetString("LOT_INFO")
TextBox5.Text = reader.GetString("TITLE_NO")
'======================CLIENT INFO===========================
TextBox6.Text = reader.GetString("NAME")
TextBox7.Text = reader.GetString("ADDR")
TextBox8.Text = reader.GetString("ADDR2")
TextBox9.Text = reader.GetString("TEL_NUM")
TextBox10.Text = reader.GetString("FAX_NUM")
'======================STATUS================================
RichTextBox1.Text = reader.GetString("STATUS")
'======================LEVEL/PROCESS=========================
If reader.GetInt32("TECH_DEPT") = 0 Then
CheckBox1.Checked = False
Else
CheckBox1.Checked = True
End If
If reader.GetInt32("T_JKR") = 0 Then
CheckBox8.Checked = False
Else
CheckBox8.Checked = True
End If
If reader.GetInt32("T_JPS") = 0 Then
CheckBox9.Checked = False
Else
CheckBox9.Checked = True
End If
If reader.GetInt32("T_BKSA") = 0 Then
CheckBox10.Checked = False
Else
CheckBox10.Checked = True
End If
If reader.GetInt32("T_JAS") = 0 Then
CheckBox11.Checked = False
Else
CheckBox11.Checked = True
End If
If reader.GetInt32("T_PERTANIAN") = 0 Then
CheckBox12.Checked = False
Else
CheckBox12.Checked = True
End If
If reader.GetInt32("T_MAJLIS") = 0 Then
CheckBox13.Checked = False
Else
CheckBox13.Checked = True
End If
If reader.GetInt32("T_JPBD") = 0 Then
CheckBox14.Checked = False
Else
CheckBox14.Checked = True
End If
If reader.GetInt32("T_KESIHATAN") = 0 Then
CheckBox15.Checked = False
Else
CheckBox15.Checked = True
End If
If reader.GetInt32("T_OTHERS") = 0 Then
CheckBox16.Checked = False
TextBox11.ResetText()
TextBox11.Enabled = False
Else
CheckBox16.Checked = True
TextBox11.Enabled = True
TextBox11.Text = reader.GetString("T_OTHERS_TXT")
End If
'===========================DONE TECH DEPARTMENT====================
If reader.GetInt32("PEJABAT_TANAH") = 0 Then
CheckBox2.Checked = False
Else
CheckBox2.Checked = True
End If
If reader.GetInt32("ULASAN_YB") = 0 Then
CheckBox3.Checked = False
Else
CheckBox3.Checked = True
End If
If reader.GetInt32("JKBB_APPROVED") = 0 Then
CheckBox4.Checked = False
Else
CheckBox4.Checked = True
End If
If reader.GetInt32("LAYOUT_ENDOSED") = 0 Then
CheckBox5.Checked = False
Else
CheckBox5.Checked = True
End If
If reader.GetInt32("5A_NOTICE") = 0 Then
CheckBox6.Checked = False
Else
CheckBox6.Checked = True
End If
If reader.GetInt32("QT_ISSURANCE") = 0 Then
CheckBox7.Checked = False
Else
CheckBox7.Checked = True
End If
'====================CHECKBOX DONE NOW IS THE TEXT=================
TextBox12.Text = reader.GetString("TECH_DEPT_TXT")
TextBox13.Text = reader.GetString("PEJABAT_TANAH_TXT")
TextBox14.Text = reader.GetString("ULASAN_YB_TXT")
TextBox15.Text = reader.GetString("JKBB_APPROVED_TXT")
TextBox16.Text = reader.GetString("LAYOUT_ENDOSED_TXT")
TextBox17.Text = reader.GetString("5A_NOTICE_TXT")
TextBox18.Text = reader.GetString("QT_ISSURANCE_TXT")
If reader.GetString("REMINDER_STATUS") Then
RadioButton1.Checked = True
Else
RadioButton2.Checked = True
End If
End While
reader.Dispose()
sqlConn.Close()
Catch ex As MySqlException
MsgBox(ex.ToString)
If sqlConn.State = ConnectionState.Open Then
sqlConn.Close()
End If
Finally
sqlConn.Dispose()
End Try
End Sub
更新 我通过慢慢剥离大部分代码找到了罪魁祸首。错误发生在这个事件上。
Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
If RadioButton2.Checked Then
Connect2Database()
Try
myCommand.CommandText = "Update kup_table SET REMINDER_STATUS = 0 Where ID = @theIDD"
myCommand.ExecuteNonQuery()
sqlConn.Close()
MsgBox("The reminder has been switched off successfully.", MsgBoxStyle.Information)
Catch ex As MySqlException
MsgBox(ex.Message)
Finally
sqlConn.Dispose()
End Try
End If
End Sub
我不确定为什么会发出致命错误,但当我对此事件进行评论时,它不再显示任何致命错误。如果您有任何人知道答案,请发一个,以便我可以表示感谢
答案 0 :(得分:0)
对不起这里的愚蠢问题。我发现了原因。这是因为我还没有在执行之前对查询进行参数化...
Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
If RadioButton2.Checked Then
Connect2Database()
Try
myCommand.CommandText = "Update kup_table SET REMINDER_STATUS = 0 Where ID = @theIDD"
myCommand.ExecuteNonQuery()
sqlConn.Close()
MsgBox("The reminder has been switched off successfully.", MsgBoxStyle.Information)
Catch ex As MySqlException
MsgBox(ex.Message)
Finally
sqlConn.Dispose()
End Try
End If
End Sub
非常抱歉,非常感谢那些试图提供帮助的人。我错过了这一行。
myCommand.Parameters.AddWithValue("@theIDD", theID)