我想出了if else语句处理多个try catch语句的代码。它在第一个if语句中工作,但在else if语句中它已经不起作用了。 你能帮助我吗??? 这是代码
If cmbxyear.Text = "NURSERY" Then
MySqlConnection = New MySqlConnection
MySqlConnection.ConnectionString = "server = localhost; port=3307; user id = root; password = 1234; database = mcs;"
Dim READER As MySqlDataReader
lblyear.Text = "N"
Try
MySqlConnection.Open()
Dim query As String
query = " insert into mcs.nursery (last_name, first_name, middle_name, year_grade, school_year) Values ('" & txtlastname.Text & "','" & txtfirstname.Text & "','" & txtmiddlename.Text & "','" & cmbxyear.Text & "','" & cmbxschoolyear.Text & "') "
Dim Command As New MySqlCommand(query, MySqlConnection)
READER = Command.ExecuteReader
MessageBox.Show("Student Successfully Registered")
MySqlConnection.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
MySqlConnection.Dispose()
End Try
Try
MySqlConnection.Open()
Dim query As String
query = " select id from mcs.nursery where last_name = '" & txtlastname.Text & "' and first_name = '" & txtfirstname.Text & "' and middle_name = '" & txtmiddlename.Text & "'"
Dim Command As New MySqlCommand(query, MySqlConnection)
READER = Command.ExecuteReader
READER.Read()
lblno.Text = READER(0)
MessageBox.Show("Student Number Generated")
MySqlConnection.Close()
Dim lbl1 As String = lblyear.Text
Dim lbl2 As String = lblno.Text
lblstudentno.Text = (String.Concat(lblyear.Text, "-", lblno.Text))
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
MySqlConnection.Dispose()
End Try
Try
MySqlConnection.Open()
Dim query2 As String
query2 = "update mcs.nursery set student_no = '" & lblstudentno.Text & "' where last_name = '" & txtlastname.Text & "'and first_name = '" & txtfirstname.Text & "' and middle_name = '" & txtmiddlename.Text & "' "
Dim Command As New MySqlCommand(query2, MySqlConnection)
READER = Command.ExecuteReader
MySqlConnection.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
MySqlConnection.Dispose()
End Try
ElseIf cmbxyear.Text = "KINDER ONE" Then
MySqlConnection = New MySqlConnection
MySqlConnection.ConnectionString = "server = localhost; port=3307; user id = root; password = 1234; database = mcs;"
Dim READER As MySqlDataReader
lblyear.Text = "K1"
Try
MySqlConnection.Open()
Dim query As String
query = " insert into mcs.kinder_one (last_name, first_name, middle_name, year_grade, school_year) Values ('" & txtlastname.Text & "','" & txtfirstname.Text & "','" & txtmiddlename.Text & "','" & cmbxyear.Text & "','" & cmbxschoolyear.Text & "') "
Dim Command As New MySqlCommand(query, MySqlConnection)
READER = Command.ExecuteReader
MessageBox.Show("Student Successfully Registered")
MySqlConnection.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
MySqlConnection.Dispose()
End Try
Try
MySqlConnection.Open()
Dim query As String
query = " select id from mcs.kinder_one where last_name = '" & txtlastname.Text & "' and first_name = '" & txtfirstname.Text & "' and middle_name = '" & txtmiddlename.Text & "'"
Dim Command As New MySqlCommand(query, MySqlConnection)
READER = Command.ExecuteReader
READER.Read()
lblno.Text = READER(0)
MessageBox.Show("Student Number Generated")
MySqlConnection.Close()
Dim lbl1 As String = lblyear.Text
Dim lbl2 As String = lblno.Text
lblstudentno.Text = (String.Concat(lblyear.Text, "-", lblno.Text))
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
MySqlConnection.Dispose()
End Try
Try
MySqlConnection.Open()
Dim query2 As String
query2 = "update mcs.kinder_one set student_no = '" & lblstudentno.Text & "' where last_name = '" & txtlastname.Text & "'and first_name = '" & txtfirstname.Text & "' and middle_name = '" & txtmiddlename.Text & "' "
Dim Command As New MySqlCommand(query2, MySqlConnection)
READER = Command.ExecuteReader
MySqlConnection.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
MySqlConnection.Dispose()
End Try
ElseIf cmbxyear.Text = "KINDER TWO" Then
MySqlConnection = New MySqlConnection
MySqlConnection.ConnectionString = "server = localhost; port=3307; user id = root; password = 1234; database = mcs;"
Dim READER As MySqlDataReader
lblyear.Text = "K2"
Try
MySqlConnection.Open()
Dim query As String
query = " insert into mcs.kinder_two (last_name, first_name, middle_name, year_grade, school_year) Values ('" & txtlastname.Text & "','" & txtfirstname.Text & "','" & txtmiddlename.Text & "','" & cmbxyear.Text & "','" & cmbxschoolyear.Text & "') "
Dim Command As New MySqlCommand(query, MySqlConnection)
READER = Command.ExecuteReader
MessageBox.Show("Student Successfully Registered")
MySqlConnection.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
MySqlConnection.Dispose()
End Try
Try
MySqlConnection.Open()
Dim query As String
query = " select id from mcs.kinder_two where last_name = '" & txtlastname.Text & "' and first_name = '" & txtfirstname.Text & "' and middle_name = '" & txtmiddlename.Text & "'"
Dim Command As New MySqlCommand(query, MySqlConnection)
READER = Command.ExecuteReader
READER.Read()
lblno.Text = READER(0)
MessageBox.Show("Student Number Generated")
MySqlConnection.Close()
Dim lbl1 As String = lblyear.Text
Dim lbl2 As String = lblno.Text
lblstudentno.Text = (String.Concat(lblyear.Text, "-", lblno.Text))
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
MySqlConnection.Dispose()
End Try
Try
MySqlConnection.Open()
Dim query2 As String
query2 = "update mcs.kinder_two set student_no = '" & lblstudentno.Text & "' where last_name = '" & txtlastname.Text & "'and first_name = '" & txtfirstname.Text & "' and middle_name = '" & txtmiddlename.Text & "' "
Dim Command As New MySqlCommand(query2, MySqlConnection)
READER = Command.ExecuteReader
MySqlConnection.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
MySqlConnection.Dispose()
End Try