Timeout in IO operation MySQL vb.NET 2012 - mysql

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

Related

Unknown database 'database'

I am trying to make a secure login for my database, using a MySQL database.
Private Sub logIn_Click(sender As Object, e As EventArgs) Handles logIn.Click
MysqlConn = New MySqlConnection
MysqlConn.ConnectionString =
"server=localhost;userid=root;password=Catawba;database=catawbapartnership"
Dim READER As MySqlDataReader
Try
MysqlConn.Open()
Dim Query As String
Query = "select * from database.admininfo where admin_username= ' " & TB_UN.Text & " ' and admin_password= ' " & TB_PD.Text & " '"
COMMAND = New MySqlCommand(Query, MysqlConn)
READER = COMMAND.ExecuteReader
Dim count As Integer
count = 0
While READER.Read
count = count + 1
End While
If count = 1 Then
MessageBox.Show("Username and Password Accepted")
ElseIf count > 1 Then
MessageBox.Show("Username and Password Are Incorrect")
Else
MessageBox.Show("Username and Password Are Incorrect")
End If
MysqlConn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
End Try`
This is the code, but I keep getting the error of Unknown database'database'
In MySQL Workbench, the entire database is named catawbapartnership
And the table I need to get info from is called admininfo
But, it keep saying I have entered it incorrectly. Please help!
Remove database. from your code. As a default database is in the connection you don't need to specify it in the question.
Please copy your implementation from somewhere else. This has SQL injection vulnerabilities and you should never store plain text passwords.
OWASP has a lot of guidance on being a responsible programmer.

How to save Combobox and checkedlistbox into mysql php using vb

i'm using vb 2017. I try to save combobox value and checkedlistbox item into database. The database table only shows "System.Data.DataRowView" for combobox and "System.Windows.Forms.CheckedListBox+ObjectCollection" for checkedlistbox. Can anyone help me? I'm using mysql phpmyadmin for database. This is the codes i'm using below. it show no error. But the selected item value has not show in database table.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim constr As String = "server=localhost;user=root;database=login;port=3306;password=root123;SslMode=none"
Dim conn As MySqlConnection = New MySqlConnection(constr)
Dim result As Integer
'If True Then
Try
conn.Open()
With {}
Dim cmd As MySqlCommand
For Each item In CheckedListBox1.CheckedItems
cmd = New MySqlCommand("INSERT INTO mastersubject(name,subjectpriority) VALUES(#name,#subjectpriority)", conn)
Next
cmd.Parameters.AddWithValue("#name", ComboBox1.SelectedItem.ToString)
cmd.Parameters.AddWithValue("#subjectpriority", CheckedListBox1.Items.ToString())
result = cmd.ExecuteNonQuery()
'conn.Close()
End With
'End If
If result > 0 Then
MsgBox("Record has been saved")
Else
MsgBox("Error!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
End If
Catch ex As Exception
Console.WriteLine(ex.ToString())
MsgBox(ex.Message)
Finally
conn.Close()
End Try
End Sub
Comments and Explanations are in line.
Private Sub InsertRecord()
Dim constr As String = "server=localhost;user=root;database=login;port=3306;password=root123;SslMode=none"
'A Using...End Using block will ensure that your data objects
'are closed and disposed event if there is an error
Try
Using conn As MySqlConnection = New MySqlConnection(constr)
'You need the new keyword to create the command
'Pass the sql query string and the connection object to the
'constructor of the command
'Create the command once, only the value of the subjectpriority changes
Using cmd As New MySqlCommand("INSERT INTO mastersubject (name, subjectpriority) VALUES (#name, #subjectpriority);", conn)
cmd.Parameters.AddWithValue("#name", ComboBox1.SelectedItem.ToString)
cmd.Parameters.Add("#subjectpriority")
'Open the connection as late as possible
conn.Open()
For i = 0 To CheckedListBox1.CheckedItems.Count - 1
Dim Result As Integer
'You are not adding a new parameter, just changing its value
cmd.Parameters("#subjecpriority").Value = CheckedListBox1.CheckedItems(i).ToString()
'the command will be executed for each item checked
Result = cmd.ExecuteNonQuery()
If Result > 0 Then
MessageBox.Show("Record has been saved")
Else
MessageBox.Show("Error!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
End If
Next
End Using 'Disposes the command
End Using ' closes and disposes the connection
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

How do you perform a SUM query through visual basic?

I am trying to to add up the points a certain user has earned from a mysql database table
My code returns an error saying the syntax is incorrect, I can't figure out how to make it work>
Here is my code:
Imports MySql.Data.MySqlClient
Public Class Leaderboard
Dim MysqlConn As MySqlConnection
Dim COMMAND As MySqlCommand
Private Sub submitBtn_Click(sender As Object, e As EventArgs) Handles submitBtn.Click
MysqlConn = New MySqlConnection
MysqlConn.ConnectionString =
"server=localhost;userid=root;password=root99;database=database"
Dim READER As MySqlDataReader
Try
MysqlConn.Open()
Dim Query As String
Query = "select SUM(Points) * from database.progress where candidate_number_fk = '" & textbox_can.Text & "'"
COMMAND = New MySqlCommand(Query, MysqlConn)
READER = Command.ExecuteReader
MessageBox.Show("You have successfully submitted your score")
MysqlConn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
End Try
My.Forms.Menu1.Show()
Me.Hide()
End Sub
End Class
This is incorrect SQL syntax:
select SUM(Points) *
from database.progress
where . . .
Presumably, you intend:
select SUM(Points) as sumpoints
from database.progress
where . . .
You can then pull the field from the result set by name.
Note: There may be other problems with your code. This only addresses the SQL query.

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)

Visual Basic 2010 and Mysql Nested Query

I'm using VB2010 Express, I've got a Mysql tablet with the following Fields Ext, CostAssing and CostAct, I'm trying to update a Mysql table from the result got in a first query:
First query Get Ext and CostAssing
Second Query Update field CostAct with CostAssing value
**
Imports MySql.Data.MySqlClient
Imports System
Imports System.IO
Public Class Form1
Public dbconn As New MySqlConnection
Public sql As String
Public sqlQuery As String
Public SQLcmd As MySqlCommand
Public dbcomm As MySqlCommand
Public dbread As MySqlDataReader
Dim Ext As String
Dim CostAssing As Integer
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
dbconn = New MySqlConnection("Data Source=localhost ; user id=root ; password=password ; database=calls")
'First Query Get Extension and CostAssing
Try
dbconn.Open()
sqlQuery = "SELECT Ext,CostAssing FROM estruc "
SQLcmd = New MySqlCommand(sqlQuery, dbconn)
dbread = SQLcmd.ExecuteReader
While dbread.Read()
Ext = dbread.Item("Ext")
CostAssing = dbread.Item("CostAssing")
MsgBox("Ext:" & Ext)
'Second Query Update Ext from CostAct to CostAssing
Try
sqlQuery = "UPDATE estruc SET CostAct = '" & "1000" & "' WHERE Ext = '" & Ext & "'"
SQLcmd = New MySqlCommand(sqlQuery, dbconn)
dbread = SQLcmd.ExecuteReader
Catch ex As Exception
MsgBox("Error 2 is :" & ex.Message)
End Try
End While
Catch ex As Exception
MsgBox("Error 1 is :" & ex.Message)
End Try
dbread.Close()
End Sub
End Class
**
The first query run ok, I get fields Ext and CostAssing, But When the second Query try to Update de field CostAct I get following error (Reported by Catch ex As Exception MsgBox("Error 2 is :" & ex.Message)):
"Error 2 is: There is already an open Datareader associated with this Connection which must be closed first."
PLease, Any Ideas?
Add the following line to close the datareader after the first query
dbread.Close();
You'll want to store the values from dbread first (datatable or list, etc), close the while loop, then run the second query with:
SQLcmd.ExecuteNonQuery();
Please see this similar question