I'm trying to setup connection to a database within my program, and I'm running into a problem when adding data to the Database. I know I've done something wrong but I can't for the life of me figure it out!
Basically the program throws an error at the ExecuteNonQuery() line, saying the connection is not open. But I've opened the connection just a couple of lines above? It might be because I'm trying to import the connection from a seperate form?
The Error is : An unhandled exception of type 'System.InvalidOperationException' occurred in MySql.Data.dll Additional information: Connection must be valid and open.
I know some parts are commeneted out, thats just what I do whem I'm troubleshooting.
Imports MySql.Data.MySqlClient
Public Class frmCompanyAdd
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
'Try
frmMain.conn.Open()
' Catch ex As Exception
'End Try
Dim cmd As New MySqlCommand(String.Format("INSERT INTO `Company` (`companyname` , `location` , `contactphone` , `numberpc`) VALUES (" & txtcompanyname.Text & "," & txtPst.Text & " , " & txtPhone.Text & "," & txtPcNo.Text & ")"))
cmd.ExecuteNonQuery()
'frmMain.conn.Close()
End Sub