You are on page 1of 3

Imports System.Data.

SqlClient

Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


Dim Id As Integer = TextBox1.Text
Dim Name As String = TextBox2.Text
Dim Address As String = TextBox3.Text
Dim City As String = ComboBox1.Text
Dim Age As Integer = TextBox4.Text
Dim Sex As String = ""
If RadioButton1.Checked = True Then
Sex = RadioButton1.Text
Else
Sex = RadioButton2.Text
End If
Dim query As String = "insert into StudentInfo_tb
values(@Id,@name,@Address,@City,@Age,@Sex)"
Using con As SqlConnection = New SqlConnection("Data Source=DESKTOP-
L4MA6U1;Initial Catalog=StudentInfo_Db;Integrated Security=True")
Using cmd As SqlCommand = New SqlCommand(query, con)
cmd.Parameters.AddWithValue("@Id", Id)
cmd.Parameters.AddWithValue("@Name", Name)
cmd.Parameters.AddWithValue("@Address", Address)
cmd.Parameters.AddWithValue("@City", City)
cmd.Parameters.AddWithValue("@Age", Age)
cmd.Parameters.AddWithValue("@Sex", Sex)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
MessageBox.Show("Successfuly added")
BindData()
End Using
End Using
End Sub
Private Sub BindData()
Dim query As String = "select* from StudentInfo_tb"
Using con As SqlConnection = New SqlConnection("Data Source=DESKTOP-
L4MA6U1;Initial Catalog=StudentInfo_Db;Integrated Security=True")
Using cmd As SqlCommand = New SqlCommand(query, con)
Using da As New SqlDataAdapter()
da.SelectCommand = cmd
Using dt As New DataTable()
da.Fill(dt)
DataGridView1.DataSource = dt
End Using
End Using
End Using
End Using
End Sub

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


Dim query As String = "select * from StudentInfo_tb where Id='" & TextBox1.Text &
"'"
Using con As SqlConnection = New SqlConnection("Data Source=DESKTOP-
L4MA6U1;Initial Catalog=StudentInfo_Db;Integrated Security=True")
Using cmd As SqlCommand = New SqlCommand(query, con)
Using da As New SqlDataAdapter()
da.SelectCommand = cmd
Using dt As New DataTable()
da.Fill(dt)
If dt.Rows.Count > 0 Then
TextBox2.Text = dt.Rows(0)(1).ToString()
TextBox3.Text = dt.Rows(0)(2).ToString()
ComboBox1.Text = dt.Rows(0)(3).ToString()
TextBox4.Text = dt.Rows(0)(4).ToString()
If dt.Rows(0)(5).ToString() = "male" Then
RadioButton1.Checked = True
Else
RadioButton2.Checked = True
End If
Else
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
End If
End Using
End Using
End Using
End Using
End Sub

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


Dim Id As Integer = TextBox1.Text
Dim Name As String = TextBox2.Text
Dim Address As String = TextBox3.Text
Dim City As String = ComboBox1.Text
Dim Age As Integer = TextBox4.Text
Dim Sex As String = ""
If RadioButton1.Checked = True Then
Sex = RadioButton1.Text
Else
Sex = RadioButton2.Text
End If
Dim query As String = "Update StudentInfo_tb set
Id=@Id,Name=@name,Address=@Address,City=@City,Age=@Age,Sex=@Sex where Id=@Id"
Using con As SqlConnection = New SqlConnection("Data Source=DESKTOP-
L4MA6U1;Initial Catalog=StudentInfo_Db;Integrated Security=True")
Using cmd As SqlCommand = New SqlCommand(query, con)
cmd.Parameters.AddWithValue("@Id", Id)
cmd.Parameters.AddWithValue("@Name", Name)
cmd.Parameters.AddWithValue("@Address", Address)
cmd.Parameters.AddWithValue("@City", City)
cmd.Parameters.AddWithValue("@Age", Age)
cmd.Parameters.AddWithValue("@Sex", Sex)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
MessageBox.Show("Successfuly Update")
BindData()
End Using
End Using
End Sub

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click


Dim Id As Integer = TextBox1.Text
Dim Name As String = TextBox2.Text
Dim Address As String = TextBox3.Text
Dim City As String = ComboBox1.Text
Dim Age As Integer = TextBox4.Text
Dim Sex As String = ""
If RadioButton1.Checked = True Then
Sex = RadioButton1.Text
Else
Sex = RadioButton2.Text
End If
Dim query As String = "Delete StudentInfo_tb where Id=@Id"
Using con As SqlConnection = New SqlConnection("Data Source=DESKTOP-
L4MA6U1;Initial Catalog=StudentInfo_Db;Integrated Security=True")
Using cmd As SqlCommand = New SqlCommand(query, con)
cmd.Parameters.AddWithValue("@Id", Id)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
MessageBox.Show("Successfuly Delete")
BindData()
End Using
End Using
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load


BindData()
End Sub
End Class

You might also like