Imports System.Data
Imports System.Data.SqlClient
Public Class DepartmentForm
Inherits System.Web.UI.Page
Dim strcon As String = ConfigurationManager.ConnectionStrings("DeviceDBConnectionString").ToString
Dim con As SqlConnection = New SqlConnection(strcon)
Dim cmd As New SqlCommand
Dim dr As SqlDataReader
Private Property ds As Object
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
BindGridView()
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Try
con.Open()
cmd.Connection = con
cmd.CommandText = "INSERT INTO TBL_DEPARTMENT_COD VALUES('" & txtdepcode.Text & "','" & txtdepname.Text & "')"
cmd.ExecuteNonQuery()
Catch ex As Exception
'MsgBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records")
MsgBox("Error " & ex.Message)
Finally
con.Close()
End Try
End Sub
Protected Sub BindGridView()
Dim da As New SqlDataAdapter("SELECT [DEPCODE], [DEPDESC] FROM [TBL_DEPARTMENT_COD]", strcon)
Dim ds As New DataSet()
da.Fill(ds)
GridView1.DataSource = ds
GridView1.DataBind()
End Sub
Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles GridView1.SelectedIndexChanged
txtdepcode.Text = GridView1.SelectedRow.Cells(0).Text
txtdepname.Text = GridView1.SelectedRow.Cells(1).Text
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnupdate.Click
Try
con.Open()
cmd.Connection = con
cmd.CommandText = "UPDATE TBL_DEPARTMENT_COD SET DEPCODE ='" & txtdepcode.Text & "', DEPDESC = '" & txtdepname.Text & "'WHERE DEPCODE= '" & txtdepcode.Text & "'"
cmd.ExecuteNonQuery()
Catch ex As Exception
'MsgBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records")
MsgBox("Error " & ex.Message)
Finally
con.Close()
End Try
End Sub
Protected Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btndelete.Click
Try
con.Open()
cmd.Connection = con
cmd.CommandText = "DELETE FROM TBL_DEPARTMENT_COD WHERE DEPCODE ='" & txtdepcode.Text & "'"
cmd.ExecuteNonQuery()
Catch ex As Exception
'MsgBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records")
MsgBox("Error " & ex.Message)
Finally
con.Close()
End Try
End Sub
Public Sub ClearTextBox(ByVal root As Control)
For Each ctrl As Control In root.Controls
ClearTextBox(ctrl)
If TypeOf ctrl Is TextBox Then
CType(ctrl, TextBox).Text = String.Empty
End If
Next ctrl
End Sub
Protected Sub Button4_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnClear.Click
ClearTextBox(Me)
End Sub
Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
GridView1.PageIndex = e.NewPageIndex
BindGridView()
End Sub
End Class