08-30-2012, 06:18 AM
Hey bud,
I'm running into an exception with the following code. The exception is below and this happens after the first part of the IF statement is evaluated and an existing record is found in the database. When I click the OK button to close the messagebox - that's when the exception is thrown.
VB then points to the following line:
[code2=vbnet]While dbData.Read()[/code2]
HERE'S THE EXCEPTION:
HERE'S MY SOLUTION:
I decided to remove the dbData.close() line just before the Else statement and the exception is no longer being thrown. Is this fine or does this create another issue that I'm unaware of?
HERE'S MY CODE:
[code2=vbnet]dbConn = New MySqlConnection("Server=" & FormLogin.ComboBoxServerIP.SelectedItem & ";Port=3306;Uid=parts;Password=parts;Database=accounting")
Dim account As Boolean = True
If dbConn.State = ConnectionState.Open Then
dbConn.Close()
End If
dbConn.Open()
Dim dbQuery As String = "SELECT * FROM customer WHERE accountNumber = '" & TextBoxAccount.Text & "';"
Dim dbData As MySqlDataReader
Dim dbAdapter As New MySqlDataAdapter
Dim dbCmd As New MySqlCommand
dbCmd.CommandText = dbQuery
dbCmd.Connection = dbConn
dbAdapter.SelectCommand = dbCmd
dbData = dbCmd.ExecuteReader
While dbData.Read()
If dbData.HasRows() = True Then
MessageBox.Show("Customer record already exists!")
account = False
dbData.Close()
Else
dbData.Close()
account = True
End If
End While
dbData.Close()
If account = True Then
Dim dbQuery2 As String = "INSERT INTO customer (accountNumber, nameLAST, nameFIRST, nameCOMPANY, addressSTREET, addressSTREET1," & _
"addressCITY, addressSTATE, addressZIPCODE, phone, fax, email)" & _
"VALUES('" & TextBoxAccount.Text & "','" & TextBoxLastName.Text & "','" & TextBoxFirstName.Text & "','" & TextBoxCompanyName.Text & _
"','" & TextBoxAddress1.Text & "','" & TextBoxAddress2.Text & "','" & TextBoxCity.Text & "','" & ComboBoxState.SelectedItem & _
"','" & MaskedTextBoxZip.Text & "','" & MaskedTextBoxPhone.Text & "','" & MaskedTextBoxFax.Text & "','" & TextBoxEmail.Text & "')"
Dim dbData2 As MySqlDataReader
Dim dbAdapter2 As New MySqlDataAdapter
Dim dbCmd2 As New MySqlCommand
dbCmd2.CommandText = dbQuery2
dbCmd2.Connection = dbConn
dbAdapter2.SelectCommand = dbCmd2
dbData2 = dbCmd2.ExecuteReader
Call lockForm()
MessageBox.Show("Customer account record saved successfully!")
End If[/code2]
Your help would be greatly appreciated, thanks.
I'm running into an exception with the following code. The exception is below and this happens after the first part of the IF statement is evaluated and an existing record is found in the database. When I click the OK button to close the messagebox - that's when the exception is thrown.
VB then points to the following line:
[code2=vbnet]While dbData.Read()[/code2]
HERE'S THE EXCEPTION:
Quote:Invalid attempt to Read when reader is closed
HERE'S MY SOLUTION:
I decided to remove the dbData.close() line just before the Else statement and the exception is no longer being thrown. Is this fine or does this create another issue that I'm unaware of?
HERE'S MY CODE:
[code2=vbnet]dbConn = New MySqlConnection("Server=" & FormLogin.ComboBoxServerIP.SelectedItem & ";Port=3306;Uid=parts;Password=parts;Database=accounting")
Dim account As Boolean = True
If dbConn.State = ConnectionState.Open Then
dbConn.Close()
End If
dbConn.Open()
Dim dbQuery As String = "SELECT * FROM customer WHERE accountNumber = '" & TextBoxAccount.Text & "';"
Dim dbData As MySqlDataReader
Dim dbAdapter As New MySqlDataAdapter
Dim dbCmd As New MySqlCommand
dbCmd.CommandText = dbQuery
dbCmd.Connection = dbConn
dbAdapter.SelectCommand = dbCmd
dbData = dbCmd.ExecuteReader
While dbData.Read()
If dbData.HasRows() = True Then
MessageBox.Show("Customer record already exists!")
account = False
dbData.Close()
Else
dbData.Close()
account = True
End If
End While
dbData.Close()
If account = True Then
Dim dbQuery2 As String = "INSERT INTO customer (accountNumber, nameLAST, nameFIRST, nameCOMPANY, addressSTREET, addressSTREET1," & _
"addressCITY, addressSTATE, addressZIPCODE, phone, fax, email)" & _
"VALUES('" & TextBoxAccount.Text & "','" & TextBoxLastName.Text & "','" & TextBoxFirstName.Text & "','" & TextBoxCompanyName.Text & _
"','" & TextBoxAddress1.Text & "','" & TextBoxAddress2.Text & "','" & TextBoxCity.Text & "','" & ComboBoxState.SelectedItem & _
"','" & MaskedTextBoxZip.Text & "','" & MaskedTextBoxPhone.Text & "','" & MaskedTextBoxFax.Text & "','" & TextBoxEmail.Text & "')"
Dim dbData2 As MySqlDataReader
Dim dbAdapter2 As New MySqlDataAdapter
Dim dbCmd2 As New MySqlCommand
dbCmd2.CommandText = dbQuery2
dbCmd2.Connection = dbConn
dbAdapter2.SelectCommand = dbCmd2
dbData2 = dbCmd2.ExecuteReader
Call lockForm()
MessageBox.Show("Customer account record saved successfully!")
End If[/code2]
Your help would be greatly appreciated, thanks.
//Kismet