Mysql Localhost Access Denied VB.Net - mysql

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

Related

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.

Mysql won't connect to program VB.net

I have installed the mysql and connector but for some reason it won't connect to the database! I am trying to make a simple login system obviously with a mysql database I created with 000webhost.com but every time I try to log in I get the error of not being able to establish connection?
here is my code:
If username.Text = "" Or password.Text = "" Then
MsgBox("Please enter a Username and Password")
Else
'Connect to Database
Dim connect As New MySqlConnection("server=host;user id=dbuser;Password=pass;database=dbname")
Try
connect.Open()
Catch myerror As MySqlException
MsgBox("Error couldn't establish a connection to the database")
End Try
'SQL Query to Get the Details
Dim myAdapter As New MySqlDataAdapter
Dim sqlquery = "Select * From User where username = '" + username.Text + "' And password = '" + password.Text + "'"
Dim myCommand As New MySqlCommand
myCommand.Connection = connect
myCommand.CommandText = sqlquery
'Starting The Query
myAdapter.SelectCommand = myCommand
Dim mydata As MySqlDataReader
mydata = myCommand.ExecuteReader
'To check the Username and password and to validate the login
If mydata.HasRows = 0 Then
MsgBox("Invalid Login")
Else
MsgBox("UserName N Password is accepted!")
End If
End If

MysqlException was unhandled DataReader with this connection must be closed vb.net

I have encountered this problem:
ERROR: There is already an open DataReader associated with this Connection which must be closed first.
Please have a look on my code:
Dim sqlQuery As String = "SELECT * FROM users"
Dim myAdapter As New MySqlDataAdapter
If txtUsername.Text = String.Empty And txtPassword.Text = String.Empty Then
MsgBox("Enter username and password", MsgBoxStyle.Exclamation, "Tea Sparkle POS")
Else
Dim sqlquerry = "Select * From users where username = '" + txtUsername.Text + "' And password= '" + txtPassword.Text + "'"
Dim myCommand As New MySqlCommand()
myCommand.Connection = SQLConnection
myCommand.CommandText = sqlquerry
'Starting The Query
myAdapter.SelectCommand = myCommand
Dim mydata As MySqlDataReader
mydata = myCommand.ExecuteReader()
'To check the Username and password and to validate the login a
If mydata.HasRows = 0 Then
MsgBox("Invalid Login")
txtPassword.Clear()
txtUsername.Clear()
Else
Dim authorityid = 0
While mydata.Read()
authorityid = mydata.GetInt32("authorityid")
End While
MsgBox("Welcome " + txtUsername.Text + "!")
If authorityid = 1 Then
MainForm.Show()
Else
MainForm.Show()
End If
Me.Hide()
End If
End If
Private Sub Login_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
SQLConnection.ConnectionString = ServerString
Try
If SQLConnection.State = ConnectionState.Closed Then
SQLConnection.Open()
Else
SQLConnection.Close()
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
This error is in this line:
mydata = myCommand.ExecuteReader()
What's wrong with this? Any help is truly appreciated.
What's wrong with this?
Well, it looks like you're reusing an existing connection:
myCommand.Connection = SQLConnection
Don't do that. Create a new connection each time you need to talk to the database, and close it when you've finished, using a Using statement to make sure it gets closed even if an exception is thrown.
Additionally, use a Using statement for your command, and another for your reader - these are all resources you should be closing.
Oh, and it also looks like you're doing this in the UI thread, which is a bad idea as your UI will be unresponsive while the database access is ongoing.

Hashing passwords for a login form in vb.net

I have a Login form, I haven't done anything about hashing the password yet, I have been reading about hash here and there yet it really confuses me and don't really know how to implement it in my code for the login form.
Code for hashing I saw
Dim bytes() as byte = System.Text.Encoding.UTF8.GetBytes(stringPassword);
dim hashOfBytes() as byte = new System.Security.Cryptography.SHA1Managed().ComputeHash(bytes)
Dim strHash as string = Convert.ToBase64String(hashOfBytes)
Convert back to bytes
hashOfBytes = Convert.FromBase64String(strHash)
** My Login Form Code**
Using conn As New MySqlConnection("Server = localhost; Username= root; Password =; Database = forms")
Using cmd
With cmd
MsgBox("Connection Established")
.Connection = conn
.Parameters.Clear()
.CommandText = "SELECT * FROM users WHERE BINARY Username = #iUsername AND Password = #iPassword"
.Parameters.Add(New MySqlParameter("#iUsername", txtUser.Text))
.Parameters.Add(New MySqlParameter("#iPassword", txtPass.Text))
End With
Try
conn.Open()
dr = cmd.ExecuteReader()
Catch ex As MySqlException
MsgBox(ex.Message.ToString())
End Try
End Using
End Using
If dr.HasRows = 0 Then
MsgBox("Invalid user")
Conn.Close()
Else
Start.Show()
Conn.Close()
End If
End Sub
You should store the hash value of the password in the Password field of your table.
Then you search for user and the password hash, not directly for the password taken from the input box.
However, your code will still fail because you try to use the MySqlDataReader after the disposing of the connection. Move the check for rows inside the Using block
Dim strHash as string = Convert.ToBase64String(hashOfBytes)
.....
Dim userIsValid as Boolean = False
Using conn As New MySqlConnection(.........)
Using cmd
....
.Parameters.Add(New MySqlParameter("#iPassword", strHashPass))
Try
conn.Open()
dr = cmd.ExecuteReader()
userIsValid = dr.HasRows
Catch ex As MySqlException
MsgBox(ex.Message.ToString())
End Try
End Using
End Using
if userIsValid then
.....
else
.....
End

MySqlDataReader giving error at build

I have a function in VB.NET that authenticates a user towards a MySQL database before launching the main application. Here's the code of the function:
Public Function authConnect() As Boolean
Dim dbserver As String
Dim dbuser As String
Dim dbpass As String
dbserver = My.Settings.dbserver.ToString
dbuser = My.Settings.dbuser.ToString
dbpass = My.Settings.dbpass.ToString
conn = New MySqlConnection
myConnString = "server=" & dbserver & ";" & "user id=" & dbuser & ";" & "password=" & dbpass & ";" & "database=rtadmin"
Dim myCommand As New MySqlCommand
Dim myAdapter As New MySqlDataAdapter
Dim myData As New DataTable
Dim myDataReader As New MySqlDataReader
Dim query As String
myCommand.Parameters.Add(New MySqlParameter("?Username", login_usr_txt.Text))
myCommand.Parameters.Add(New MySqlParameter("?Password", login_pass_txt.Text))
query = "select * from users where user = ?Username and passwd = ?Password"
conn.ConnectionString = myConnString
Try
conn.Open()
Try
myCommand.Connection = conn
myCommand.CommandText = query
myAdapter.SelectCommand = myCommand
myDataReader = myCommand.ExecuteReader
If myDataReader.HasRows() Then
MessageBox.Show("You've been logged in.", "RT Live! Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Catch ex As Exception
End Try
Catch ex As Exception
End Try
End Function
The function is not yet complete, there are a few other things that need to be done before launching the application, since I'm using a MessageBox to display the result of the login attempt.
The error that I'm getting is the following:
Error 1 'MySql.Data.MySqlClient.MySqlDataReader.Friend Sub New(cmd As MySql.Data.MySqlClient.MySqlCommand, statement As MySql.Data.MySqlClient.PreparableStatement, behavior As System.Data.CommandBehavior)' is not accessible in this context because it is 'Friend'. C:\Users\Mario\documents\visual studio 2010\Projects\Remote Techs Live!\Remote Techs Live!\Login.vb 43 13 Remote Techs Live!
Any ideas?
It makes no sense to try to create a MySqlDataReader and then throw it away!
First you do this to attempt to create a reader:
Dim myDataReader As New MySqlDataReader
Then later you attempt to throw that away when you do this:
myDataReader = myCommand.ExecuteReader
Just remove the New from your initial declaration. I suspect that the constructor for the MySqlDataReader is not publicly accessible.
You could try the .AddWithValue instead of .Add.
For example:
cmd.Parameters.AddWithValue("?Username", login_usr_txt.Text)