Ok. I am building a program for a small office and I have a little problem with the code so I thought it's better to post a topic before continiue.. I have search into google and a lot of forums about database connectivity and my problem is simillar to other but a lot of things differs... The error that I get is on executeNoQuery()
and it's called {"Connection must be valid and open."}... Well a lot of things may related to about that... At least now from my re-search I covered all the filds of the problem and still not fixed... So please check my code for (Add new customer) button and let me know where I have mess up...!!! Thank you very much for your understanding..!!!
Imports MySql.Data.MySqlClient
Public Class Form2
Dim ServerString As String = "Server=localhost;User Id=root;Password=root;Database=customers2"
Dim SQLConnection As MySqlConnection = New MySqlConnection
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SQLConnection.ConnectionString = ServerString
Try
If SQLConnection.State = ConnectionState.Closed Then
SQLConnection.Open()
MsgBox("Successfully Connected to MySQL Database")
Else
SQLConnection.Close()
MsgBox("Connection is Closed.")
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Public Sub SaveNames(ByRef SQLStatement As String)
Dim cmd As MySqlCommand = New MySqlCommand
With cmd
.CommandText = SQLStatement
.CommandType = CommandType.Text
.Connection = SQLConnection
.ExecuteNonQuery()
End With
SQLConnection.Close()
MsgBox("Successfully Added!")
SQLConnection.Dispose()
End Sub
Private Sub addUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addUser.Click
Dim SQLStatement As String = "INSERT INTO people(First_Name,Last_Name,Street,State,City,Postal,Country,Phone,Mobile,Fax,Email,Note) VALUES('" & firsttxt.Text & "', '" & lasttxt.Text & "', '" & streettxt.Text & "', '" & statetxt.Text & "', '" & citytxt.Text & "', '" & postaltxt.Text & "', '" & countrytxt.Text & "', '" & phonetxt.Text & "', '" & mobiletxt.Text & "', '" & faxtxt.Text & "', '" & emailtxt.Text & "', '" & notetxt.Text & "')"
SaveNames(SQLStatement)
End Sub
Private Sub products_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles products.Click
Me.GroupBox2.Visible = True
Me.customers.Enabled = True
Me.products.Enabled = False
End Sub
Private Sub customers_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles customers.Click
Me.customers.Enabled = False
Me.products.Enabled = True
Me.GroupBox2.Visible = False
End Sub
End Class