Can't save info to mysql database - mysql

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

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

Populate Label in VB from MySQL select query

I have found a few articles that are similar to my question, and I have tried the suggestions in those articles and none of them have worked for me. What I need seems to be fairly simple and straight forward. (I am able to complete this same action with SQL Server, just not with MySQL. This 'description' information must come a MySQL db)
I have a Listbox and as the user clicks on listbox items, I would like a 'description' label to update with a value pulled from a MySQL database.
I created a public sub in the Module, and I'm calling the sub from the Listbox1_SelectedIndexChanged event (also tried Listbox1_mouseclick event).
However, everything I have tried does not update the label. Any suggestions would be greatly appreciated.
here is the code being used to pull and attempt to populate the label:
Dim conn As New MySqlConnection(My.Resources.MySqlstr)
Try
conn.Open()
Dim cmd As MySqlCommand = New MySqlCommand("select Description from resourceaccess where tid = '" & ReportPicker.ListBox1.ValueMember & "' ", conn)
Dim reader As MySqlDataReader = cmd.ExecuteReader()
While reader.Read()
ReportPicker.Label3.Text = reader.GetString("Description")
End While
reader.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
conn.Close()
End Try
I don't know if something else is wrong too but this definitely is:
Dim cmd As MySqlCommand = New MySqlCommand("select Description from resourceaccess where tid = '" & ReportPicker.ListBox1.ValueMember & "' ", conn)
Apart from the fact that you should be using a parameter there, the use of ValueMember can't possibly be right. That's the name of a column, not a value from that column. You should be using SelectedValue, which is the value from that column for the item that's selected.
here is what worked:
Dim cs As String = My.Resources.MySqlstr
Dim stm As String = "select Description from resourceaccess where resource = '" & ReportPicker.ListBox1.Text & "' "
Dim conn As MySqlConnection = New MySqlConnection(cs)
Try
conn.Open()
Dim cmd As MySqlCommand = New MySqlCommand(stm, conn)
ReportPicker.Label3.Text = Convert.ToString(cmd.ExecuteScalar())
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
conn.Close()
End Try

Show Date with DateTimePicker from MySQL

I'm at the moment having a problem with my program I want to retrieve the Date that is in MySQL DB to show the Date/Week/Year I choose in the DateTimePicker in my ListView but I encountered a problem and can't see the solution nor have I found something that looks like it:
Try
conn.Open()
Dim Reader As MySqlDataReader
Dim Query As String
Query = "Select * from farm.sales where datesale= ('" & DateTimePicker1.Text & "') "
command = New MySqlCommand(Query, conn)
Reader = command.ExecuteReader
While Reader.Read
Dim Datesale = Reader.GetString("datesale")
Dim Clients= Reader.GetString("clients")
Dim Bank= Reader.GetString("bank")
Dim Revenue= Reader.GetString("revenue")
Dim Total = Reader.GetString("total")
Dim LV As New ListViewItem
LV.Text = Reader("Datesale").ToString
LV.SubItems.Add(Reader("Clients").ToString)
LV.SubItems.Add(Reader("Bank").ToString)
LV.SubItems.Add(Reader("Revenue").ToString)
LV.SubItems.Add(Reader("Total").ToString)
ListView1.Items.Add(LV)
End While
conn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
conn.Dispose()
End Try
The problem I'm getting is in this line LV.Text = Reader("Datesale ").ToString, which I assume its because of the format I'm retrieving but I can't find a solution can someone pls help me get to a solution
Thanks
Edit:
My bad didn't post the error the problem the program crashes and I get the error An unhandled exception of type System.IndexOutOfRangeException occurred in MySql.Data.dll
Additional information: Could not find specified column in results: Datesales
this is how I use listview. I hope this helps.
conn.Close()
conn.Open()
cmd.CommandText = "SELECT * FROM tblUser"
cmd.Connection = conn
dr = cmd.ExecuteReader
While dr.Read
Dim itm = frmAdminMain.lvUser.Items.Add(dr("UserID"))
itm.SubItems.Add(dr("Username"))
itm.SubItems.Add(dr("Password"))
itm.SubItems.Add(dr("FName"))
itm.SubItems.Add(dr("LName"))
itm.SubItems.Add(dr("Title"))
itm.SubItems.Add(dr("Department"))
End While
dr.Close()
conn.Close()
i used lvUser.Items.Add(dr("UserID"))
in your case, you used LV.text..
just change it to LV.Items.Add(Reader.GetString("datesale")
goodluck! :)
Finally resolved it don't know what the error actually was I simply changed the name of the variable and the name on the listview and it solved it
Thanks so much for the replys and

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

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