0% found this document useful (0 votes)
3 views

Prg_26TO30_DATABASE

The document provides a series of VB.NET code examples for creating database applications, including login forms, employee management, and CRUD operations on various tables. Each section includes code for connecting to a SQL database, executing commands, and handling user input for operations like insert, update, delete, and navigation. The examples demonstrate the use of SQL commands, data binding, and user interface elements in VB.NET.

Uploaded by

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

Prg_26TO30_DATABASE

The document provides a series of VB.NET code examples for creating database applications, including login forms, employee management, and CRUD operations on various tables. Each section includes code for connecting to a SQL database, executing commands, and handling user input for operations like insert, update, delete, and navigation. The examples demonstrate the use of SQL commands, data binding, and user interface elements in VB.NET.

Uploaded by

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

 Database Programs

26) Design the Login Form as shown below.

Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Dim a As Integer = 0
Private Sub BtnLogin_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim con As SqlConnection = New SqlConnection("Data
Source=(LocalDB)\v11.0;AttachDbFilename=E:\2024\VB.NET\DATABASE_PRG\p26\p26\Database1.
mdf;Integrated Security=True")
Dim cmd As SqlCommand = New SqlCommand("select * from login where username='"
+ Txtunm.Text + "' and password='" + TxtPass.Text + "'", con)
Dim sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
If (dt.Rows.Count > 0) Then
MsgBox("Login SuccessFully...")
Else
MsgBox("Invalid Username or Password")
a = a + 1
If a >= 3 Then
Me.Close()
End If
End If
End Sub

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


Button2.Click
Me.Close()
End Sub
End Class

O/P:
27) Create a Employee table with the fields mentioned on the form in SQL. Add few
record in this table using SQL. Design following form in vb.net and perform
navigations.

Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Dim con As New SqlConnection("Data
Source=(LocalDB)\v11.0;AttachDbFilename=D:\2024\VB.NET\DATABASE_PRG\p27\p27\Database1.
mdf;Integrated Security=True")
Dim cmd As New SqlCommand
Dim da As SqlDataAdapter
Dim ds As DataSet
Dim tb1 As New DataTable()
Dim bm As BindingManagerBase
Dim str As String

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


Btnfirst.Click
bm.Position = 1
End Sub

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


If (con.State = ConnectionState.Closed) Then
con.Open()
End If
str = "select *from emp"
cmd = New SqlCommand(str, con)
da = New SqlDataAdapter(cmd)
ds = New DataSet
da.Fill(ds)
tb1 = ds.Tables(0)
bm = Me.BindingContext(tb1)
Txtno.DataBindings.Add("text", tb1, "EmpNo")
Txtnm.DataBindings.Add("text", tb1, "EmpNm")
TxtDept.DataBindings.Add("text", tb1, "Dept")
TxtDesi.DataBindings.Add("text", tb1, "Desig")
con.Close()

End Sub

Private Sub BtnPrev_Click(sender As Object, e As EventArgs) Handles BtnPrev.Click


bm.Position -= 1
End Sub

Private Sub TxtNext_Click(sender As Object, e As EventArgs) Handles BtnNext.Click


bm.Position += 1
End Sub

Private Sub BtnLast_Click(sender As Object, e As EventArgs) Handles BtnLast.Click


bm.Position = bm.count
End Sub
End Class

O/P:

28) Design the Form as shown below

a. Create the form that shows the basic operations on Student database (in SQL) like insert, delete
and update.
b. The navigation operation on database.

Imports System.Data
Imports System.Data.SqlClient

Public Class Form1


Dim con As New SqlConnection
Dim cmd As New SqlCommand
Dim i As Integer
Dim str As String
Private Sub BtnInsert_Click(sender As Object, e As EventArgs) Handles
BtnInsert.Click
str = "insert into Emp values(@Empno,@Empname,@Address)"
cmd = New SqlCommand(str, con)
cmd.Parameters.AddWithValue("@Empno", TxtNo.Text)
cmd.Parameters.AddWithValue("@Empname", TxtName.Text)
cmd.Parameters.AddWithValue("@Address", TxtAddress.Text)

cmd.ExecuteNonQuery()
MsgBox("Record is Inserted Sucessfully")

TxtNo.Clear()
TxtName.Clear()
TxtAddress.Clear()
End Sub

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


con = New SqlConnection("Data
Source=(LocalDB)\v11.0;AttachDbFilename=D:\2024\VB.NET\DATABASE_PRG\p28\p28\Database1.
mdf;Integrated Security=True")

If con.State = ConnectionState.Closed Then


con.Open()
End If
End Sub

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


BtnUpdate.Click
str = "update Emp set Empname=@Empname,Address=@Address where Empno=@Empno"

cmd = New SqlCommand(str, con)


cmd.Parameters.AddWithValue("@Empno", TxtNo.Text)
cmd.Parameters.AddWithValue("@Empname", TxtName.Text)
cmd.Parameters.AddWithValue("@Address", TxtAddress.Text)
cmd.ExecuteNonQuery()
MsgBox("Record is Updated Sucessfully")
TxtNo.Clear()
TxtName.Clear()
TxtAddress.Clear()
End Sub

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


BtnDelete.Click
str = "delete from Emp where Empno=@Empno"
cmd = New SqlCommand(str, con)

cmd.Parameters.AddWithValue("@Empno", TxtNo.Text)
cmd.ExecuteNonQuery()

MsgBox("Record is Deleted")
TxtNo.Clear()

End Sub
End Class
O/P:
29) Design the form as shown below
Create table in SQL with following fields. Perform all functions indicated on buttons.

Imports System.Data
Imports System.Data.SqlClient

Public Class Form1


Dim con As New SqlConnection("Data
Source=(LocalDB)\v11.0;AttachDbFilename=D:\2024\VB.NET\DATABASE_PRG\p29\p29\Database1.
mdf;Integrated Security=True")
Dim cmd As New SqlCommand
Dim i As Integer
Dim str As String
Dim da As SqlDataAdapter
Dim ds As DataSet
Dim tb1 As New DataTable
Dim bm As BindingManagerBase
Dim cnt As Integer
Dim dr As SqlDataReader
Dim a As Integer = 0

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


If (con.State = ConnectionState.Closed) Then
con.Open()
End If
str = "select *from Employee"
cmd = New SqlCommand(str, con)
da = New SqlDataAdapter(cmd)
ds = New DataSet
da.Fill(ds)
tb1 = ds.Tables(0)
bm = Me.BindingContext(tb1)
TxtEno.DataBindings.Add("text", tb1, "EmpNo")
TxtName.DataBindings.Add("text", tb1, "Empnm")
TxtJob.DataBindings.Add("text", tb1, "Job")
TxtSal.DataBindings.Add("text", tb1, "Salary")
TxtDeptNo.DataBindings.Add("text", tb1, "DeptNo")
DateTimePicker1.DataBindings.Add("text", tb1, "Date")
TxtMaNo.DataBindings.Add("text", tb1, "ManagerNo")
TxtComm.DataBindings.Add("text", tb1, "Comm")
TxtEno.Clear()
TxtComm.Clear()
TxtDeptNo.Clear()
DateTimePicker1.Text = Today
TxtJob.Clear()
TxtMaNo.Clear()
TxtName.Clear()
TxtSal.Clear()

End Sub

Private Sub BtnExit_Click(sender As Object, e As EventArgs) Handles BtnExit.Click


Me.Close()
End Sub

Private Sub BtnAdd_Click(sender As Object, e As EventArgs) Handles BtnAdd.Click


TxtEno.Clear()
TxtComm.Clear()
TxtDeptNo.Clear()
DateTimePicker1.Text = Today
TxtJob.Clear()
TxtMaNo.Clear()
TxtName.Clear()
TxtSal.Clear()

End Sub

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


BtnUpdate.Click
str = "update Employee set
Empnm=@Empnm,Job=@Job,Salary=@Salary,DeptNo=@DeptNo,Date=@Date,ManagerNo=@ManagerNo,Co
mm=@Comm where Empno=@Empno"
cmd = New SqlCommand(str, con)

cmd.Parameters.AddWithValue("@Empno", TxtEno.Text)
cmd.Parameters.AddWithValue("@Empnm", TxtName.Text)
cmd.Parameters.AddWithValue("@Job", TxtJob.Text)
cmd.Parameters.AddWithValue("@Salary", TxtSal.Text)
cmd.Parameters.AddWithValue("@DeptNo", TxtDeptNo.Text)
cmd.Parameters.AddWithValue("@Date", DateTimePicker1.Text)
cmd.Parameters.AddWithValue("@ManagerNo", TxtMaNo.Text)
cmd.Parameters.AddWithValue("@Comm", TxtComm.Text)

cmd.ExecuteNonQuery()
MsgBox("Record is Updated Sucessfully")
TxtEno.Clear()
TxtComm.Clear()
TxtDeptNo.Clear()
DateTimePicker1.Text = Today
TxtJob.Clear()
TxtMaNo.Clear()
TxtName.Clear()
TxtSal.Clear()

End Sub

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


BtnInsert.Click
str = "insert into Employee
values(@Empno,@Empnm,@Job,@Salary,@DeptNo,@Date,@ManagerNo,@Comm)"
cmd = New SqlCommand(str, con)
cmd.Parameters.AddWithValue("@Empno", TxtEno.Text)
cmd.Parameters.AddWithValue("@Empnm", TxtName.Text)
cmd.Parameters.AddWithValue("@Job", TxtJob.Text)
cmd.Parameters.AddWithValue("@Salary", TxtSal.Text)
cmd.Parameters.AddWithValue("@DeptNo", TxtDeptNo.Text)
cmd.Parameters.AddWithValue("@Date", DateTimePicker1.Text)
cmd.Parameters.AddWithValue("@ManagerNo", TxtMaNo.Text)
cmd.Parameters.AddWithValue("@Comm", TxtComm.Text)

cmd.ExecuteNonQuery()
MsgBox("Record is Inserted Sucessfully")

TxtEno.Clear()
TxtComm.Clear()
TxtDeptNo.Clear()
DateTimePicker1.Text = Today
TxtJob.Clear()
TxtMaNo.Clear()
TxtName.Clear()
TxtSal.Clear()

End Sub

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


BtnDelete.Click
str = "delete from Employee where Empno=@Empno"
cmd = New SqlCommand(str, con)

cmd.Parameters.AddWithValue("@Empno", TxtEno.Text)
cmd.ExecuteNonQuery()

MsgBox("Record is Deleted")
TxtEno.Clear()
TxtComm.Clear()
TxtDeptNo.Clear()
DateTimePicker1.Text = Today
TxtJob.Clear()
TxtMaNo.Clear()
TxtName.Clear()
TxtSal.Clear()

End Sub
Public Sub employee()
str = "Select * from Employee"
cmd = New SqlCommand(str, con)
da = New SqlDataAdapter(cmd)
ds = New DataSet
da.Fill(ds, "Employee")
TxtEno.Text = ds.Tables("Employee").Rows(a).Item(0)
TxtName.Text = ds.Tables("Employee").Rows(a).Item(1)
TxtJob.Text = ds.Tables("Employee").Rows(a).Item(2)
TxtSal.Text = ds.Tables("Employee").Rows(a).Item(3)
TxtDeptNo.Text = ds.Tables("Employee").Rows(a).Item(4)
DateTimePicker1.Text = ds.Tables("Employee").Rows(a).Item(5)
TxtMaNo.Text = ds.Tables("Employee").Rows(a).Item(6)
TxtComm.Text = ds.Tables("Employee").Rows(a).Item(7)
End Sub

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


BtnSearch.Click
i = InputBox("Enter no")
str = "select * from Employee where Empno = '" + i.ToString + "'"
cmd = New SqlCommand(str, con)
dr = cmd.ExecuteReader

If dr.Read Then
TxtEno.Text = dr.Item(0)
TxtName.Text = dr.Item(1)
TxtJob.Text = dr.Item(2)
TxtSal.Text = dr.Item(3)
TxtDeptNo.Text = dr.Item(4)
DateTimePicker1.Text = dr.Item(5)
TxtMaNo.Text = dr.Item(6)
TxtComm.Text = dr.Item(7)
Else
MsgBox("No is not exit")
End If
Call employee()

End Sub

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


BtnFirst.Click
bm.Position = 0
End Sub

Private Sub BtnPre_Click(sender As Object, e As EventArgs) Handles BtnPre.Click


bm.Position -= 1
End Sub

Private Sub BtnNext_Click(sender As Object, e As EventArgs) Handles BtnNext.Click


bm.Position += 1
End Sub

Private Sub BtnLast_Click(sender As Object, e As EventArgs) Handles BtnLast.Click


bm.Position = bm.Count
End Sub
End Class

O/P:
30) Design the form as shown below:
Create table in SQL with following fields. Perform all functions indicated on
buttons and populate data in DataGridView.

Imports System.Data
Imports System.Data.SqlClient

Public Class Form1


Dim con As New SqlConnection
Dim cmd As New SqlCommand
Dim da As SqlDataAdapter
Dim dt As DataTable
Dim i As Integer
Dim s As String
Public Sub abc()
s = "Select * from Information1"
cmd = New SqlCommand(s, con)
da = New SqlDataAdapter(cmd)
dt = New DataTable
da.Fill(dt)
DataGridView1.DataSource = dt
End Sub

Private Sub BtnAdd_Click(sender As Object, e As EventArgs) Handles BtnAdd.Click


TxtAdd.Clear()
TxtCity.Clear()
TxtNm.Clear()
TxtPNo.Clear()
DateTimePicker1.Text = Today
End Sub

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


con = New SqlConnection("Data
Source=(LocalDB)\v11.0;AttachDbFilename=D:\2024\VB.NET\DATABASE_PRG\p30\p30\Database1.
mdf;Integrated Security=True")
con.Open()
Call abc()
End Sub
Private Sub BtnInsert_Click(sender As Object, e As EventArgs) Handles
BtnInsert.Click
If TxtNm.Text = "" Or TxtAdd.Text = "" Or TxtCity.Text = "" Or TxtPNo.Text =
"" Then
MsgBox("Please fill data")
Else
s = "insert into Information1 values('" + TxtNm.Text + "','" + TxtAdd.Text
+ "','" + TxtCity.Text + "','" + TxtPNo.Text + "','" + DateTimePicker1.Text + "')"
cmd = New SqlCommand(s, con)
i = cmd.ExecuteNonQuery
If i > 0 Then
MsgBox("Data Inserted")
Else
MsgBox("Data Not Inserted")
End If
End If
Call abc()
End Sub

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


BtnUpdate.Click
If TxtNm.Text = "" Or TxtAdd.Text = "" Or TxtCity.Text = "" Or TxtPNo.Text =
"" Then
MsgBox("Please Fill data")
Else
s = "update Information1 set Address='" + TxtAdd.Text + "',City='" +
TxtCity.Text + "',PhNo='" + TxtPNo.Text + "',Date='" + DateTimePicker1.Text + "' where
Name='" + TxtNm.Text + "'"
cmd = New SqlCommand(s, con)
i = cmd.ExecuteNonQuery
If i > 0 Then
MsgBox("Data Updated")
Else
MsgBox("Data Not Updated")
End If
End If
Call abc()
End Sub

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


BtnDelete.Click
If TxtNm.Text = "" Then
MsgBox("Please enter student number")
Else
s = "delete from Information1 where Name='" + TxtNm.Text + "'"
cmd = New SqlCommand(s, con)
i = cmd.ExecuteNonQuery
If i > 0 Then
MsgBox("Data Deleted")
Else
MsgBox("Data Not Deleted")
End If
End If
Call abc()
End Sub
End Class
O/P:

You might also like