.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

How to update some records in a Grid view of a given table in other page

Posted By:satyapriyanayak       Posted Date: September 12, 2013    Points: 200    Category: ASP.NET    URL: http://www.dotnetspark.com  

In this article we will know how to update some records in a Grid view of a given table in other page. Here first we will insert some records in 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 first record will be display as a link. So if we need to update some values except the first record, then we will click the corresponding record link, the control moves to Update.aspx page. W
 

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 Object, ByVal e As EventArgs) Handles btn_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 Object, ByVal e As System.EventArgs) Handles Me.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 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 Object, ByVal e As System.EventArgs) Handles Me.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 Object, ByVal e As EventArgs) Handles btnUpdate.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(Me, Me.GetType(), "ShowSuccess", "javascript:Display('" & lblFullName.Text & "')", True)
        End If
    End Sub

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCancel.Click
        Response.Redirect("~/Default.aspx")
    End Sub
End Class


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend