Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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
Posted

1 solution

XML
vb.net code:

 Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
        BindGridView()
        GridView1.PageIndex = e.NewPageIndex
        GridView1.DataBind()
    End Sub"

The ASPX page

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" AllowPaging="true" PageSize="10" OnPageIndexChanging="GridView1_PageIndexChanging">
 
Share this answer
 
Comments
Abdurrhmanzaki 30-Apr-17 2:41am    
Thank u , worked 4 me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900