squestion sanswer vb.net aaa - mysql

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

Related

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?

How to Add an error message box for duplicate id?

this is my code that inputs a record for my database. if I put a duplicate it will just terminate the program. my goal is to put a message box that displays an error so that the program will not terminate.
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
MysqlConn = New MySqlConnection("server=localhost; user=root; pwd=; database=payrollmanagement")
MysqlConn.Open()
command = New MySqlCommand("INSERT INTO emp_info(empID,lastname,firstname,MiddleInitial,Age,address,Position,ContactNumber,pay_type,gender,dept,email) Values ('" & TextBox1.Text & "','" & TextBox5.Text & "','" & TextBox6.Text & "','" & TextBox7.Text & "','" & TextBox11.Text & "','" & TextBox3.Text & "','" & TextBox10.Text & "','" & TextBox4.Text & "','" & TextBox8.Text & "','" & TextBox10.Text & "','" & TextBox2.Text & "','" & TextBox14.Text & "')", MysqlConn)
If (TextBox1.Text = "") Then
MessageBox.Show("Data not Inserted")
ElseIf (TextBox2.Text = "") Then
MessageBox.Show("Data not Inserted")
ElseIf (TextBox3.Text = "") Then
MessageBox.Show("Data not Inserted")
ElseIf (TextBox4.Text = "") Then
MessageBox.Show("Data not Inserted")
ElseIf (TextBox5.Text = "") Then
MessageBox.Show("Data not Inserted")
ElseIf (TextBox6.Text = "") Then
MessageBox.Show("Data not Inserted")
ElseIf (TextBox7.Text = "") Then
MessageBox.Show("Data not Inserted")
ElseIf (TextBox8.Text = "") Then
MessageBox.Show("Data not Inserted")
ElseIf (TextBox9.Text = "") Then
MessageBox.Show("Data not Inserted")
ElseIf (TextBox10.Text = "") Then
MessageBox.Show("Data not Inserted")
ElseIf (TextBox11.Text = "") Then
MessageBox.Show("Data not Inserted")
ElseIf (TextBox14.Text = "") Then
MessageBox.Show("Data not Inserted")
Else
command.ExecuteNonQuery()
MessageBox.Show("Data Inserted")
Shadows_load()
End If
End Sub
Add a MySqlDataReader class so that the program can read if there is any existing data in the database
MysqlConn = New MySqlConnection("//YourConnection")
MysqlConn.Open()
command = New MySqlCommand("//Your sql")
MySqlDataReader reader;
reader = command.ExecuteReader()
If reader.Read() = true Then 'This will solve your problem
MessageBox.Show("There is already existing data.")
Else
If (TextBox1.Text = "") Then
MessageBox.Show("Data not Inserted")
...
Else
command.ExecuteNonQuery()
MessageBox.Show("Data Inserted")
Shadows_load()
End If
End If
If all textboxes are used to insert data, you can loop through textboxes to add error message boxes.
For Each c As Control In Controls
If TypeOf c Is TextBox Then
If c.Text = "" Then
MessageBox.Show("Data not Inserted")
End If
End If
Next
You can also use 'Tag' or 'TabIndex' property to identify each textbox.
First the validation code is run and if it is OK then attempt to insert the new user. The Insert statement will only add the user if the id does not exist.
Private Function ValidateInput() As Boolean
For Each tb In Controls.OfType(Of TextBox)
If tb.Text = "" Then
MessageBox.Show("Data not Inserted")
tb.Focus()
Return False
End If
Next
Return True
End Function
Private Function InsertUser(id As String, lname As String, fname As String, middle As String) As Integer
Dim sql = "INSERT INTO dbusers (empID, lastname, firstname, MiddleInitial)
Select #ID, #LName, #FName #MiddleIn
WHERE Not EXISTS
(SELECT * FROM dbusers WHERE empID = #ID);"
Dim RetVal As Integer
Using cn As New MySqlConnection("server=localhost; user=root; pwd=; database=payrollmanagement"),
cmd As New MySqlCommand(sql, cn)
With cmd.Parameters
.Add("#Id", MySqlDbType.VarChar).Value = id
.Add("#LName", MySqlDbType.VarChar).Value = lname
.Add("#FName", MySqlDbType.VarChar).Value = fname
.Add("#MiddleInitial", MySqlDbType.VarChar).Value = middle
End With
cn.Open()
RetVal = cmd.ExecuteNonQuery()
End Using
Return RetVal
End Function
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
If ValidateInput() Then
Dim RowsEffected = InsertUser(TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text)
If RowsEffected = 1 Then
MessageBox.Show("Successful insert.")
Else
MessageBox.Show("Error with insert.")
End If
End If
End Sub

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..

Cannot update Data in mySQL database

What I want to do is, check first if the ID number exist, then if it exist then do the updating process, but the problem is, it does not update. What is the problem ?
sqlconn = New MySqlConnection
sqlconn.ConnectionString = "server=localhost;userid=root;password='';database=innovative"
Try
sqlconn.Open()
query = "SELECT Full_Name FROM employee WHERE ID='" & txt_id_number.Text & "'"
cmd = New MySqlCommand(query, sqlconn)
reader = cmd.ExecuteReader
If reader.HasRows = False Then
MsgBox("Invalid ID number please secure that the ID number is already Exist" & vbNewLine & "TAKE NOTE:" & vbNewLine & "You cannot update or change the existing ID number for it is the primary Key for the Employee, If you want to Change it, its better to delete the Employee then add it again." & vbNewLine & "Other than that you can change the Full name, age, contact and etc.", vbCritical)
Else
reader.Close()
sqlconn.Open()
query1 = "UPDATE employee SET Full_Name ='" & txt_fullname.Text & "', Employee_Type='" & txt_employee_type.Text & "', Age='" & txt_age.Text & "',Sex='" & cb_sex.Text & "', Status='" & txt_status.Text & "', Contact ='" & txt_contact.Text & "',E_mail='" & txt_email.Text & "' WHERE ID = '" & txt_id_number.Text & "'"
cmd = New MySqlCommand(query1, sqlconn)
reader1 = cmd.ExecuteReader
MsgBox(txt_fullname.Text & " was successfully updated", vbInformation)
txt_age.Text = ""
txt_contact.Text = ""
txt_email.Text = ""
txt_employee_type.Text = ""
txt_fullname.Text = ""
txt_id_number.Text = ""
txt_status.Text = ""
cb_sex.Text = ""
add_employee()
End If
sqlconn.Close()
Catch ex As Exception
Finally
sqlconn.Dispose()
End Try
Imports MySql.Data.MySqlClient
Public Class Form1
Private sqlconn As MySqlConnection
Private query, query1 As String
Private cmd As MySqlCommand
Private reader As MySqlDataReader
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
sqlconn = New MySqlConnection
sqlconn.ConnectionString = "server=localhost;userid=root;password='';database=innovative"
Try
sqlconn.Open()
query = "SELECT Full_Name FROM employee WHERE ID='" & txt_id_number.Text & "'"
cmd = New MySqlCommand(query, sqlconn)
reader = cmd.ExecuteReader
If reader.HasRows = False Then
MsgBox("Invalid ID number please secure that the ID number is already Exist" & vbNewLine & "TAKE NOTE:" & vbNewLine & "You cannot update or change the existing ID number for it is the primary Key for the Employee, If you want to Change it, its better to delete the Employee then add it again." & vbNewLine & "Other than that you can change the Full name, age, contact and etc.", vbCritical)
Else
query1 = "UPDATE employee SET Full_Name = #txt_fullname, Employee_Type=txt_employee_type, Age=#txt_age'"
cmd = New MySqlCommand(query1, sqlconn)
cmd.CommandType = CommandType.Text
cmd.Parameters.Add("#txt_fullname", SqlDbType.VarChar, 255).Value = txt_fullname.Text
cmd.Parameters.Add("#txt_employee_type", SqlDbType.VarChar, 255).Value = txt_employee_type.Text
cmd.Parameters.Add("#txt_age", SqlDbType.VarChar, 255).Value = txt_age.Text
cmd.Parameters.Add("")
cmd.ExecuteNonQuery()
MsgBox(txt_fullname.Text & " was successfully updated", vbInformation)
txt_age.Text = ""
txt_contact.Text = ""
txt_email.Text = ""
txt_employee_type.Text = ""
txt_fullname.Text = ""
txt_id_number.Text = ""
txt_status.Text = ""
cb_sex.Text = ""
add_employee()
End If
sqlconn.Close()
reader.Close()
Catch ex As Exception
Finally
sqlconn.Dispose()
End Try
End Sub
End Class
Three things to change.
Use cmd.ExecuteNonQuery for Insert or Update queries.
Do not use conn.Open again when it is not closed; It returns 'Connection is already open' error and execution will terminate to catch block. This is the reason why your code didnt work.
Parameterize the queries for security and type-casting.
Happy coding!

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