Connection to a mysql database fails - mysql

When I try and execute the following code to input a value into a text box it comes up with an error saying : Authentication to host '' for user '' using method 'mysql_native_password' failed with message: Access denied for user ''#'Adam-PC' (using password: NO)
Public Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles Datetxt.TextChanged
Dim ConnectionString As String
Dim con As New MySqlConnection
ConnectionString = "server=localhost;user id=root;database=river_data;allowuservariables=True"";Integrated Security=True;MultipleActiveResultSets=True;"
Dim cmd As MySqlCommand
Dim query As String = "SELECT date FROM river-derwent-keswick-portinscale WHERE(`date` = '01.01.2013') "
cmd = New MySqlCommand(query, con)
con.Open()
Dim myreader As MySqlDataReader = cmd.ExecuteReader()
If myreader.Read() Then
Datetxt.Text = myreader.GetValue(0)
Else
Me.Close()
HomeForm.Show()
End If
myreader.Close()

ConnectionString = "server=localhost;user id=root;database=river_data;allowuservariables=True"";Integrated Security=True;MultipleActiveResultSets=True;"
it looks like the user part is wrong. Try:
ConnectionString = "server=localhost;user=root;database=river_data;allowuservariables=True"";Integrated Security=True;MultipleActiveResultSets=True;"

Related

chart connection with MySql

I am connecting a chart object in mysql database but getting this error:
object reference not set to an instance of an object
This is the code I used:
Imports MySql.Data.MySqlClient
Public Class Form1
Dim con As New MySqlConnection
Dim com As MySqlCommand
Dim dt As New DataTable
Private Sub btnlaod_Click(sender As Object, e As EventArgs) Handles btnlaod.Click
con = New MySqlConnection
con.ConnectionString = "server=localhost;userid=root;password=;database=noh_mis"
Dim Reader As MySqlDataReader
Try
con.Open()
Dim query As String
query = "Select * From database.students_profile"
Reader = com.ExecuteReader
While Reader.Read
Chart1.Series("Male").Points.AddXY(Reader.GetString("Gender"), Reader.GetString("GradeLevel"))
End While
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
con.Dispose()
End Try
End Sub
End Class
You do not make a command before using com.ExecuteReader. You make a string of sql, but never connect that with your database.
Try adding the below line after building query but before Reader = com.ExecuteReader:
com = New MySqlCommand(query, con)

How to select from a cell in mysql and add to button.text

i'm a newbie to visual basic 2015 in visual studio community.
what i'm trying to do on load of main form
i have 7 buttons that need the text field changed to correspond with the entries in the Database. these buttons can be change to now categories by the end user down the road.
I'm using MySQL for my database. Any help would be MUCH appreciated as i have searched google and youtube and it's an endless world of OVERLOAD.
My DB Structure is as follows since i cant embed an image:
idbtncat btn_Name btn_caption PanelNo btn_image
1 btn_cat1 Pizza pnl_cat1 pizza.jpg
Public Class frm_MainConsole
Dim conn As New MySqlConnection
Sub dbconn()
Dim DatabaseName As String = "posdb"
Dim server As String = "localhost"
Dim userName As String = "root"
Dim password As String = "8943117"
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
conn.Open()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub frm_MainConsole_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Show()
dbconn()
Dim query As String
Dim command As MySqlCommand
Dim reader As MySqlDataReader
Try
dbconn()
query = "select * from posdb.button_cat where btn_caption"
command = New MySqlCommand(query, conn)
reader = command.ExecuteReader
While reader.Read
btn_Cat1.Text = reader("btn_caption")
conn.Close()
End While
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
conn.Dispose()
End Try
End Sub
Private Sub testDatabaseConnectionToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles testDatabaseConnectionToolStripMenuItem.Click
If mnit_dbConn.Text = "DB NOT Connected" Then
dbconn()
mnit_dbConn.Text = "DB CONNECTED"
Else
mnit_dbConn.Text = "DB NOT Connected"
conn.Close()
End If
End Sub

Mysql Localhost Access Denied VB.Net

I Have Login Form in VB.net
but i got problem if i access my localhost , previous the code is working, but after i leave the VS and go back again, i got problem like this http://i.imgur.com/SfMghZj.png
this is my source code
MySqlConn = New MySqlConnection
MySqlConn.ConnectionString = "server=localhost;userid=root;password=***;database=exodium"
Dim Reader As MySqlDataReader
Try
MySqlConn.Open()
Dim Query As String
Query = "select * from exodium.member where Username='" & UsernameTxt.Text & "' and Password='" & PasswordTxt.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
Loading.Show()
ElseIf count > 1 Then
MessageBox.Show("Duplicate !")
Else
MessageBox.Show("Not Correct !")
End If
MySqlConn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
MySqlConn.Dispose()
End Try
and this is my Localhost http://i.imgur.com/CfeOHuh.png
anyone can help? thanks T_T
It may be possible of that you don't have permission on mysql database.
Look into mysql.user table that you have entry for localhost and that password[Will be in encrypted form].
if not please insert one and use
CREATE USER 'root'#'localhost' IDENTIFIED BY '14253690';
GRANT ALL PRIVILEGES ON * . * TO 'root'#'localhost' IDENTIFIED BY '14253690';
FLUSH PRIVILEGES;
Try
Dim MySqlConn As MySqlConnection
Dim COMMAND As MySqlCommand
MySqlConn = New MySqlConnection
MySqlConn.ConnectionString = "server=localhost;user id=root;password=;database=exodium"
Dim READER As MySqlDataReader
MySqlConn.Open()
Dim Query As String
Query = "SELECT Username,Password FROM member"
COMMAND = New MySqlCommand(Query, MySqlConn)
READER = COMMAND.ExecuteReader
While READER.Read
Dim userNameDB = READER.GetString("Username")
Dim PasswordDB = READER.GetString("Password")
Dim userName As String = UsernameTxt.Text
Dim Password As String = PasswordTxt.Text
If userNameDB = userName And PasswordDB = Password Then
MessageBox.Show("Duplicate !")
Else
MessageBox.Show("Not Correct !")
End If
End While
MySqlConn.Close()
Catch myerror As Exception
MessageBox.Show(myerror.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try

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.

access a mysql db and displaying it in a gridview or table

I am trying to access a Mysql DB and display it in a Gridview in .Net
I can get it to connect to the DB, but it displays nothing and there is records in the DB, this is the code i am using.
Dim MysqlConn As MySqlConnection
Dim ContactsCommand As New MySqlCommand
Dim ContactsAdapter As New MySqlDataAdapter
Dim ContactsData As New DataTable
Dim SQL As String
Private Sub btnGrabData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGrabData.Click
MysqlConn = New MySqlConnection()
SQL = "SELECT AffID FROM toutcome"
MysqlConn.ConnectionString = "Server=localhost;Database=merrywoodtest;UID=MerryWoodTest;PWD=H0r$hamTest;"
Try
MysqlConn.Open()
ContactsCommand.Connection = MysqlConn
ContactsCommand.CommandText = SQL
ContactsAdapter.SelectCommand = ContactsCommand
ContactsAdapter.Fill(ContactsData)
DataGridView1.DataSource = ContactsData
Catch myerror As MySqlException
MessageBox.Show("Cannot connect to database: " & myerror.Message)
Finally
MysqlConn.Close()
End Try
End Sub
Please make sure that you will call DataBind() method of DataGridView after the data source is assinged.
You may also set the Connection property of the ContactsCommand object and the SelectCommand property of the ContactsAdapter object.