VB MySql AddWithValue doesn't pass the value successfully - mysql

I have the following VB code
Dim cmd As New MySqlCommand("SELECT code FROM decoder WHERE ann_id = #aid", conn)
cmd.Parameters.AddWithValue("#aid", 1)
Dim reader As MySqlDataReader = cmd.ExecuteReader()
The reader.Read() then gives me nothing, however, if I replace the code by
Dim cmd As New MySqlCommand("SELECT code FROM decoder WHERE ann_id = 1", conn)
Dim reader As MySqlDataReader = cmd.ExecuteReader()
reader.Read() gives the correct result. Tried using Add instead, doesn't help. What am I missing here?
Many thanks,

remove #
Dim cmd As New MySqlCommand("SELECT code FROM decoder WHERE ann_id = #aid", conn)
cmd.Parameters.AddWithValue("aid", 1)
Dim reader As MySqlDataReader = cmd.ExecuteReader()

Private Sub ButtonInsert_Click(sender As Object, e As EventArgs) Handles ButtonInsert.Click
Dim conn As New MySqlConnection
Dim cmd As New MySqlCommand
conn.ConnectionString = Me.Tag.ToString
Try
conn.Open()
cmd.Connection = conn
cmd.CommandText = "INSERT INTO myTable VALUES(NULL, #number, #text)"
cmd.Prepare()
cmd.Parameters.AddWithValue("#number", 1) 'sets #number to be numeric
cmd.Parameters.AddWithValue("#text", "One") 'sets #text to be characters
For i = 1 To 1000
cmd.Parameters("#number").Value = i
cmd.Parameters("#text").Value = "A string value"
cmd.ExecuteNonQuery()
Next
Catch ex As MySqlException
MessageBox.Show("Error " & ex.Number & " has occurred: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try

Related

Displaying a value obtained from a mysql query into a VB.net textbox

I am trying to display a value I obatain from a COUNT(*) query in a textbox in vb.net but instead of displaying the obtained value it displays the actual query.
Here is my code:
Imports MySql.Data.MySqlClient
Public Class Statistics
Dim conn As MySqlConnection
Dim command As MySqlCommand
Dim query As String
Dim dadapter As New MySqlDataAdapter
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
conn = New MySqlConnection
conn.ConnectionString =
"server=localhost;userid=root;database=librarydatabase"
Dim reader As MySqlDataReader
Try
conn.Open()
query = "SELECT COUNT(*) FROM login where users='" & Username.Text & "'"
command = New MySqlCommand(query, conn)
reader = command.ExecuteReader
dadapter.SelectCommand = command
If reader.HasRows Then
reader.Read()
takenout.Text = (query)
End If
conn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
conn.Dispose()
End Try
End Sub
Here is a screenshot of my program:
When trying to fix the code I change it to replace takenout.Text = (query) with takenout.Text = (reader.read()) and deleting the reader.read()) above and dadapter.SelectCommand = command
Here is the changed code:
Imports MySql.Data.MySqlClient
Public Class Statistics
Dim conn As MySqlConnection
Dim command As MySqlCommand
Dim query As String
Dim dadapter As New MySqlDataAdapter
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
conn = New MySqlConnection
conn.ConnectionString =
"server=localhost;userid=root;database=librarydatabase"
Dim reader As MySqlDataReader
Try
conn.Open()
query = "SELECT COUNT(*) FROM login where users='" & Username.Text & "'"
command = New MySqlCommand(query, conn)
reader = command.ExecuteReader
If reader.HasRows() Then
takenout.Text = (reader.Read())
End If
conn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
conn.Dispose()
End Try
End Sub
This code doesn't fix the problem but instead displays true in the textbox when the button is pressed no matter whats in the username textbox.
Here is an image of that:
Use command.ExecuteScalar() instead.
Using command.executescalar() fixes the issue.
Here is a copy of the fixed code if anyone else has a similar issue and wants to see a working example.
Public Class Statistics
Dim conn As MySqlConnection
Dim command As MySqlCommand
Dim query As String
Dim dadapter As New MySqlDataAdapter
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
conn = New MySqlConnection
conn.ConnectionString =
"server=localhost;userid=root;database=librarydatabase"
Dim numtakenout As String
Try
conn.Open()
query = "SELECT COUNT(*) FROM login where users='" & Username.Text & "'"
command = New MySqlCommand(query, conn)
numtakenout = Convert.ToString(command.ExecuteScalar())
takenout.Text = numtakenout
conn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
conn.Dispose()
End Try
End Sub

check the manual that corresponds to your mariadb server version for the right syntax to use near "user" at line 1

I have a textbox, button, and datagridview in my form. When i click the button, system will grab a table depending on my textbox from the database and show on datagridview.
I getting this error when i click the button. Where am I wrong?
here is my dbconn
Module mod_dbconn
Public conn As MySqlConnection
Public Sub openDB()
Dim dbname As String = scr_sales.btn_dbswitch.Text
Dim server As String = "localhost"
Dim user As String = "root"
Dim password As String = ""
Try
conn = New MySqlConnection
conn.ConnectionString = String.Format("server={0}; user id={1}; password={2}; database={3}; pooling=false", server, user, password, dbname)
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
This is my form
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim query As String = "SELECT * FROM '" + TextBox1.Text + "'"
Dim cmd As New MySqlCommand(query, conn)
Dim da As New MySqlDataAdapter(cmd)
Dim dt = New DataTable
Dim cb As MySqlCommandBuilder
cb = New MySqlCommandBuilder(da)
DataGridView1.Refresh()
Try
conn.Open()
da.Fill(dt)
Dim bsource As New BindingSource
bsource.DataSource = dt
Me.DataGridView1.DataSource = bsource
da.Update(dt)
conn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
conn.Dispose()
End Try
End Sub
You are trying to build a dynamic table select so for the table name you don't need the quotes around the tablename
"SELECT * FROM " + TextBox1.Text + " ;"

Choose from various dates using date time picker

This button filters the datagridview using datetimepicker.
What should I do if I want to filter it between dates?
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
MySqlConn = New MySqlConnection
MySqlConn.ConnectionString = "Server = Localhost; database = venuesdb; user id = root; Password = "
Dim SQLDataAdapter As New MySqlDataAdapter
Dim DatabaseDatSet As New DataTable
Dim Bindsource As New BindingSource
Try
MySqlConn.Open()
Dim Query = "Select * From venuesdb.cost where EventDate = ('" & DateTimePicker1.Text & "')"
Command = New MySqlCommand(Query, MySqlConn)
SQLDataAdapter.SelectCommand = Command
SQLDataAdapter.Fill(DatabaseDatSet)
Bindsource.DataSource = DatabaseDatSet
DataGridView1.DataSource = Bindsource
SQLDataAdapter.Update(DatabaseDatSet)
MySqlConn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
MySqlConn.Dispose()
End Sub

What is wrong in this code Vb MySql data to TextBox

What is wrong in this code
I run the software click the button and nothing happens if you have a better code that would help thanks in advance
Dim sql As String
Dim con As MySqlConnection = New
MySqlConnection("Server=localhost;Database=******;Uid=*****;password =******")
Dim ds As DataSet = New DataSet
Dim dataadapter As MySqlDataAdapter = New MySqlDataAdapter
Dim cmd As MySqlCommand = New MySqlCommand()
Dim datareader As MySqlDataReader
Try
sql = "SELECT * FROM users"
con.Open()
cmd.CommandText = sql
cmd.Connection = con
dataadapter.SelectCommand = cmd
datareader = cmd.ExecuteReader
While datareader.Read
datareader.Read()
Email.Text = datareader("Email")
LastName.Text = datareader("LastName")
Address.Text = datareader("Address")
End While
Catch ex As Exception
End Try
con.Close()
End Sub
Narrow the scope of your select. The way its written I think it might set the textbox 100 times if 100 results are returned. Here is how I would write this:
Imports MySql.Data.MySqlClient
Public Sub getData()
Dim objConn As MySqlConnection = New MySqlConnection("Data Source=localhost;" _
& "Database=TestDB;" _
& "User ID=Root;" _
& "Password=myPassword;")
Dim strSQL As String = "SELECT TOP 1 Email, LastName, Address FROM users"
Dim da As MySql.Data.MySqlClient.MySqlDataAdapter
Dim dt As New DataTable
Try
objConn.Open()
da = New MySql.Data.MySqlClient.MySqlDataAdapter(strSQL, objConn)
da.Fill(dt)
If dt.Rows.Count > 0 Then
Email.Text = dt.Rows(0)("Email").ToString
LastName.Text = dt.Rows(0)("LastName").ToString
Address.Text = dt.Rows(0)("Address").ToString
End If
da = Nothing
objConn.Close()
objConn = Nothing
Catch ex As Exception
objConn.Close()
objConn = Nothing
End Try
End Sub

Multiply Insert to MySQL with vb.net

i have a problem for put in multiply input
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
this current code is for putting only one input for each table "IdBorangA" and "Answer"
i try by tweak it a little bit
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
parameterB_IdA.Value = Label2.Text
Dim parameterB_Answer As MySqlParameter = New MySqlParameter("#B_Answer", MySqlDbType.VarChar, 300)
parameterB_Answer.Value = TextBox1.Text
parameterB_Answer.Value = TextBox2.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
by adding more parameter "parameterB_IdA.Value = Label1.Text" for IdBorangA
and "parameterB_Answer.Value = TextBox2.Text" for Answer
but the result i get that the table only filling with the data from Label2.text and Textbox2.text.
i dunno what went wrong..
if you guys have any idea to help me about this problem.
feel free to answer :)
This line is wrong
Dim myCommand As MySqlCommand = New MySqlCommand(StrSQL, conn.open)
the correct one is
Dim myCommand As MySqlCommand = New MySqlCommand(StrSQL, conn)
This query is an INSERT so you need to call ExecuteNonQuery not ExecuteReader which is intended for data retrivial with a MySqlDataReader
And, of course, you write only one record in the database not two, you are only changing the value of the parameters before sending the command to the database, only the last value will be written.