DataGridView select multiple row, check each row - mysql

I am trying to select all rows by a button or select different rows with ctrl+click.
But even if I have selected multiple rows, it only checks the first row. Then it rereads it again instead of going to the next row.
I'm not sure either where to put the multiselect code.
Originally, you're only supposed to just click a row then check if conditions are satisfied. However it was requested that you can do multiple and check each row if condition is satisfied.
Here is my code:
Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
Dim i As Integer
i = DataGridView1.CurrentRow.Index
_subjectno = DataGridView1.Item(0, i).Value
_title = DataGridView1.Item(1, i).Value
_unit = DataGridView1.Item(2, i).Value
_pre = DataGridView1.Item(3, i).Value
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
DataGridView1.MultiSelect = True
DataGridView1.SelectAll()
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim selectedItems As DataGridViewSelectedRowCollection = DataGridView1.SelectedRows
For Each selectedItem As DataGridViewRow In selectedItems
add()
Next
End Sub
Sub add()
Try
If IS_EMPTY(txtSno) = True Then Return
cm = New MySqlCommand("select * from tblenroll where subjectnumber like '" & _subjectno & "' and remarks <> 'Failed' and studentno like '" & txtSno.Text & "'", cn)
dr = cm.ExecuteReader
dr.Read()
If dr.HasRows Then
MsgBox("Subject is already taken.", vbExclamation)
dr.Close()
Return
Else
dr.Close()
End If
If _pre = "NONE" Or _pre = "2ND YR STANDING" Or _pre = "3RD YR STANDING" Or _pre = "4TH YR STANDING" Or _pre = "5TH YR STANDING" Then
cm = New MySqlCommand("insert into tblenroll (studentno, subjectnumber, ay,semester,dateenrolled, curriculum) values ('" & txtSno.Text & "','" & _subjectno & "','" & txtAY.Text & "','" & txtSem.Text & "','" & Now & "','" & txtCurriculum.Text & "')", cn)
cm.ExecuteNonQuery()
MsgBox(_subjectno & " successfully added.", vbInformation)
LoadEnrolled()
Else
cm = New MySqlCommand("select * from tblenroll where studentno like '" & txtSno.Text & "' and subjectnumber like '" & _subjectno & "' and ay like '" & txtAY.Text & "'", cn)
dr = cm.ExecuteReader
dr.Read()
If dr.HasRows Then
MsgBox("Subject is already taken.", vbExclamation)
dr.Close()
Return
Else
dr.Close()
End If
Dim strok As Boolean = False
Dim strArr() As String
Dim count As Integer
Dim strpre As String = _pre
strArr = strpre.Split(", ")
For count = 0 To strArr.Length - 1
cm = New MySqlCommand("select * from tblenroll as e inner join tblsubject as s on e.subjectnumber = s.subjectno where s.subjectno like '%" & Trim(strArr(count)) & "%' and studentno like '" & txtSno.Text & "' and remarks like 'Passed'", cn)
dr = cm.ExecuteReader
dr.Read()
If dr.HasRows Then
dr.Close()
strok = True
Else
MsgBox("Unable to enroll this subject. Pre-requisite " & strArr(count) & ".", vbExclamation)
dr.Close()
Return
End If
Next
If strok = True Then
cm = New MySqlCommand("insert into tblenroll (studentno, subjectnumber, ay, semester, dateenrolled, curriculum) values ('" & txtSno.Text & "','" & _subjectno & "','" & txtAY.Text & "','" & txtSem.Text & "','" & Now & "','" & txtCurriculum.Text & "')", cn)
cm.ExecuteNonQuery()
MsgBox(_subjectno & " successfully added.", vbInformation)
LoadEnrolled()
End If
End If
Catch ex As Exception
MsgBox(ex.Message, vbCritical)
End Try
End Sub
Thank you so much!

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
For Each selectedItem As DataGridViewRow In DataGridView1.SelectedRows
_subjectno = selectedItem.Cells(0).Value
_title = selectedItem.Cells(1).Value
_unit = selectedItem.Cells(2).Value
_pre = selectedItem.Cells(3).Value
add()
Next
End Sub

Related

please what's the cause of this error in visual basic

System.Data.SqlClient.SqlException: 'An explicit value for the identity column in table 'table1' can only be specified when a column list is used and IDENTITY_INSERT is ON.'
when i run the code it displays the error below
this is what pops up
**this is my code**
Imports System.Data.SqlClient
Public Class ARRIVAL
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
con.ConnectionString = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\PRINCE\Documents\real.mdf;Integrated Security=True;Connect Timeout=30"
If con.State = ConnectionState.Open Then
con.Close()
End If
con.Open()
disp_data()
End Sub
Private Sub insert_Click(sender As Object, e As EventArgs) Handles insert.Click
cmd = con.CreateCommand()
cmd.CommandType = CommandType.Text
cmd.CommandText = "insert into table1 values('" & DateTimePicker1.Text & "','" & shiftsupervisorcb.SelectedItem.ToString() & "','" & teammemberscb.SelectedItem.ToString() & "','" & airlinecb.SelectedItem.ToString() & "','" & DateTimePicker2.Text & "','" & conternocb.SelectedItem.ToString() & "','" + TextBox2.Text + "','" + TextBox3.Text + "','" & linemanagercb.SelectedItem.ToString() & "','" & okimechcb.SelectedItem.ToString & "','" & okiribboncb.SelectedItem.ToString() & "','" + ocrcb.SelectedItem.ToString() & "','" & mousecb.SelectedItem.ToString() & "' ,'" & okiprinhcb.SelectedItem.ToString() & "','" & atbcb.SelectedItem.ToString() & "','" & btpcb.SelectedItem.ToString() & "','" & bgrcb.SelectedItem.ToString() & "')"
cmd.ExecuteNonQuery()
disp_data()
MessageBox.Show("record inserted successfully")
End Sub
Public Sub disp_data()
cmd = con.CreateCommand()
cmd.CommandType = CommandType.Text
cmd.CommandText = "select * from table1"
cmd.ExecuteNonQuery()
Dim dt As New DataTable()
Dim da As New SqlDataAdapter(cmd)
da.Fill(dt)
DataGridView1.DataSource = dt
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Me.Hide()
Dim back = New BORDING
back.Show()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
End Sub
Private Sub bgrcb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles bgrcb.SelectedIndexChanged
End Sub
End Class

I want to insert and retrieve data under one button event in vb.net with sql database

Here i am trying to register student's detail. and at the same time i want to retrieve a unique user id from connected SQL server and that's where i got stuck!
I want to know what i am doing wrong here!
Everything stored in the SQL server is correct and i have make an auto increment code in SQL!
so why its not inserting and retrieving the data simultaneously
Imports MySql.Data.MySqlClient
Public Class Form3
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim con As New MySqlConnection("server=localhost;user id=root;password=root;persistsecurityinfo=True;database=bcaproject")
Dim cmd As New MySqlCommand
If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Or TextBox3.Text = "" Or TextBox4.Text = "" Or TextBox5.Text = "" Or TextBox6.Text = "" Then
MessageBox.Show("Information shouldn't be empty...")
Else
con.Open()
cmd.Connection = con
cmd.CommandText = "insert into student(F_name,L_name,Email,Phoneno,Username,Password) values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "','" & TextBox6.Text & "')"
cmd.ExecuteNonQuery()
MessageBox.Show("successfully Registered")
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
Form2.Show()
Me.Hide()
End If
End Sub
Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Form2.Show()
Me.Hide()
End Sub
Private Sub TextBox4_KeyPress1(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox4.KeyPress
If Asc(e.KeyChar) <> 8 Then
If Not IsNumeric(e.KeyChar) Then
tipvalidation.Show("Numeric input required", sender, 5000)
e.KeyChar = Nothing
End If
End If
End Sub
End Class

INSERT INTO in MySQL not working but no errors?

I somehow can't insert data into my MySQL database but I know there's no trouble with the query cause there is no error message and it can make it as far as the Success message box. I think the query is right for MySQL but it is not the specific one that I should use for INSERT INTO.
Here's my code:
Imports MySql.Data.MySqlClient
Public Class Register
Dim ServerString As String = "Server=localhost; UserId =root; Password = ; Database = gym;"
Dim MysqlConn As MySqlConnection = New MySqlConnection
Dim COMMAND As MySqlCommand
Dim password, pass As String
Dim member As Integer
Private Sub Register_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.CenterToParent()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MysqlConn.ConnectionString = ServerString
Dim READER As MySqlDataReader
password = TextBox2.Text
pass = TextBox3.Text
confirm(password, pass)
If TextBox1.Text = Nothing Or TextBox2.Text = Nothing Or TextBox3.Text = Nothing Or TextBox4.Text = Nothing Or TextBox5.Text = Nothing Or TextBox6.Text = Nothing Or DateTimePicker1.Text = Nothing Or RadioButton1.Checked = False And RadioButton2.Checked = False Then
MsgBox("Please Fill your Information Completely")
Else
MysqlConn.ConnectionString = ServerString
Try
MysqlConn.Open()
Dim query As String
query = "select * from gym.user where user_name ='" & TextBox1.Text & "'"
COMMAND = New MySqlCommand(query, MysqlConn)
READER = COMMAND.ExecuteReader
Dim count As Integer
While READER.Read
count = count + 1
End While
MysqlConn.Close()
If count > 0 Then
MsgBox("User Already Exists")
Else
MysqlConn.Open()
query = "INSERT INTO gym.user(user_name,user_password,user_firstname,user_lastname,user_birthday,user_contact,user_membership) VALUES ('" & TextBox1.Text & "', md5('" & TextBox2.Text & "') ,'" & TextBox4.Text & "','" & TextBox5.Text & "','" & DateTimePicker1.Value.Date & "','" & TextBox6.Text & "','" & member & "')"
COMMAND = New MySqlCommand(query, MysqlConn)
MsgBox("USER REGISTERED")
MysqlConn.Close()
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
MysqlConn.Dispose()
End Try
End If
End Sub
You're missing ExecuteNonQuery statement:
query = "INSERT INTO gym.user(user_name,user_password,user_firstname,user_lastname,user_birthday,user_contact,user_membership) VALUES ('" & TextBox1.Text & "', md5('" & TextBox2.Text & "') ,'" & TextBox4.Text & "','" & TextBox5.Text & "','" & DateTimePicker1.Value.Date & "','" & TextBox6.Text & "','" & member & "')"
COMMAND = New MySqlCommand(query, MysqlConn)
COMMAND.ExecuteNonQuery()

Select Value then Insert To Mysql

I'm trying to insert a value from a selected value. If the row fetched is equal to 1 or greater than 0 then It will insert the data to another table.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Try
con.Open()
sql = "SELECT user_id, username, password FROM tbl_user WHERE username = '" & txt_user.Text & "' AND password= '" & txt_pass.Text & "' IS NOT NULL"
cmd = New MySqlCommand(sql, con)
dr = cmd.ExecuteReader
While dr.Read
txt_user.Text = dr("username")
txt_pass.Text = dr("password")
Main.Show()
Me.Hide()
ctr += 1
End While
If ctr = 0 Then
MsgBox("Login Failed!")
txt_user.Clear()
txt_pass.Clear()
Else
sql = "INSERT INTO user_log(user_id, username, log_datetime)VALUES(" & dr(0) & ",'" & dr(1) & "','" & DateTime.Today & "')"
cmd2 = New MySqlCommand(sql, con)
cmd2.ExecuteNonQuery()
End If
dr.Close()
cmd.Dispose()
con.Close()
' Catch ex As Exception
' End Try
End Sub

error database: Syntax error in INSERT INTO statement

I'm having difficulties with my library system. I get an error that states "error database: Syntax error in INSERT INTO statement". I'm using Access for the data base.
Here's my code:
Public Class Form7
Private Sub Form7_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
Me.Text = "Registration"
sqlCon.Close()
sqlCon.Close()
clear()
End Sub
Private Sub Form7_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Split(Me.Text, " - ")(1) = "Edit" Then
sqlSTR = "Select * From BookRegister WHERE id = " & globalID
ExecuteSQLQuery(sqlSTR)
If sqlDr.Read Then
TextBox1.Text = sqlDr(1)
TextBox2.Text = sqlDr(2)
TextBox3.Text = sqlDr(3)
TextBox4.Text = sqlDr(4)
TextBox5.Text = sqlDr(5)
TextBox6.Text = sqlDr(6)
TextBox7.Text = sqlDr(7)
TextBox7.Text = sqlDr(8)
End If
End If
End Sub
Private Sub TextBox6_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox6.TextChanged
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text = "" Then
MsgBox("Please fill up all the information", MsgBoxStyle.Information)
ElseIf TextBox2.Text = "" Then
MsgBox("Please fill up all the information", MsgBoxStyle.Information)
ElseIf TextBox3.Text = "" Then
MsgBox("Please fill up your information", MsgBoxStyle.Information)
ElseIf TextBox4.Text = "" Then
MsgBox("Please fill up your information", MsgBoxStyle.Information)
ElseIf TextBox5.Text = "" Then
MsgBox("Please fill up your information", MsgBoxStyle.Information)
ElseIf TextBox6.Text = "" Then
MsgBox("Please fill up your information", MsgBoxStyle.Information)
ElseIf TextBox7.Text = "" Then
MsgBox("Please fill up all the information", MsgBoxStyle.Information)
ElseIf TextBox8.Text = "" Then
MsgBox("Please fill up all the information", MsgBoxStyle.Information)
ElseIf Split(Me.Text, " - ")(1) = "Add" Then
sqlSTR = "INSERT INTO BookRegister(ISBN, Title, Author, Volume, Pages, Publisher, Section, Year_Published) " & _
"VALUES('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "'" & _
",'" & TextBox6.Text & "','" & TextBox7.Text & "','" & TextBox8.Text & ")"""
ExecuteSQLQuery(sqlSTR)
MsgBox("New record has been saved.", MsgBoxStyle.Information)
clear()
Else
sqlSTR = "UPDATE BookRegister SET ISBN ='" & TextBox1.Text & "', " & _
"title='" & TextBox2.Text & "', " & _
"author='" & TextBox3.Text & "', " & _
"volume='" & TextBox4.Text & "', " & _
"pages='" & TextBox5.Text & "', " & _
"publisher='" & TextBox6.Text & "', " & _
"section='" & TextBox7.Text & "', " & _
"year_published='" & Format("yyyy", TextBox8.Text) & "', " & _
" WHERE id =" & globalID
ExecuteSQLQuery(sqlSTR)
MsgBox("Record has been updated !!", MsgBoxStyle.Information)
clear()
globalID = 0
edit = False
Me.Close()
End If
FillListView(Form6.ListView1, ExecuteSQLQuery("select * from bookregister"))
End Sub
Private Sub clear()
Textbox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
TextBox7.Text = ""
TextBox8.Text = ""
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
End Class
It looks like you are missing a final single quote at the end of your INSERT statement, and then appending an extraneous double quote after the closing parenthesis, i.e.,
& "','" & TextBox8.Text & ")"""
should be
& "','" & TextBox8.Text & "')"