Show Date with DateTimePicker from MySQL - 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

Related

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

Database values in textbox if select Combobox vb.net mysql

i cant figure out my problem there is no error but value in combobox not showing from mysqldatabase
here is my code
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim conn As New MySqlConnection
conn.ConnectionString = "server=localhost;user vbid=root;password=admin;database=dnidb"
Dim reader As MySqlDataReader
Try
conn.Open()
Dim query As String
query = "select * from dnidb.hargapro where NamaP = '" & ComboBox1.Text & "'"
command = New MySqlCommand(query, conn)
reader = command.ExecuteReader
While reader.Read
TextBox8.Text = reader.GetInt32("HargaP")
End While
conn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
conn.Dispose()
End Try
End Sub
this code was no error, but didnt show anything in the combobox, i try to make this cone on combobox_selectedindexchanges and same thing nothing showing up in the combobox
i just follow the code from here
https://www.youtube.com/watch?v=HfnoGEmRGvc
I don't see any code that tries to retrieve the product names to combobox, and it seems like the above code should be on SelectedIndexChanged event of the combobox.

Updating MYSQL records via VB.NET

What I am trying to do is do the basic insert, refresh and update mysql querys in my vb.net application. The refresh, and the insert work perfect the only one I can't get is the update can anyone tell me what I am doing wrong? Here is a picture of the program layout:
http://i.imgur.com/mHxKGrb.png
and here is my source code:
Private Sub KnightButton3_Click(sender As Object, e As EventArgs) Handles KnightButton3.Click
cn = New MySqlConnection
cn.ConnectionString = "my info"
Try
cn.Open()
Dim query As String
Dim command As MySqlCommand
query = "UPDATE Refers.exploitsociety SET Refferals='" + refupdate.Text + "' WHERE Refferals='" + DataGridView1.CurrentCell.Selected + "';"
command = New MySqlCommand(query, cn)
cmd.ExecuteNonQuery()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
cn.Dispose()
End Try
cn.Close()
End Sub
I think your problem is in the usage of the property Selected of the CurrentCell. This is a boolean value and it is not the content of that cell.
I would use this kind of code with a parameterized query and a using statement around disposable objects
Try
query = "UPDATE Refers.exploitsociety " & _
"SET Refferals=#refs " & _
"WHERE Refferals=#oldrefs"
Using cn = New MySqlConnection(".... connection string ....")
Using command = New MySqlCommand(query, cn)
cn.Open()
command.Parameters.Add("#refs", MySqlDbType.Int32).Value = _
Convert.ToInt32(refupdate.Text)
command.Parameters.Add("#oldrefs", MySqlDbType.Int32).Value = _
Convert.ToInt32(DataGridView1.CurrentCell.Value.ToString())
command.ExecuteNonQuery()
End Using
End Using
Catch ex As MySqlException
MessageBox.Show(ex.Message)
End Try
Notice that with a using statement you don't need to close/dispose the connection because this is automatically done when the code leaves the using block (also in case of exceptions)

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