What am I doing wrong here? I get an error:
incorrect syntax near =
on this line of code:
Dim SqlDataReader As SqlClient.SqlDataReader = SQLCommad.ExecuteReader()
My code:
Dim Username = TUserName.Text
Dim Password = TPassword.Text
Dim SqlConnection = New SqlClient.SqlConnection(My.Settings.DBConnection.ToString)
Dim SQLCommand = New SqlClient.SqlCommand("select * from " + My.Settings.TableName.ToString + "Where EmpID = " + Username, SqlConnection)
SqlConnection.Open()
Dim SqlDataReader As SqlClient.SqlDataReader = SQLCommand.ExecuteReader
While (SqlDataReader.Read)
If Username = SqlDataReader(1).ToString And Password = SqlDataReader(20).ToString Then
If eEncrypt(Username, Password) Then
MsgBox("You are NOT logged in")
Else
MsgBox("You are logged in")
End If
End If
End While
SqlConnection.Close()
This should work, see how a parammeter is added
Dim Username = TUserName.Text
Dim Password = TPassword.Text
Dim SqlConnection = New SqlClient.SqlConnection(My.Settings.DBConnection.ToString)
Dim SQLCommand = New SqlClient.SqlCommand("select * from " + My.Settings.TableName.ToString + "Where EmpID = #Username", SqlConnection)
SQLCommand.Parameters.AddWithValue("#Username", Username)
SqlConnection.Open()
Dim SqlDataReader As SqlClient.SqlDataReader = SQLCommand.ExecuteReader()
While (SqlDataReader.Read)
If Username = SqlDataReader(1).ToString And Password = SqlDataReader(20).ToString Then
If eEncrypt(Username, Password) Then
MsgBox("You are NOT logged in")
Else
MsgBox("You are logged in")
End If
End If
End While
SqlConnection.Close()
you miss parenthesis
Dim SqlDataReader As SqlClient.SqlDataReader = SQLCommad.ExecuteReader()
This line of code
Dim SQLCommand = New SqlClient.SqlCommand("select * from " + My.Settings.TableName.ToString + "Where EmpID = " + Username, SqlConnection)
should be
Dim SQLCommand = New SqlClient.SqlCommand("select * from " + My.Settings.TableName.ToString + "Where EmpID = '" + Username + "'", SqlConnection)
posted from mobile.
Related
I was using vb.net and try to update the row of the sourcedata in the gridview which connected to the database.I'm using primary key id in the database.
Below is the related code,This is to generate the primary key ID:
Private Sub GetId()
Dim Id As Integer
Dim query As String
Dim nameid As Integer
query = "select Id from information.information order by Id Desc"
sqlConn.ConnectionString = "server =" + server + ";" + "user id=" + username + ";" _
+ "password=" + password + ";" + "database = " + database
sqlConn.Open()
sqlCmd = New MySqlCommand(query, sqlConn)
sqlRd = sqlCmd.ExecuteReader()
If (sqlRd.Read()) Then
nameid = Integer.Parse(sqlRd(0)) + 1
Id = nameid.ToString("00")
ElseIf (Convert.IsDBNull(sqlRd)) Then
Id = ("01")
Else
Id = ("01")
End If
sqlConn.Close()
txtId.Text = Id
End Sub
This is to update the rows:
Private Sub Editrow()
sqlConn.ConnectionString = "server =" + server + ";" + "user id=" + username + ";" _
+ "password=" + password + ";" + "database = " + database
sqlConn.Open()
sqlCmd.Connection = sqlConn
With sqlCmd
.CommandText = "Update information.information set Name=#Name,EmailAddress=#EmailAddress,PhoneNumber=#PhoneNumber,DOB=#DOB,Address=#Address where Id=#newId"
.CommandType = CommandType.Text
.Parameters.AddWithValue("#newId", txtId)
.Parameters.AddWithValue("#Name", txtName)
.Parameters.AddWithValue("#EmailAddress", txtEmailAddress)
.Parameters.AddWithValue("#PhoneNumber", txtPhoneNumber)
.Parameters.AddWithValue("#DOB", txtDob)
.Parameters.AddWithValue("#Address", txtAddress)
End With
sqlCmd.ExecuteNonQuery()
sqlConn.Close()
updateTable()
'clear txtbox values
txtId.Text = ""
txtName.Text = ""
txtEmailAddress.Text = ""
txtPhoneNumber.Text = ""
txtDob.Text = ""
txtAddress.Text = ""
End Sub
this is to get the rows:
Private Sub Getrow()
Try
txtId.Text = DataGridView1.SelectedRows(0).Cells(0).Value.ToString()
txtName.Text = DataGridView1.SelectedRows(0).Cells(1).Value
txtEmailAddress.Text = DataGridView1.SelectedRows(0).Cells(2).Value
txtPhoneNumber.Text = DataGridView1.SelectedRows(0).Cells(3).Value
txtDob.Text = DataGridView1.SelectedRows(0).Cells(4).Value
txtAddress.Text = DataGridView1.SelectedRows(0).Cells(5).Value
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
And here is the error:
error message
here is my database setting:
database
I am making the following assumption: txtId, txtName, txtEmailAddress, etc. are textboxes on a form.
In your code you currently are passing the Textbox object. I think if you pass the Text property of your textbox (ie txtName.Text) instead it will fix your issue.
With sqlCmd
.CommandText = "Update information.information set Name=#Name,EmailAddress=#EmailAddress,PhoneNumber=#PhoneNumber,DOB=#DOB,Address=#Address where Id=#newId"
.CommandType = CommandType.Text
.Parameters.AddWithValue("#newId", txtId.Text)
.Parameters.AddWithValue("#Name", txtName.Text)
.Parameters.AddWithValue("#EmailAddress", txtEmailAddress.Text)
.Parameters.AddWithValue("#PhoneNumber", txtPhoneNumber.Text)
.Parameters.AddWithValue("#DOB", txtDob.Text)
.Parameters.AddWithValue("#Address", txtAddress.Text)
End With
Im new to visual basic and im currently working on a log in form linked to mysql...i have a code for the log in button but it only gives me an error.This is the code
Private Sub LogIn_btn_Click(sender As Object, e As EventArgs) Handles LogIn_btn.Click
dbconn = New MySqlConnection()
dbconn = New MySqlConnection("Data Source=localhost;user id=root;password=root;database=oeas") With {
.ConnectionString = "Data Source=localhost;user id=root;password=root;database=oeas"
}
sqlcmd = New MySqlCommand()
Try
dbconn.Open()
Const V As String = "select * From oeas.users where User_Name = '" & Pass_txtbx.Text&"' and Password ='" & UserN_txtbx.Text& "' "
query = V
sqlcmd = New MySqlCommand(query, dbconn)
dbrd = sqlcmd.ExecuteReader
Dim count As Integer
count = 0
While dbrd.Read
count = count + 1
End While
If count = 1 Then
MessageBox.Show("username and password are correct")
Profile_form.Show()
Me.Hide()
ElseIf count > 1 Then
MessageBox.Show("username and password are duplicate")
Else
MessageBox.Show("username and password are not correct")
End If
Catch ex As MySqlException
MsgBox("Connection Error: " & ex.Message.ToString)
End Try
End Sub
what might be wrong?
In the above I want it to give me the value of "ID" of the "Username" associated with it.
Here is the command I tried
Dim Query2 = String.Format("SELECT ID FROM account WHERE character = " & UsernameTextBox.Text & " ")
Command = New MySqlCommand(Query2, mydbcon)
Dim idtest = reader.Read.ToString
MsgBox(idtest)
however it is returning "false" what am I doing wrong?
Here is the entire code that I am working on:
Try
mydbcon.Open()
Dim Query As String
Query = String.Format("SELECT * FROM account WHERE username = '{0}' AND password = '{1}'", Me.UsernameTextBox.Text.Trim(), Me.PasswordTextBox.Text.Trim())
Dim Command = New MySqlCommand(Query, mydbcon)
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 are correct")
Query = String.Format("SELECT username AS ID FROM account WHERE character = " & UsernameTextBox.Text & " ")
Command = New MySqlCommand(Query, mydbcon)
My.Settings.LoginID = reader.Read
MsgBox(My.Settings.LoginID)
ElseIf count > 1 Then
MessageBox.Show("Username and password are duplicate")
Else
MessageBox.Show("Username and password are wrong")
End If
mydbcon.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
mydbcon.Dispose()
End Try
Use ID as alias (so select username AS anyNameYouWantToAssignToIt):
Dim Query2 = String.Format("SELECT username AS ID FROM account WHERE character = " & UsernameTextBox.Text & " ")
i'm having an error
there is already an open DataReader associated with this connection which must be closed first.
this is my code.
can you please help me???
thank you
MySqlConnection = New MySqlConnection
MySqlConnection.ConnectionString = "server = localhost; port=3307; user id = root; password = 1234; database = mcs;"
Dim READER As MySqlDataReader
If cmbxyear.Text = "NURSERY" Then
MySqlConnection.Open()
Dim query As String
query = " select student_no from mcs.fullpayment_nursery where student_no = '" & txtstudent_no.Text & "'"
Dim Command As New MySqlCommand(query, MySqlConnection)
READER = Command.ExecuteReader
If query = txtstudent_no.Text Then
MessageBox.Show("YOU DON'T HAVE REMAINING BALANCE")
MySqlConnection.Close()
Else
Dim query1 As String
query1 = " select student_balance from mcs.installmentpayment_nursery where student_no = '" & txtstudent_no.Text & "'"
Dim Command1 As New MySqlCommand(query1, MySqlConnection)
READER = Command.ExecuteReader
READER.Read()
txtstudentbalance.Text = READER(0)
MessageBox.Show("Student Balance Generated")
MySqlConnection.Close()
End If
FYI, I have tables Authority and Users. In my Users table, it has 4 columns namely: user_id, username, password, authority_id and in Authority table, it has 2 columns: authority_id, authority_level.
My login form asks user for their username and password. Upon giving the correct parameters, it should validate the user if it is an admin = 1, nurse = 2, or doctor = 2.
Here's what I've tried so far:
Private Sub btnLogin_Click(sender As System.Object, e As System.EventArgs) Handles btnLogin.Click
If txtUsername.Text = "" Or txtPassword.Text = "" Then
MsgBox("Enter UserName and Password Moron")
Else
'SQL Query To Get The Details
Dim myAdapter As New MySqlDataAdapter
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")
Else
MsgBox("Welcome " + txtUsername.Text + "!")
If authority Then
frmMain.Show()
Else
frmMainNurse.Show()
End If
Me.Hide()
End If
End If
End Sub
Notice that I haven't added something in this line:
If authority Then
I don't know how to do it in vb.net. So if anyone knows how to do this, I'll greatly appreciate your help. Thanks.
Do this in your else part
Else
Dim authorityid = 0
While mydata.Read()
authorityid = mydata.GetInt32("authority_id")
End While
MsgBox("Welcome " + txtUsername.Text + "!")
If authorityid = 1 Then
frmMain.Show()
Else
frmMainNurse.Show()
End If
Me.Hide()
End If