Preventing duplicate entries of username - duplicates

Private Sub APMSubmitBTN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles APMSubmitBTN.Click
con = New OleDbConnection(cs)
con.Open()
If txt_appmngr_username.Text = "" Then
MessageBox.Show("Please enter Employee Name", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
txt_appmngr_username.Focus()
Exit Sub
End If
If txt_appmngr_password.Text = "" Then
MessageBox.Show("Please enter password", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
txt_appmngr_password.Focus()
Exit Sub
End If
If chck_box_admin.Checked Then
Dim sqlCheck As New OleDbCommand
Dim cb As String = "INSERT INTO Admin_Account([Admin_User],[Admin_Pass]) values (#a1,#a2)"
cmd = New OleDbCommand(cb, con)
'CHECKING WETHER THE USERNAME IS ALREADY TAKEN BY OTHER ADMINISTRATOR'
sqlCheck.CommandText = "SELECT count(*) FROM Admin_Account WHERE Admin_User = #1"
sqlCheck.Connection = con
cmd.Parameters.AddWithValue("#a1", txt_appmngr_username.Text)
Dim oleRdr As OleDbDataReader = sqlCheck.ExecuteReader
If oleRdr.HasRows = True Then
oleRdr.Read()
If oleRdr.Item(0) = 0 Then
MessageBox.Show("Username is available", "Registration", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("Username is unavailable", "Registration", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End If
cmd.Parameters.AddWithValue("#a2", txt_appmngr_password.Text)
cmd.ExecuteReader()
MessageBox.Show("Successfully registered as Admin", "Registration", MessageBoxButtons.OK, MessageBoxIcon.Information)
con.Close()
ElseIf chck_box_user.Checked Then
Dim sqlCheck As New OleDbCommand
Dim cb As String = "INSERT INTO Staff_Account([Staff_User],[Staff_Pass]) values (#s1,#s2)"
cmd = New OleDbCommand(cb, con)
'CHECKING WETHER THE USERNAME IS ALREADY TAKEN BY OTHER STAFF'
sqlCheck.CommandText = "SELECT count(*) FROM Staff_Account WHERE Staff_User = #s1"
sqlCheck.Connection = con
cmd.Parameters.AddWithValue("#s1", txt_appmngr_username.Text)
Dim oleRdr1 As OleDbDataReader = sqlCheck.ExecuteReader
If oleRdr1.HasRows = True Then
oleRdr1.Read()
If oleRdr1.Item(0) = 0 Then
MessageBox.Show("Username is available", "Registration", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("Username is unavailable", "Registration", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End If
cmd.Parameters.AddWithValue("#s2", txt_appmngr_password.Text)
cmd.ExecuteReader()
MessageBox.Show("Successfully registered as Staff", "Registration", MessageBoxButtons.OK, MessageBoxIcon.Information)
con.Close()
End If
End Sub
Im getting error on the line of Dim oleRdr As OleDbDataReader = sqlCheck.ExecuteReader of both 'user and admin accounts. when i run a program.
The error said "No value given for one or more required parameters."

Related

"System.Windows.Forms.PlaceholderTextBox, Text: " Error when inserting data in MySQL database using myPHPAdmin

Am trying to capture data from textbox to save it to mysql database but the field is System.Windows.Forms.PlaceholderTextBox, Text: Adm
This is my Buttonsave click event save
Dim theFirstname As String = firstName.Text
type here
Dim theEmail As String = emailCom.Text
Dim username As String = userName2.Text
Dim thePassword As String = passWord2.Text
If theFirstname.Trim() = "" Or theEmail.Trim() = "" Or username.Trim() = "" Or thePassword.Trim() = "" Then
MessageBox.Show("One Or More Fields Are Empty", "Missing Data", MessageBoxButtons.OK, MessageBoxIcon.Stop)
ElseIf Usernamexist(username) Then
MessageBox.Show("This Username Already Exists, Choose Another One", "Duplicate Username", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
connection.Open()
Dim command As New MySqlCommand("INSERT INTO authentic (firstname, email, username, password) VALUES (#firstname,#email,#username,#password)", connection)
command.Parameters.Add("#firstname", MySqlDbType.VarChar).Value = firstName
command.Parameters.Add("#email", MySqlDbType.VarChar).Value = emailCom
command.Parameters.Add("#username", MySqlDbType.VarChar).Value = userName2
command.Parameters.Add("#password", MySqlDbType.VarChar).Value = passWord2
connection.Open()
MessageBox.Show("Registration Completed Successfully", "User Added", MessageBoxButtons.OK, MessageBoxIcon.Information)
connection.Close()
If command.ExecuteNonQuery() = 1 Then
MessageBox.Show("Registration Completed Successfully", "User Added", MessageBoxButtons.OK, MessageBoxIcon.Information)
connection.Close()
Else
MessageBox.Show("Something Happen", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
connection.Close()
End If
firstName.Text = ""
emailCom.Text = ""
userName2.Text = ""
passWord2.Text = ""
accLogin.Visible = True
createAcc.Visible = False
End If
This is what am getting at the moment
anyone with an idea how to go about this?
Faithfully
Moe
You had the answer yourself, in the first few lines of your code.
Where you set the values in the parameters, none of the lines are using the value, they are getting the object.
command.Parameters.Add("#firstname", MySqlDbType.VarChar).Value = firstName
command.Parameters.Add("#email", MySqlDbType.VarChar).Value = emailCom
command.Parameters.Add("#username", MySqlDbType.VarChar).Value = userName2
command.Parameters.Add("#password", MySqlDbType.VarChar).Value = passWord2
Change to:
command.Parameters.Add("#firstname", MySqlDbType.VarChar).Value = firstName.Text
command.Parameters.Add("#email", MySqlDbType.VarChar).Value = emailCom.Text
command.Parameters.Add("#username", MySqlDbType.VarChar).Value = userName2.Text
command.Parameters.Add("#password", MySqlDbType.VarChar).Value = passWord2.Text
Each of the lines has a .Text at the end to extract the value of the object.

VB Log In button linked to mysq

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?

Visual Basic No Data Exist for the row/column

I've been searching for many references on this problem and I have nothing.. It's still have the same error, when i do login, then it will not having any problem, and after a while, the error will be shown so many times.
Call Koneksi()
Try
cmd = New Odbc.OdbcCommand("SELECT * FROM tb_user WHERE username = '" & txt_username.Text & "' AND password = '" & txt_password.Text & "'", conn)
rd = cmd.ExecuteReader
rd.Read()
If rd.HasRows = True Then
txt_akses.Text = rd!hak_akses
MsgBox("Welcome '" & rd!nama & "'", MsgBoxStyle.Exclamation, "Information")
MenuCustomer.txt_user.Text = rd!username
cmd = New Odbc.OdbcCommand("SELECT * FROM tb_permainan WHERE status = 1", conn)
rd = cmd.ExecuteReader
rd.Read()
If txt_akses.Text = "Customer" Then
If rd.HasRows = False Then
txt_username.Text = ""
txt_password.Text = ""
txt_akses.Text = ""
MenuCustomer.Show()
Else
MsgBox("Permainan sedang berlangsung. Mohon Untuk login sesaat lagi", MsgBoxStyle.Exclamation, "Information")
End If
Else
MenuStaff.Show()
End If
Else
txt_username.Text = ""
txt_password.Text = ""
txt_akses.Text = ""
txt_username.Select()
MsgBox("Username atau password salah", MsgBoxStyle.Exclamation, "Information")
End If
rd.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
And here is my module named koneksi
Imports System.Data.Odbc
Module CRUD
'Setting Koneksi
Public conn As OdbcConnection
Public cmd As OdbcCommand
Public ds As New DataSet
Public da As OdbcDataAdapter
Public rd As OdbcDataReader
Public dt As New DataTable
Public LokasiData As String
Public result As String
Sub Koneksi()
LokasiData = "Driver={MySQL ODBC 3.51 Driver};Database=db_bubble;server=192.168.1.14;uid=root"
conn = New OdbcConnection(LokasiData)
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
End Sub
The DataReader.HasRows property check must precede the DataReader.Read() method, which should be enclosed in While loop because DataReader is forward-only stream (checking row availability immediately after ExecuteReader()). This example below is the correct way:
Try
cmd = New Odbc.OdbcCommand("SELECT * FROM tb_user WHERE username = #UserName AND password = #Password", conn)
cmd.Parameters.AddWithValue("#UserName", txt_username.Text)
cmd.Parameters.AddWithValue("#Password", txt_password.Text)
Using rd As OdbcDataReader = cmd.ExecuteReader()
If rd.HasRows = True Then
While rd.Read()
txt_akses.Text = rd!hak_akses
MsgBox("Welcome '" & rd!nama & "'", MsgBoxStyle.Exclamation, "Information")
MenuCustomer.txt_user.Text = rd!username
cmd2 = New Odbc.OdbcCommand("SELECT * FROM tb_permainan WHERE status = 1", conn)
Using rd2 As OdbcDataReader = cmd2.ExecuteReader()
If rd2.HasRows = True Then
While rd2.Read()
If txt_akses.Text = "Customer" Then
If rd.HasRows = False Then
txt_username.Text = ""
txt_password.Text = ""
txt_akses.Text = ""
MenuCustomer.Show()
Else
MsgBox("Game is in progress. Please login for a while", MsgBoxStyle.Exclamation, "Information")
End If
Else
MenuStaff.Show()
End If
End While
End If
End Using
End While
Else
txt_username.Text = ""
txt_password.Text = ""
txt_akses.Text = ""
txt_username.Select()
MsgBox("Incorrect username or password", MsgBoxStyle.Exclamation, "Information")
End If
End Using
Catch ex As Exception
MsgBox(ex.Message)
End Try
Note that you should use different DataReader instances for this case, because in current code the second DataReader assignment overwrites the first. Also use parameters inside query to prevent SQL injection attempt.
I have modified my code like this, and it still show error
Using rd As OdbcDataReader = cmd.ExecuteReader
If rd.HasRows = True Then
While rd.Read()
txt_akses.Text = rd!hak_akses
MsgBox("Welcome '" & rd!nama & "'", MsgBoxStyle.Exclamation, "Information")
MenuCustomer.txt_user.Text = rd!username
If txt_akses.Text = "Customer" Then
cmd2 = New OdbcCommand("SELECT * FROM tb_permainan WHERE status = 1", conn)
Using rd2 As OdbcDataReader = cmd2.ExecuteReader
If rd2.HasRows = True Then
MsgBox("Permainan sedang berlangsung. Mohon Untuk login sesaat lagi", MsgBoxStyle.Exclamation, "Information")
Else
While rd2.Read()
txt_username.Text = ""
txt_password.Text = ""
txt_akses.Text = ""
MenuCustomer.Show()
End While
End If
End Using
Else
MenuStaff.Show()
End If
End While
Else
txt_username.Text = ""
txt_password.Text = ""
txt_akses.Text = ""
txt_username.Select()
MsgBox("Username atau password salah", MsgBoxStyle.Exclamation, "Information")
End If
End Using
The error shown is looping..

squestion sanswer vb.net aaa

MysqlConn.Open()
command.Connection = MysqlConn
command.CommandType = CommandType.Text
command.CommandText = "select securityq from admin.customer where securityq = #secuq"
With command.Parameters
.AddWithValue("#secuq", Label2.Text)
End With
READER = command.ExecuteReader
If READER.HasRows Then
command.CommandText = "select securitya from admin.customer where securitya = #secuqa"
With command.Parameters
.AddWithValue("#secuqa", TextBox2.Text)
End With
End If
If Label2.Text = TextBox2.Text Or TextBox2.Text = Label2.Text Then
Panel1.Visible = False
Panel2.Visible = False
Panel3.Visible = True
READER.Close()
Else
MessageBox.Show(" Incorrect Answer! Please try again. " & count & " tries left")
TextBox2.Clear()
Count -= 1
End If
Count is working but security question and security answer matching is not working
If Count = 0 Then
MessageBox.Show(" Please contact Admin", " Error! ", MessageBoxButtons.OK, MessageBoxIcon.Error)
Login.Show()
Me.Hide()
End If

How to fix when I'm editing a data in datagridview in another form all the content is being cleared?

Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBtn.Click
'Try
If Not IsNumeric(txtUnit.Text) Then
MsgBox("Enter Unit Number.", MsgBoxStyle.Critical, "LASAC")
Exit Sub
ElseIf txtName.Text = "" Then
MsgBox("Enter Name.", MsgBoxStyle.Critical, "LASAC")
Exit Sub
End If
If DataGridView1.SelectedRows.Count = 0 Then
conn.ConnectionString = constring
conn.Open()
sSQL = "INSERT INTO tblBilling(Unit,FullName) VALUES(" & CInt(txtUnit.Text) & ",'" & Replace(txtName.Text, "'", "''") & "')"
cmd = New OleDbCommand(sSQL, conn)
cmd.ExecuteReader()
conn.Close()
MsgBox("Sucessfully added", MsgBoxStyle.Information, "New")
NewBtn.PerformClick()
Else
conn.ConnectionString = constring
conn.Open()
sSQL = "UPDATE tblBilling SET FullName = '" & Replace(txtName.Text, "'", "''") & "', Unit = " & CInt(txtUnit.Text) & " WHERE 'ID = '" & DataGridView1.SelectedRows(0).Cells(0).Value & ""
cmd = New OleDbCommand(sSQL, conn)
cmd.ExecuteReader()
conn.Close()
MsgBox("Successfully Updated", MsgBoxStyle.Information, "Update")
End If
DataGridView1.Rows.Clear()
showmember()
txtName.Enabled = False
txtUnit.Enabled = False
'Catch ex As Exception
' MsgBox(ex.Message, MsgBoxStyle.Critical, "Save")
'End Try
End Sub
Private Sub NewBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewBtn.Click
txtName.clear()
txtUnit.Clear()
txtUnit.Enabled = True
txtName.Enabled = True
DataGridView1.ClearSelection()
End Sub
Private Sub DeleteBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteBtn.Click
If DataGridView1.SelectedRows.Count <> 0 Then
If MsgBox("You are deleting a confidential data?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Statement Deleting") = MsgBoxResult.Yes Then
conn.ConnectionString = constring
conn.Open()
'MsgBox(DataGridView1.SelectedRows(0).Cells(0).Value)
sSQL = "DELETE FROM tblBilling WHERE ID = " & DataGridView1.SelectedRows(0).Cells(0).Value & ""
cmd = New OleDbCommand(sSQL, conn)
cmd.ExecuteReader()
conn.Close()
'MsgBox("Data successfully delete", MsgBoxStyle.Information, "DELETE")
DataGridView1.Rows.RemoveAt(DataGridView1.SelectedRows(0).Index)
NewBtn.PerformClick()
'Else
' MsgBox("Delete Canceled")
End If
End If
showmember()
txtName.Enabled = False
txtUnit.Enabled = False
End Sub
You have a code to clear all your rows, but didn't to retrieve the data bank
Else
conn.ConnectionString = constring
conn.Open()
sSQL = "UPDATE tblBilling SET FullName = '" & Replace(txtName.Text, "'", "''") & "', Unit = " & CInt(txtUnit.Text) & " WHERE 'ID = '" & DataGridView1.SelectedRows(0).Cells(0).Value & ""
cmd = New OleDbCommand(sSQL, conn)
cmd.ExecuteReader()
conn.Close()
MsgBox("Successfully Updated", MsgBoxStyle.Information, "Update")
End If
' you have clear all your rows here
DataGridView1.Rows.Clear()
showmember()
txtName.Enabled = False
txtUnit.Enabled = False
'Catch ex As Exception
' MsgBox(ex.Message, MsgBoxStyle.Critical, "Save")
'End Try