ARTICLE

Update Records of a Table Through Different Page in ASP.NET

Posted by Satyapriya Nayak Articles | Visual Basic .NET December 29, 2011
In this article we will learn how to update some records in a GridView of a given table in another page.
Reader Level:
 

Here we will first insert some records into an Insert.aspx page. After inserting the records the control will move to the Default.aspx page where we can visualize the records that we have already inserted. Here the first record will be displayed as a link. So if we need to update some values other than the first record, then we will click the corresponding record link and the control moves to the Update.aspx page. We can update the records with an update button or if we don't want to update the record then click the cancel button, so that the control will move to the Default.aspx page.

Table Creation

gridview.gif

Insert.aspx code

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Insert.aspx.vb"Inherits="Gridview_update_other_page_vb.Insert" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<
html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<
body>
    <form id="form1" runat="server">
    <div>
    <h2>Insert Employee Details</h2>
    <asp:Label ID="Label1" runat="server" Text="EmpName" Font-Bold="True" 
        Width="100px"></asp:Label>
    <asp:TextBox ID="txtFullName" runat="server"></asp:TextBox><br />
    
     <asp:Label ID="Label2" runat="server" Text="FName" Font-Bold="True" 
        Width="100px"></asp:Label>
    <asp:TextBox ID="txtFName" runat="server"></asp:TextBox><br />
    
     <asp:Label ID="Label3" runat="server" Text="LName" Font-Bold="True"

        Width="100px"></asp:Label>
    <asp:TextBox ID="txtLName" runat="server"></asp:TextBox><br />
    
     <asp:Label ID="Label4" runat="server" Text="City" Font-Bold="True" 
        Width="100px"></asp:Label>
    <asp:TextBox ID="txtcity" runat="server"></asp:TextBox><br />
    
     <asp:Label ID="Label5" runat="server" Text="State" Font-Bold="True" 
        Width="100px"></asp:Label>
    <asp:TextBox ID="txtstate" runat="server"></asp:TextBox><br />
    <asp:Button ID="btn_insert" runat="server" Text="Insert Records" 
        Font-Bold="True" onclick="btn_insert_Click" />
    </div>
    </form>
</body>
</
html>

Insert.aspx.vb code

Imports System.Data
Imports System.Data.SqlClient

Partial Public Class Insert
    Inherits System.Web.UI.Page
    Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ToString()
    Dim con As New SqlConnection(strConnString)

    Dim str As String
    Dim com As SqlCommand
    Dim sqlda As SqlDataAdapter
    Dim ds As DataSet
 
    Protected Sub btn_insert_Click(ByVal sender As ObjectByVal e As EventArgs) Handlesbtn_insert.Click
        con.Open()
        str = "insert into employee(Empname,EmpFname,EmpLname,Empcity,Empstate) values('" & txtFullName.Text & "','" & txtFName.Text & "','" &
txtLName.Text & "','" & txtcity.Text & "','" & txtstate.Text & "')"
        com = New SqlCommand(str, con)
        com.ExecuteNonQuery()
        con.Close()
        Response.Redirect("Default.aspx")
    End Sub
End Class

Default.aspx code

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb"Inherits="Gridview_update_other_page_vb._Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<
html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<
body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView runat="server" ID="GridView1"  AutoGenerateColumns="false" 
            HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="White" 
            DataKeyNames="Empid" ForeColor="#663300" Font-Bold="True"> 
    <Columns>
    <asp:TemplateField HeaderText="Employee">
    <ItemTemplate>
    <a href ='<%#"Update.aspx?Empid=" & DataBinder.Eval(container.dataitem,("Empid")) %>'><%#Eval("Empname")%>  </a>
    </ItemTemplate
    </asp:TemplateField>
    <asp:BoundField DataField="Empname" HeaderText="EmpName" />
    <asp:BoundField DataField="EmpFname" HeaderText="EmpFname" />
    <asp:BoundField DataField="EmpLname" HeaderText="EmpLname" />
    <asp:BoundField DataField="Empcity" HeaderText="Empcity" />
    <asp:BoundField DataField="Empstate" HeaderText="Empstate" />
    </Columns>           
 
<HeaderStyle BackColor="Red" ForeColor="White"></HeaderStyle>
            <AlternatingRowStyle ForeColor="#003300" /> 
    </asp:GridView>
    </div>
    </form>
</body>
</
html>

Default.aspx.vb

Imports System.Data
Imports System.Data.SqlClient

Partial Public Class _Default
    Inherits System.Web.UI.Page
    Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ToString()
    Dim con As New SqlConnection(strConnString)
    Dim str As String
    Dim com As SqlCommand
    Dim sqlda As SqlDataAdapter
    Dim ds As DataSet

    Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) HandlesMe.Load
        If Not IsPostBack Then
            bind()
        End If
    End Sub
    Sub bind()
        con.Open()
        str = "select * from employee"
        com = New SqlCommand(str, con)
        sqlda = New SqlDataAdapter(com)
        con.Close()

        ds = New DataSet()
        sqlda.Fill(ds, "employee")
        GridView1.DataSource = ds
        GridView1.DataMember = "employee"
        GridView1.DataBind()
    End Sub
 
End
 Class

Update.aspx code 

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Update.aspx.vb"Inherits="Gridview_update_other_page_vb.Update" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
           function Display(Empname) {
            alert(Empname + ':::updated successfully');
            if (alert) {
                window.location = 'Default.aspx';
            }
        }
    </script>
</head>
<
body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td colspan="2" align="center">
                    <b>Edit Employee Details</b>
                </td>
            </tr>
            <tr>
                <td>
                    EmpName:
                </td>
                <td>
                    <asp:Label ID="lblFullName" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                    FName:
                </td>
                <td>
                    <asp:TextBox ID="txtFName" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    LName:
                </td>
                <td>
                    <asp:TextBox ID="txtLName" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    City:
                </td>
                <td>
                    <asp:TextBox ID="txtcity" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    State:
                </td>
                <td>
                    <asp:TextBox ID="txtstate" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <asp:Button ID="btnUpdate" runat="server" Text="Update"OnClick="btnUpdate_Click" />
                    <asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClick="btnCancel_Click"/>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</
html>
 

Update.aspx.vb

Imports System.Data
Imports System.Data.SqlClient

Partial Public Class Update
    Inherits System.Web.UI.Page
    Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ToString()
    Dim con As New SqlConnection(strConnString)
    Dim str As String
    Dim com As SqlCommand
    Dim sqlda As SqlDataAdapter
    Dim ds As DataSet
    Dim empid As Integer = 0
    Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) HandlesMe.Load
        empid = Convert.ToInt32(Request.QueryString("Empid").ToString())

        If Not IsPostBack Then
            bind()
        End If
    End Sub
    Sub bind()
        con.Open()
        str = "select * from employee where Empid=" & empid
        com = New SqlCommand(str, con)
        sqlda = New SqlDataAdapter(com)
        com.ExecuteNonQuery()
        con.Close()
        ds = New DataSet()
        sqlda.Fill(ds)
        lblFullName.Text = ds.Tables(0).Rows(0)(1).ToString()
        txtFName.Text = ds.Tables(0).Rows(0)(2).ToString()
        txtLName.Text = ds.Tables(0).Rows(0)(3).ToString()
        txtcity.Text = ds.Tables(0).Rows(0)(4).ToString()
        txtstate.Text = ds.Tables(0).Rows(0)(5).ToString()
    End Sub
 
    Protected Sub btnUpdate_Click(ByVal sender As ObjectByVal e As EventArgs) HandlesbtnUpdate.Click
        con.Open()
        str = "update employee set EmpFname='" & txtFName.Text & "',EmpLname='" & txtLName.Text & "',Empcity='" & txtcity.Text & "',Empstate='" &
txtstate.Text & "' where Empid=" & empid
        com = New SqlCommand(str, con)
        sqlda = New SqlDataAdapter(com)
        Dim result As Integer
        result = com.ExecuteNonQuery()
        con.Close()
        If result = 1 Then
            ScriptManager.RegisterStartupScript(MeMe.GetType(), "ShowSuccess","javascript:Display('" & lblFullName.Text & "')"True)
        End If
    End Sub
 
    Protected Sub btnCancel_Click(ByVal sender As ObjectByVal e As EventArgs) HandlesbtnCancel.Click
        Response.Redirect("~/Default.aspx")
    End Sub
End Class 


Output

Gridview2.gif

Thanks for Reading.....

COMMENT USING

Trending up