Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with creating an INSERT statement using MySQL
#4
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:
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


Messages In This Thread
Re: Help with creating an INSERT statement using MySQL - by kismetgerald - 08-30-2012, 06:18 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I read a link from mysql databse??? mnxford 9 31,125 04-12-2013, 06:27 PM
Last Post: brandonio21
  How do I use Listview to delete MySQL DB records kismetgerald 10 32,104 11-28-2012, 05:19 PM
Last Post: brandonio21
  [SOLVED] MySQL Update Query - What am I doing wrong? kismetgerald 11 40,577 10-18-2012, 07:16 PM
Last Post: brandonio21
  How do I fill my form with MySQL data based on Combobox? kismetgerald 8 25,325 10-14-2012, 09:05 PM
Last Post: brandonio21
  MySQL Database Issue Moldraxian 7 27,846 08-13-2012, 08:58 PM
Last Post: brandonio21
  MySql Database Queries in VB.Net Moldraxian 6 21,112 07-26-2012, 03:44 PM
Last Post: Moldraxian
  How to Package MySql Connector and MySql Program? Moldraxian 3 14,057 07-20-2012, 12:23 PM
Last Post: brandonio21
  MySql Help Blackrobot 3 13,837 04-06-2012, 03:49 AM
Last Post: xolara
  VB and MySQL help please Chad1020 2 11,606 03-21-2012, 04:10 PM
Last Post: brandonio21
  MySQL Help -Kid- 6 23,825 02-20-2012, 11:57 AM
Last Post: brandonio21

Forum Jump:


Users browsing this thread: 1 Guest(s)