0% found this document useful (0 votes)
83 views3 pages

Delete

This code connects to a MySQL database called "candicequiz1" and allows users to register, delete, and update records in the "RegistrationPart3" table. It includes form controls to collect and display student data. The Button1_Click handler inserts new records. Button2_Click deletes records by student ID. Button3_Click updates existing records based on ID fields. Validation checks for empty fields and verifies records exist before updates or deletes.

Uploaded by

Angel Lyxeia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views3 pages

Delete

This code connects to a MySQL database called "candicequiz1" and allows users to register, delete, and update records in the "RegistrationPart3" table. It includes form controls to collect and display student data. The Button1_Click handler inserts new records. Button2_Click deletes records by student ID. Button3_Click updates existing records based on ID fields. Validation checks for empty fields and verifies records exist before updates or deletes.

Uploaded by

Angel Lyxeia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Imports MySql.Data.

MySqlClient

Public Class Form1


Dim stringcon As String =
"Server=localhost;Database=candicequiz1;Uid=root;Sslmode=none"
Dim con As New MySqlConnection
Dim cmd As New MySqlCommand
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim StudNum1, Email1, Password1, Name2, Course1, Year2 As String
StudNum1 = StudNum.Text
Email1 = Email.Text
Password1 = Password.Text
Name2 = Name1.Text
Course1 = Course.Text
Year2 = Year1.Text

con = New MySqlConnection(stringcon)


Dim sql1 As String = "Select * from RegistrationPart3 where (StudentNumber = '" +
(StudNum.Text) + "')"
Dim sql As String = "Insert into
RegistrationPart3(StudentNumber,Email,Password,Name,Course,Year)
VALUES ('" & StudNum1 & "', '" & Email1 & "', '" & Password1 & "', '" & Name2 &
"', '" & Course1 & "', '" & Year2 & "')"
Dim sqladapter As New MySqlDataAdapter
con.Open()
cmd = New MySqlCommand(sql1, con)
Dim cmd1 = cmd.ExecuteReader()

If Trim(StudNum.Text) = "" Or Trim(Email.Text) = "" Or Trim(Password.Text) = ""


Or Trim(Name1.Text) = "" Or Trim(Course.Text) = "" Or Trim(Year1.Text) = "" Then
MsgBox("Please fill out the indicated boxes.", MsgBoxStyle.Exclamation,
"Error!!")
Exit Sub

ElseIf Not cmd1.HasRows Then


cmd1.Close()
sqladapter.InsertCommand = New MySqlCommand(sql, con)
sqladapter.InsertCommand.ExecuteNonQuery()

sqladapter.Dispose()
MsgBox("Registered", MsgBoxStyle.Information, "Success!!")
Quiz1()
StudNum.Text = ""
Email.Text = ""
Password.Text = ""
Name1.Text = ""
Course.Text = ""
Year1.Text = ""

Else
MsgBox("Information is existing.", MsgBoxStyle.Exclamation, "Error!!")
End If
con.Close()
con.Dispose()

End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Quiz1()
End Sub
Public Sub Quiz1()
con = New MySqlConnection(stringcon)
Dim sql3 As String = "SELECT * FROM RegistrationPart3"
Dim ds As DataSet = New DataSet()

Try
con.Open()
Dim da As MySqlDataAdapter = New MySqlDataAdapter(sql3, con)
da.Fill(ds)
DG.DataSource = ds.Tables(0)
Me.DG.FirstDisplayedScrollingRowIndex = Me.DG.RowCount - 1
Me.DG.Rows(Me.DG.RowCount - 1).Selected = True

Catch ex As Exception
MsgBox(ex.Message)

End Try

End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click


con = New MySqlConnection(stringcon)
Dim sqlselect As String = "Select * from RegistrationPart3 where (StudentNumber =
'" + (TextBox1.Text) + "')"
Dim sqldelete As String = "Delete from RegistrationPart3 where (StudentNumber =
'" + (TextBox1.Text) + "')"
Dim sqladapter As New MySqlDataAdapter
con.Open()
cmd = New MySqlCommand(sqlselect, con)
Dim cmd1 = cmd.ExecuteReader()

If Trim(TextBox1.Text) = "" Then


MsgBox("Please fill out the indicated boxes.", MsgBoxStyle.Exclamation,
"Error!!")
Exit Sub

ElseIf cmd1.HasRows Then


If MessageBox.Show("Are you sure to delete this information?", "DELETE",
MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.No Then
MsgBox("Delete canceled", MsgBoxStyle.Information, "Canceled")
TextBox1.Text = " "

Else
cmd1.Close()
sqladapter.DeleteCommand = New MySqlCommand(sqldelete, con)
sqladapter.DeleteCommand.ExecuteNonQuery()
sqladapter.Dispose()
Quiz1()
MsgBox("Student Information Deleted")
StudNum.Text = " "
End If
Else
MsgBox("Student ID is not existing.")
TextBox1.Text = ""
End If
con.Dispose()
con.Close()
End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click


con = New MySqlConnection(stringcon)
Dim sqlselect As String = "Select * from RegistrationPart3 where (StudentNumber =
'" + (SNU.Text) + "')"
Dim sqlup As String = "Update RegistrationPart3 Set Email=@EU, Password=@PU,
Name=@NU, Course=@CU, Year=@YU where (StudentNumber = '" + (SNU.Text) + "')"
Dim sqladapter As New MySqlDataAdapter
con.Open()
cmd = New MySqlCommand(sqlselect, con)
Dim cmd1 = cmd.ExecuteReader()

If Trim(SNU.Text) = "" Or Trim(EU.Text) = "" Or Trim(PU.Text) = "" Or


Trim(NU.Text) = "" Or Trim(CU.Text) = "" Or Trim(YU.Text) = "" Then
MsgBox("Please fill out the indicated boxes.", MsgBoxStyle.Exclamation,
"Error!!")
Exit Sub

ElseIf cmd1.HasRows Then


If MessageBox.Show("Are you sure to update this information?", "UPDATE",
MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.No Then
MsgBox("Update canceled", MsgBoxStyle.Information, "Canceled")
SNU.Text = " "

Else
cmd1.Close()
cmd = New MySqlCommand(sqlup, con)
Quiz1()
cmd.Parameters.AddWithValue("@EU", EU.Text)
cmd.Parameters.AddWithValue("@PU", PU.Text)
cmd.Parameters.AddWithValue("@NU", NU.Text)
cmd.Parameters.AddWithValue("@CU", CU.Text)
cmd.Parameters.AddWithValue("@YU", YU.Text)
cmd.ExecuteNonQuery()
sqladapter.Dispose()
MsgBox("Student Information Updated")
StudNum.Text = " "
End If
Else
MsgBox("Student ID is not existing.")
SNU.Text = ""
End If
con.Dispose()
con.Close()
End Sub
End Class

You might also like