VB.NET MySQL Fatal Error But database accepts input - mysql

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

Related

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

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

VB Insert into MySql

As a Vb noob im working on this school project. I need to insert my values into my mysql database but for a reason it isn't inserting tried everything but i can't find why it isn't inserting.
Thx in advance
Dim sqlCommand As New MySqlCommand
Dim SQLConnection As MySqlConnection = New MySqlConnection
Dim strStockSQL As String
Dim server As String = "localhost"
Dim DatabaseName As String = "Gip"
Dim userName As String = "root"
Dim password As String = ""
SQLConnection = New MySqlConnection()
If Not conn Is Nothing Then conn.Close()
conn.ConnectionString = String.Format("server={0}; user id={1}; password={2}; database={3}; pooling=false", server, userName, password, DatabaseName)
Try
strStockSQL = "insert into stock (Barcode,Naam_Product,Verkoopprijs) values (#Barcode,#Naam_product,#Verkoopprijs)"
sqlCommand.Connection = SQLConnection
sqlCommand.CommandText = strStockSQL
sqlCommand.Parameters.AddWithValue("#Barcode", Convert.ToString(txtBarcode.Text))
sqlCommand.Parameters.AddWithValue("#Naam_product", Convert.ToString(txtNaam.Text))
sqlCommand.Parameters.AddWithValue("#Verkoopprijs", Convert.ToInt32(txtVP.Text))
sqlCommand.ExecuteNonQuery()
Catch ex As Exception
MsgBox("Error occured: Could not insert record")
When executing an sqlCommand you must have it's related connection object in open state.
SQLConnection.Open()
sqlCommand.ExecuteNonQuery()
SQLConnection.Close()
Also, read about Using statement and use it for SqlConnection.
Another thing: this code line is meaningless: If Not conn Is Nothing Then conn.Close() remove it.

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

Inserting VB.net Label to mysql table

How to Inserting Label.text data into mySql table.
i have no problem with textbox.text but i can't figure out how it with Label.text
i try the same code with textbox.text
parameterB_Answer.Value = TextBox1.Text
it work find but when i try with
parameterB_Answer.Value = Label1.Text
mySqlReader seems can't read it.
Update:
1.1.1 is label1.text. My Idea is to insert the text "1.1.1" from Label1 as Primary key and the Textbox(textbox1.text) as the following
my code is:
Try
Dim StrSQL As String = "INSERT INTO boranga" & _
"(IdBorangA,Answers)" & _
"VALUES (#B_IdA,#B_Answer);"
Dim myCommand As MySqlCommand = New MySqlCommand(StrSQL, conn.open)
myCommand.CommandType = CommandType.Text
Dim parameterB_IdA As MySqlParameter = New MySqlParameter("#B_IdA", MySqlDbType.VarChar, 300)
parameterB_IdA.Value = Label1.Text
Dim parameterB_Answer As MySqlParameter = New MySqlParameter("#B_Answer", MySqlDbType.VarChar, 300)
parameterB_Answer.Value = TextBox1.Text
With myCommand.Parameters
.Add(parameterB_IdA)
.Add(parameterB_Answer)
End With
Dim result As MySqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
MsgBox("Tersimpan", vbYes, "boranga")
Catch SqlEx As MySqlException
Throw New Exception(SqlEx.Message.ToString())
End Try
but when I change the value of Label1.text (1.1.1) to 111, it works just fine. probably because I put INT for the column for label1.text to fill while "1.1.1" isn't integer
thank a lot
PS:seems i can't post image because low of reputation
Try using this code format first of all:
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
Dim conn As MySqlConnection
'Connect to the database using these credentials
conn = New MySqlConnection
conn.ConnectionString = "server=your server site (generally long url); user id=login id for mysql user; password=self explanatory; database=name of the DB you're trying to reach"
'Try and connect (conn.open)
Try
conn.Open()
Catch myerror As MySqlException 'If it fails do this... (i.e. no internet connection, etc.)
MsgBox("Error connecting to database. Check your internet connection.", MsgBoxStyle.Critical)
End Try
'MySQL query (where to call for information)
Dim myAdapter As New MySqlDataAdapter
'Tell where to find the file with the emails/passes stored
Dim sqlquery = "SELECT * FROM the database you selected above WHERE Email = '" & txtEmail.Text & "' AND Password = '" & txtPassword.Text & "'"
Dim myCommand As New MySqlCommand
myCommand.Connection = conn
myCommand.CommandText = sqlquery
'Start query
myAdapter.SelectCommand = myCommand
Dim myData As MySqlDataReader
myData = myCommand.ExecuteReader
If myData.HasRows = 0 Then
MsgBox("Invalid email address or password.", MsgBoxStyle.Critical)
Else
MsgBox("Logged in as " & txtEmail.Text & ".", MsgBoxStyle.Information)
Me.Close()
End If
End Sub
And to insert label.text try replacing one of the textbox.text fields first and see if it will accept it. If it does, then the answer was all in the formatting.
Also, do not forget to call:
imports mysql.data.mysqlclient
and make sure to add the mysql.data reference.