how to solve mysql define error in vb.net app? - mysql

i have this piece of code
Dim query As String = "INSERT INTO person(name) VALUES (#name);"
Try
conn.Open()
Dim cmd As MySqlCommand = New MySqlCommand(query, conn)
If String.IsNullOrEmpty(name.Text) Or String.IsNullOrWhiteSpace(name.Text) Then
cmd.Parameters.AddWithValue("#name", " ")
Else
cmd.Parameters.AddWithValue("#name", name.Text)
End If
cmd.ExecuteNonQuery()
Catch ex As MySqlException
MsgBox(ex.ToString)
End Try
when i run the code i get this exception:
MySql.Data.MySqlClient.MySqlException (0x80004005): Fatal error encountered during command execution. ---> MySql.Data.MySqlClient.MySqlException (0x80004005): Parameter '#name' must be defined.
how to solve?

Do it this way:
Dim query As String = "INSERT INTO person(name) VALUES (#name);"
Try
conn.Open()
Dim cmd As MySqlCommand = New MySqlCommand()
cmd.CommandText = query;
If String.IsNullOrEmpty(name.Text) Or String.IsNullOrWhiteSpace(name.Text) Then
cmd.Parameters.AddWithValue("#name", " ")
Else
cmd.Parameters.AddWithValue("#name", name.Text)
End If
cmd.Connection = conn;
cmd.ExecuteNonQuery()
Catch ex As MySqlException
MsgBox(ex.ToString)
End Try

Solved just adding this to connection string:
Allow User Variables=True

Related

How do I catch MySQL errors in a try catch block?

I am trying to execute a query on a MySQL database. I've put the execute command in a try catch block, but It still throws an exception when the query fails. How do I prevent this and simply catch the error?
Dim db_connection As String = my_connection_string
Dim MyConn As OdbcConnection
MyConn = New OdbcConnection(db_connection)
Dim MyCommand As New OdbcCommand()
Dim myReader As OdbcDataReader = Nothing
MyCommand.Connection = MyConn
MyConn.Open()
query = "Alter table Table1 ADD COLUMN `col_2` VARCHAR(50) AFTER `col_1`"
MyCommand.CommandText = query
try
myReader = MyCommand.ExecuteReader()
catch ex as Exception
output(ex.tostring)
end try
myReader.Close()
If the column already exists, it throws an error but if it does not, then everything is fine and the query gets executed and the program go on.
If you are going to catch an exception, you should catch the whole code of the query because, the connection can fail as well for instance.
Dim db_connection As String = my_connection_string
Dim MyConn As OdbcConnection
Dim MyCommand As New OdbcCommand()
try
MyConn = New OdbcConnection(db_connection)
MyCommand.Connection = MyConn
MyConn.Open()
query = "Alter table Table1 ADD COLUMN `col_2` VARCHAR(50) AFTER `col_1`"
MyCommand.CommandText = query
MyCommand.ExecuteNonQuery()
catch ex as Exception
output(ex.tostring)
end try
EDIT:
Try
Using connection As New OdbcConnection(my_connection_string)
query = "Alter table Table1 ADD COLUMN `col_2` VARCHAR(50) AFTER `col_1`"
Using command As New OdbcCommand(query, connection)
connection.Open()
command.ExecuteNonQuery()
End Using
End Using
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
EDIT
Try
'code here
Catch exception As OdbcException
Console.WriteLine(exception.Message)
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try

How to display message if no records found

This is my code in filtering data into TextBoxes/Labels
Dim reader As MySqlDataReader
Try
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
Dim query As String
query = "SELECT * FROM member WHERE (memor = '" & memor.Text & "')"
sqlcom = New MySqlCommand(query, conn)
reader = sqlcom.ExecuteReader
While reader.Read()
Me.lblname.Text = reader("membname").ToString
Me.txtmembtype.Text = reader("membtype").ToString
Me.lblmembdate.Text = reader("membdate").ToString
End While
conn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
conn.Dispose()
End Try
Now I want to have a message box that prompts that there is no data found in my mysql database? Any idea?
You can simply use reader.HasRows() to check for records
reader = sqlcom.ExecuteReader
if Reader.HasRows() Then
While reader.Read()
Me.lblname.Text = reader("membname").ToString
Me.txtmembtype.Text = reader("membtype").ToString
Me.lblmembdate.Text = reader("membdate").ToString
End While
Else
MessageBox.Show("No records found")
End If
Beaten, but still posting for the obligatory SQL parameterization warning.
Revised code:
Try
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
Dim query As String
query = "SELECT * FROM member WHERE (memor = #memor)"
sqlcom = New MySqlCommand(query, conn)
sqlcom.Parameters.AddWithValue("#memor", memor.Text)
reader = sqlcom.ExecuteReader
If reader.HasRows Then
While reader.Read()
Me.lblname.Text = reader("membname").ToString
Me.txtmembtype.Text = reader("membtype").ToString
Me.lblmembdate.Text = reader("membdate").ToString
End While
Else
MsgBox("No data found.")
End If
conn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
conn.Dispose()
End Try

VB.NET MySQL Fatal Error But database accepts input

I get a fatal error when I execute this code, but the info still shows up in the database. Can anyone see an error here?
sql.RunQuery("INSERT INTO test(test1,test2,test3) values(?FName,?LName,?DOB)")
sql.SQLcmd.Parameters.AddWithValue("?FName", Gender.SelectedItem)
sql.SQLcmd.Parameters.AddWithValue("?LName", Age)
sql.SQLcmd.Parameters.AddWithValue("?DOB", RDIAge)
sql.SQLcmd.ExecuteNonQuery()
Your code should looks like:
Dim connString As String = "Database=yourDB;Data Source=localhost;";
connString +="User Id=yourUserDb;Password=dbPass"
Dim conn As New MySqlConnection(connString)
Dim cmd As New MySqlCommand()
Try
conn.Open()
cmd.Connection = conn
cmd.CommandText = "INSERT INTO test(test1,test2,test3) values(#FName,#LName,#DOB)"
cmd.Prepare()
cmd.Parameters.AddWithValue("#FName", Gender.SelectedItem)
cmd.Parameters.AddWithValue("#LName", Age)
cmd.Parameters.AddWithValue("#DOB", RDIAge)
cmd.ExecuteNonQuery()
conn.Close()
Catch ex As MySqlException
Console.WriteLine("Error: " & ex.ToString())
End Try

Can't save info to mysql database

I'm trying to store info to a mysql database, but for some reason it's not working for me.
Dim connString As String = "server=sql3.freemysqlhosting.net; userid=Censored;password=Censored;database=sql364455"
Dim conn As New MySqlConnection(connString)
Dim cmd As New MySqlCommand()
Try
conn.Open()
cmd.Connection = conn
cmd.CommandText = "INSERT INTO accounts (`user_num`, `username`, `password`) values (#1,#2,#3)"
MsgBox("1")
cmd.Parameters.AddWithValue("#1", TextBox1.Text)
cmd.Parameters.AddWithValue("#2", TextBox2.Text)
cmd.Parameters.AddWithValue("#3", TextBox3.Text)
MsgBox("2")
cmd.ExecuteNonQuery()
MsgBox("3")
MessageBox.Show("User Profile Created!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
conn.Close()
Catch ex As Exception
End Try
According to this code the message boxes 1 and 2 is popping up but 3 is not.
Any idea? thanks in advance.
#WoeIsMe Plutonix already told you how to write it correctly (put it into brackets: [password]). And, just and advice; if you're already using MsgBox as a debugging tool, always put another one inside the catch block with the exception MsgBox(ex.ToString()), so you'll know why it is not working. – Josh Part 8
I've put a msgbox on the exception block to find out what is the problem and I found that it didn't worked because: "Duplicate entry '0' for key 'PRIMARY' " - it didn't worked because there was already a user with this number.
thanks alot for all the people who helped me.
the code I used to detect the problem:
Dim connString As String = "server=sql3.freemysqlhosting.net; userid=username;password=password;database=sql364455"
Dim conn As New MySqlConnection(connString)
Dim cmd As New MySqlCommand()
Try
conn.Open()
cmd.Connection = conn
cmd.CommandText = "INSERT INTO accounts (user_num, username, password) values (#1,#2,#3)"
MsgBox("1")
cmd.Parameters.AddWithValue("#1", TextBox1.Text)
cmd.Parameters.AddWithValue("#2", TextBox2.Text)
cmd.Parameters.AddWithValue("#3", TextBox3.Text)
MsgBox("2")
cmd.ExecuteNonQuery()
MsgBox("3")
MessageBox.Show("User Profile Created!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
conn.Close()
Catch ex As Exception
' here
MsgBox(ex.Message)
End Try

Timeout in IO operation MySQL vb.NET 2012

Just last friday this code was working perfectly and I was able to create a table in MySQL database but now its showing an error "Timeout in IO operation", need solution guys thanks.
Private Sub toCreateTable()
Dim varString As String = "tablenaming"
Dim Query As String
Dim con As MySqlConnection = New MySqlConnection("server=192.168.0.1; user=logging; database=db_logging; port=3306; password=passing;")
con.Open()
Query = "CREATE TABLE `" & varString & "` ( usernames varchar(50) ) "
Dim cmd As New MySqlCommand(Query, con)
If (cmd.ExecuteNonQuery()) Then
End If
con.Close()
End Sub
Try catch.... Put this in place of your If Then End If.
Try
cmd.ExecuteNonQuery()
'any other code that needs to be completed after running query
Catch ex As Exception
MsgBox(ex.Message)
Finally
con.Close()
End Try
Then let us know if you get any exception message