Click here to Skip to main content
15,867,750 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having error like this :

Additional information: Authentication to host '' for user '' using method 'mysql_native_password' failed with message: Access denied for user ''@'user-PC' (using password: NO)


in this code:

VB
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
       Dim user As String = TextBoxUserName.Text
       Dim pass As String = TextBoxPassword.Text

       cn.Open()
       cmd.CommandText = "SELECT * FROM login_table WHERE username= '" & TextBoxUserName.Text & "' and password= '" & TextBoxPassword.Text & "', cn"
       cmd.Connection = cn
       cmd.Parameters.Add(New MySqlParameter("@UserName", TextBoxUserName.Text))
       cmd.Parameters.Add(New MySqlParameter("@Password", TextBoxPassword.Text))
       cmd.Connection = cn
       'cmd.EndExecuteReader = reader

       If reader.HasRows() Then
           MessageBox.Show("Login success!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information)
           'Form1.Show()
           'Me.Close()
           Button1.Show()
           Button2.Show()
           Button5.Show()
           TextBoxPassword.Clear()
       Else

           MsgBox("Invalid Login information.!", MessageBoxButtons.OK, MessageBoxIcon.Information)
       End If
       cn.Close()
Posted
Comments
AndrewCharlz 3-Oct-14 1:49am    
It clearly says that the password or username what u gave was wrong.
NekoNao 3-Oct-14 1:52am    
but it was right. it is from the database. I even checked it before entering the pw and username. It always gets error in "cn.open"
AndrewCharlz 3-Oct-14 2:01am    
did u try to execute that query manually
NekoNao 3-Oct-14 2:23am    
how will i do that?
AndrewCharlz 3-Oct-14 2:29am    
yeah because you have some error in ur connection string and what query editor do u use to test ur query like sqlyog or workbench etc

1 solution

You will need to edit this basic example to reflect your own queries, variables and connection string. If you found this reply helpful, please mark it as a solution. I have pointed out a few things to you in the comments.

This whole line is wrong and one of your problems is the " at the end should be before the , at then end. You should also be using ("Put Query here") blocks.
VB
"SELECT * FROM login_table WHERE username= '" & TextBoxUserName.Text & "' and password= '" & TextBoxPassword.Text & "', cn"


Try this:

VB
Dim SelectID As Integer
Dim UserName As String = "Tom"
Dim iUserGuid As String = "5266-2453-4523-3562" 

'Create your own variable for your parameter placeholder. The above are only examples.  
 
Dim MysqlConn As MySqlConnection 'Creates a MySQL Connection should one be called
Dim MySqlConnectionString As String = "http://www.connectionstrings.com/mysql/" 
'Read up on connection strings from the link i gave you, and put your connection string in this variable above.      
'Don't use these '' symbols, use `` instead.

            Try

                MysqlConn = New MySqlConnection()
                MysqlConn.ConnectionString = MySqlConnectionString
                Query = "SELECT `session` FROM `databasename`.`yourtablehere` WHERE `username` = @username AND `guid` = @guid;" 

'Do not concatenate queries. Use Parameters. 

'Connect your query command to the connection.

                Dim QueryComCheck As New MySqlCommand(Query, MysqlConn) 
                QueryComCheck.Parameters.AddWithValue("@username", UserName)

'@username and @guid is a placeholder for the declared declarations UserName and iUserGuid.  
                QueryComCheck.Parameters.AddWithValue("@guid", iUserGuid)

'Check if the connection is open or not, if its closed, open it... 
'Don't assume something, check it first.  

                If MysqlConn.State = ConnectionState.Closed Then
                    MysqlConn.Open()
                End If
                Using reader As MySqlDataReader = QueryComCheck.ExecuteReader

'Check to see if there are any rows. 

                    If reader.HasRows Then 

'Do While Reader.Read returns false by default if nothing to read.

                        Do While reader.Read() 
'Debug.WriteLine(reader.GetString(1)) 'Write the result to console. 

                            If CBool(reader.GetString("session")) = True Then

'Do your own checking here and handle how you get your data. 

                                SelectID = CInt(reader.GetString("session"))

                            End If

                        Loop

                    End If

                End Using

'By using the reader, you don't need to dispose of the resources of mysql, as the using blocks does this for you. 

            Catch GetSessionFromUser As Exception
'Handle Errors here
            End Try
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900