DESKTOP

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

Student_Info_DBConnectionString

Student_Info_DBDataSet

Student_Info_DBDataSet1

DESKTOP-8AE97D7
Data Source=DESKTOP-8AE97D7\SQLSERVER;Initial Catalog=Student_Info_DB;Integrated
Security=True

DESKTOP-8AE97D7\SQLSERVER

_stud

Imports System.Data.SqlClient

Public Class Form1


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

Me.Tbl_stud_infoTableAdapter.Fill(Me.Student_Info_DBDataSet.tbl_stud_info)

End Sub

Private Sub btnInsert_Click(sender As Object, e As EventArgs) Handles


btnInsert.Click
Dim ID As Integer = txtSearch.Text
Dim FIRSTNAME As String = txtFName.Text
Dim LASTNAME As String = txtLName.Text
Dim ADDRESS As String = txtAddress.Text
Dim AGE As Integer = txtAge.Text
Dim GENDER As String = cbGender.SelectedItem

Dim query As String = "INSERT INTO tbl_stud_info


VALUES(@ID,@FIRSTNAME,@LASTNAME,@ADDRESS,@AGE,@GENDER)"
Using con As SqlConnection = New SqlConnection("Data Source=DESKTOP-
8AE97D7\SQLSERVER;Initial Catalog=Student_Info_DB;Integrated Security=True")
Using cmd As SqlCommand = New SqlCommand(query, con)
cmd.Parameters.AddWithValue("@ID", ID)
cmd.Parameters.AddWithValue("@FIRSTNAME", FIRSTNAME)
cmd.Parameters.AddWithValue("@LASTNAME", LASTNAME)
cmd.Parameters.AddWithValue("@ADDRESS", ADDRESS)
cmd.Parameters.AddWithValue("@AGE", AGE)
cmd.Parameters.AddWithValue("@GENDER", GENDER)

con.Open()
cmd.ExecuteNonQuery()
con.Close()

txtSearch.Text = ""
txtFName.Text = ""
txtLName.Text = ""
txtAddress.Text = ""
txtAge.Text = ""
cbGender.SelectedItem = ""

MessageBox.Show("Data Inserted Successfully")


BindData()
End Using
End Using

End Sub

Public Sub BindData()


Dim query As String = "SELECT * FROM tbl_stud_info"
Using con As SqlConnection = New SqlConnection("Data Source=DESKTOP-
8AE97D7\SQLSERVER;Initial Catalog=Student_Info_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 btn_Exit_Click(sender As Object, e As EventArgs) Handles


btn_Exit.Click
Me.Close()
End Sub

Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles


btnSearch.Click

Dim query As String = "SELECT * FROM tbl_stud_info WHERE ID ='" &


txtSearch.Text & "'"
Using con As SqlConnection = New SqlConnection("Data Source=DESKTOP-
8AE97D7\SQLSERVER;Initial Catalog=Student_Info_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
txtSearch.Text = dt.Rows(0)(0).ToString()
txtFName.Text = dt.Rows(0)(1).ToString()
txtLName.Text = dt.Rows(0)(2).ToString()
txtAddress.Text = dt.Rows(0)(3).ToString()
txtAge.Text = dt.Rows(0)(4).ToString()
cbGender.Text = dt.Rows(0)(5).ToString()
Else
txtSearch.Text = ""
txtFName.Text = ""
txtLName.Text = ""
txtAddress.Text = ""
txtAge.Text = ""
cbGender.Text = ""
End If
End Using
End Using
End Using
End Using
End Sub

Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles


btnDelete.Click
Dim ID As String = txtSearch.Text
Dim query As String = "DELETE FROM tbl_stud_info WHERE ID= @ID"
Using con As SqlConnection = New SqlConnection("Data Source=DESKTOP-
8AE97D7\SQLSERVER;Initial Catalog=Student_Info_DB;Integrated Security=True")
Using cmd As SqlCommand = New SqlCommand(query, con)
cmd.Parameters.AddWithValue("@ID", ID)

con.Open()
cmd.ExecuteNonQuery()
con.Close()
txtSearch.Text = ""
txtFName.Text = ""
txtLName.Text = ""
txtAddress.Text = ""
txtAge.Text = ""
cbGender.Text = ""

MessageBox.Show("Deleted Succesfully!")
BindData()
End Using
End Using
End Sub

Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles


btnUpdate.Click

Dim ID As Integer = txtSearch.Text


Dim FIRSTNAME As String = txtFName.Text
Dim LASTNAME As String = txtLName.Text
Dim ADDRESS As String = txtAddress.Text
Dim AGE As Integer = txtAge.Text
Dim GENDER As String = cbGender.SelectedItem

Dim query As String = "UPDATE tbl_stud_info SET FIRSTNAME = @FIRSTNAME,


LASTNAME = @LASTNAME, ADDRESS = @ADDRESS, AGE = @AGE, GENDER = @GENDER WHERE
ID=@ID"
Using con As SqlConnection = New SqlConnection("Data Source=DESKTOP-
8AE97D7\SQLSERVER;Initial Catalog=Student_Info_DB;Integrated Security=True")

Using cmd As SqlCommand = New SqlCommand(query, con)


cmd.Parameters.AddWithValue("@ID", ID)
cmd.Parameters.AddWithValue("@FIRSTNAME", FIRSTNAME)
cmd.Parameters.AddWithValue("@LASTNAME", LASTNAME)
cmd.Parameters.AddWithValue("@ADDRESS", ADDRESS)
cmd.Parameters.AddWithValue("@AGE", AGE)
cmd.Parameters.AddWithValue("@GENDER", GENDER)

con.Open()
cmd.ExecuteNonQuery()
con.Close()
txtSearch.Text = ""
txtFName.Text = ""
txtLName.Text = ""
txtAddress.Text = ""
txtAge.Text = ""
cbGender.Text = ""

MessageBox.Show("Updated Succesfully!")
BindData()
End Using
End Using

End Sub
End Class

You might also like