Mysql won't connect to program VB.net - mysql

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

Related

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

Check username and password in asp.net

I tried a lot, but giving the same problem again and again.
When i am entering correct username and password , "if" block and "catch" block ,both are getting executed at the same time. However ,it is not the case with "else" block.
Here is my code.
Try
conn = New SqlConnection
conn.ConnectionString = "Data Source=hp-hp;Initial Catalog=student;User ID=sa;pwd=student"
conn.Open()
comm = New SqlCommand
comm.Connection = conn
Dim sql As String = "SELECT * FROM intern WHERE username='" & TextBox1.Text & "' AND password = '" & TextBox2.Text & "'"
comm.CommandText = sql
reader = comm.ExecuteReader
If reader.Read() Then
Response.Redirect("Register.aspx")
comm.EndExecuteReader(reader)
conn.Close()
'Response.Redirect("Register.aspx")
Else
' If user enter wrong username and password combination
' Throw an error message
MsgBox("Username and Password do not match..")
'Clear all fields
TextBox1.Text = ""
TextBox2.Text = ""
'Focus on Username field
TextBox1.Focus()
End If
Catch ex As Exception
MsgBox("Failed to connect to Database..")
End Try
Thanks.

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.

Inserting VB.net Label to mysql table

How to Inserting Label.text data into mySql table.
i have no problem with textbox.text but i can't figure out how it with Label.text
i try the same code with textbox.text
parameterB_Answer.Value = TextBox1.Text
it work find but when i try with
parameterB_Answer.Value = Label1.Text
mySqlReader seems can't read it.
Update:
1.1.1 is label1.text. My Idea is to insert the text "1.1.1" from Label1 as Primary key and the Textbox(textbox1.text) as the following
my code is:
Try
Dim StrSQL As String = "INSERT INTO boranga" & _
"(IdBorangA,Answers)" & _
"VALUES (#B_IdA,#B_Answer);"
Dim myCommand As MySqlCommand = New MySqlCommand(StrSQL, conn.open)
myCommand.CommandType = CommandType.Text
Dim parameterB_IdA As MySqlParameter = New MySqlParameter("#B_IdA", MySqlDbType.VarChar, 300)
parameterB_IdA.Value = Label1.Text
Dim parameterB_Answer As MySqlParameter = New MySqlParameter("#B_Answer", MySqlDbType.VarChar, 300)
parameterB_Answer.Value = TextBox1.Text
With myCommand.Parameters
.Add(parameterB_IdA)
.Add(parameterB_Answer)
End With
Dim result As MySqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
MsgBox("Tersimpan", vbYes, "boranga")
Catch SqlEx As MySqlException
Throw New Exception(SqlEx.Message.ToString())
End Try
but when I change the value of Label1.text (1.1.1) to 111, it works just fine. probably because I put INT for the column for label1.text to fill while "1.1.1" isn't integer
thank a lot
PS:seems i can't post image because low of reputation
Try using this code format first of all:
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
Dim conn As MySqlConnection
'Connect to the database using these credentials
conn = New MySqlConnection
conn.ConnectionString = "server=your server site (generally long url); user id=login id for mysql user; password=self explanatory; database=name of the DB you're trying to reach"
'Try and connect (conn.open)
Try
conn.Open()
Catch myerror As MySqlException 'If it fails do this... (i.e. no internet connection, etc.)
MsgBox("Error connecting to database. Check your internet connection.", MsgBoxStyle.Critical)
End Try
'MySQL query (where to call for information)
Dim myAdapter As New MySqlDataAdapter
'Tell where to find the file with the emails/passes stored
Dim sqlquery = "SELECT * FROM the database you selected above WHERE Email = '" & txtEmail.Text & "' AND Password = '" & txtPassword.Text & "'"
Dim myCommand As New MySqlCommand
myCommand.Connection = conn
myCommand.CommandText = sqlquery
'Start query
myAdapter.SelectCommand = myCommand
Dim myData As MySqlDataReader
myData = myCommand.ExecuteReader
If myData.HasRows = 0 Then
MsgBox("Invalid email address or password.", MsgBoxStyle.Critical)
Else
MsgBox("Logged in as " & txtEmail.Text & ".", MsgBoxStyle.Information)
Me.Close()
End If
End Sub
And to insert label.text try replacing one of the textbox.text fields first and see if it will accept it. If it does, then the answer was all in the formatting.
Also, do not forget to call:
imports mysql.data.mysqlclient
and make sure to add the mysql.data reference.

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)