You are on page 1of 5

VB.ASP.

NET
Imports System.Data
Imports System.Data.SqlClient

Public Class WebForm1


Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "


#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


DropDownList1.Items.Add("select the person")
Call NameView()
End Sub

'============FILL DATA TO DROPDOWN LIST IN FORM LORD=================


Public Sub NameView()
Try
Dim con As New SqlConnection("Data Source=paxar_is;Initial catalog=Esolution;uid=sa;pwd=")
'Dim Con As New SqlConnection("Packet Size=4096;Data Source=paxar_is;Initial
Catalog=Esolution;Persist Security Info=False;uid=sa;pwd=")
'con.Open()
Dim Ds As New SqlDataAdapter("select * from Employee ", con)
Dim dt As New DataTable

Ds.Fill(dt)

Dim ctr As Integer


For ctr = 0 To dt.Rows.Count - 1
DropDownList1.Items.Add(dt.Rows(ctr).Item("Name"))
Next

Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
'=========ADD====================================

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


Button1.Click

If (TextBox1.Text = "") Then


Label4.Text = "Please enter Employee Name!"
Exit Sub
End If

Try
Dim Con As New SqlConnection("Data Source=paxar_is;Initial Catalog=Esolution;uid=sa;pwd=")
Con.Open()
Dim Com As New SqlCommand("INSERT INTO Employee(Name,Dept,Salary)VALUES('" & TextBox1.Text &
"','" & TextBox2.Text & "','" & TextBox3.Text & "')", Con)
Com.ExecuteNonQuery()

Catch ex As Exception
MsgBox(ex.ToString())
End Try
Response.Redirect("webform2.aspx")
End Sub

'===============DELETE==================

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


Button4.Click
Try
Dim Con As New SqlConnection("Data Source=paxar_is;Initial Catalog=Esolution;uid=sa;pwd=")
Con.Open()
Dim Com As New SqlCommand("delete from Employee where name ='" & Trim(TextBox1.Text) & "'", Con)
Com.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.ToString())
End Try
Response.Redirect("webform2.aspx")
End Sub
'=========SEARCH=USING TEXTBOX==================
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button2.Click
Try
Dim con As New SqlConnection("Data Source=paxar_is;Initial Catalog=Esolution;uid=sa;pwd=")
con.Open()
Dim ds As New SqlDataAdapter("select * from Employee where name='" & TextBox1.Text & "'", con)
Dim dt As New DataTable
ds.Fill(dt)
TextBox2.Text = dt.Rows(0).Item("dept")
TextBox3.Text = dt.Rows(0).Item("salary")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

'============UPDATE=====================

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


Button3.Click

Try
Dim con As New SqlConnection("Data Source=paxar_is;Initial Catalog=Esolution;uid=sa;pwd=")
con.Open()
Dim cmd As New SqlCommand("update employee set dept='" & TextBox2.Text & "',salary='" &
TextBox3.Text & "' where name='" & TextBox1.Text & "'", con)
cmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.ToString())
End Try
Response.Redirect("webform2.aspx")
End Sub

'==========SEARCH RECORD WHEN INDEX CHANGE IN DROPDOWN LIST==============


Public Function SeachRecord(ByVal cusname As String)
Try
Dim Con As New SqlConnection("Packet Size=4096;Data Source=paxar_is;Initial
Catalog=Esolution;Persist Security Info=False;uid=sa;pwd=")
Dim Ds As New SqlDataAdapter("Select * From Employee", Con)
Dim Dt As New DataTable
Ds.Fill(Dt)
Dim ctr As Integer

For ctr = 0 To Dt.Rows.Count - 1


If Trim(Dt.Rows(ctr).Item("name")) = Trim(cusname) Then

TextBox1.Text = Dt.Rows(ctr).Item("name")
TextBox2.Text = Dt.Rows(ctr).Item("dept")
TextBox3.Text = Dt.Rows(ctr).Item("salary")

Exit Function
End If
Next
Catch ex As Exception
MsgBox(ex.ToString())
End Try

End Function

Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles DropDownList1.SelectedIndexChanged
Call SeachRecord(DropDownList1.SelectedItem.Value)
End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


Button5.Click
DropDownList1.Items.Clear()
NameView()
End Sub
End Class
'==========SEARCH RECORD TO GRID WHEN INDEX CHANGE IN DROPDOWN LIST==============
'FIRST SEARCH RECORD TO TEXT BOX,THEN THAT RECORD TO DATA GRID

Public Function SearchGrid(ByVal cusname As String)


Try
Dim con As New SqlConnection("data source=paxar_is;initial catalog=esolution;uid=sa;pwd=")
con.Open()
Dim ds As New SqlDataAdapter("select * from employee where name='" & TextBox1.Text & "'", con)
Dim dt As New DataSet
ds.Fill(dt)
DataGrid1.DataSource = dt
DataGrid1.DataBind()

Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Function

Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles DropDownList1.SelectedIndexChanged
Call SeachRecord(DropDownList1.SelectedItem.Value)
Call SearchGrid(DropDownList1.SelectedItem.Value)
End Sub