You must Sign In to post a response.
  • Category: .NET

    Gridview not storing the records and displaying the last entered records in gridview

    in my application i first need to have a button on clicking the button user should see a pop up with labels and text boxes and on clicking the add button the data should be added to grid view every thing is done but if for the first time i eneter the details of a person and i click on add button then they are added to gridview,again if i enter details of another person then the details that are in gridview for first person are disappered and in gridview the second person details are displayed but it should display both the records also if i again run the application after adding the records then the added records are not displayed in gridview how can i display the added records even i run the application any no of times.


    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    <style type="text/css">
    .Background
    {
    background-color: Black;
    filter: alpha(opacity=90);
    opacity: 0.8;
    }
    .Popup
    {
    background-color: #FFFFFF;
    border-width: 3px;
    border-style: solid;
    border-color: black;
    padding-top: 10px;
    padding-left: 10px;
    width: 400px;
    height: 350px;
    }
    .lbl
    {
    font-size:16px;
    font-style:italic;
    font-weight:bold;
    }
    </style>
    </head>
    <body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:Button ID="Button1" runat="server" Text="Fill Form in Popup" />

    <div style="margin-left:10px;margin-top:10px">
    <asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server" CellPadding="2"
    ForeColor="#333333" GridLines="None">
    <AlternatingRowStyle BackColor="White" />
    <Columns>
    <asp:BoundField HeaderStyle-Width="120px" HeaderText="FirstName" DataField="FirstName" />
    <asp:BoundField HeaderStyle-Width="120px" HeaderText=" MiddleName" DataField="MiddleName" />
    <asp:BoundField HeaderStyle-Width="120px" HeaderText=" LastName" DataField="LastName" />
    <asp:BoundField HeaderStyle-Width="120px" HeaderText="Gender" DataField="Gender" />
    </Columns>
    <EditRowStyle BackColor="#2461BF" />
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#EFF3FB" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#F5F7FB" />
    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
    <SortedDescendingCellStyle BackColor="#E9EBEF" />
    <SortedDescendingHeaderStyle BackColor="#4870BE" />
    </asp:GridView>
    </div>
    <!-- ModalPopupExtender -->
    <cc1:ModalPopupExtender ID="mp1" runat="server" PopupControlID="Panl1" TargetControlID="Button1"
    CancelControlID="Button2" BackgroundCssClass="Background">
    </cc1:ModalPopupExtender>
    <asp:Panel ID="Panl1" runat="server" CssClass="Popup" align="center" style = "display:none">
    <div id="irm1" style=" width: 300px; height: 250px;" runat="server" >
    <table>
    <tr>
    <td>
    <asp:Label ID="Label1" runat="server" CssClass="lbl" Text="First Name"></asp:Label>
    </td>
    <td>
    <asp:TextBox ID="TextBox1" runat="server" Font-Size="14px" ></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>
    <asp:Label ID="Label2" runat="server" CssClass="lbl" Text="Middle Name"></asp:Label>
    </td>
    <td>
    <asp:TextBox ID="TextBox2" runat="server" Font-Size="14px" ></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>
    <asp:Label ID="Label3" runat="server" CssClass="lbl" Text="Last Name"></asp:Label>
    </td>
    <td>
    <asp:TextBox ID="TextBox3" runat="server" Font-Size="14px" ></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>
    <asp:Label ID="Label4" runat="server" CssClass="lbl" Text="Gender"></asp:Label>
    </td>
    <td>
    <asp:TextBox ID="TextBox4" runat="server" Font-Size="14px" ></asp:TextBox>
    </td>
    <td>
    <asp:Button ID="Button3" runat="server" Text="Add" OnClick="Button3_Click" />
    </td>
    </tr>
    </table>
    </div>
    <br />
    <asp:Button ID="Button2" runat="server" Text="Close" />
    </asp:Panel>
    <!-- ModalPopupExtender -->
    </form>
    </body>
    In my code behind-

    protected void Page_Load(object sender, EventArgs e)
    {
    dt = new DataTable();
    DataColumn dc1 = new DataColumn("FirstName");
    DataColumn dc2 = new DataColumn("MiddleName");
    DataColumn dc3 = new DataColumn("LastName");
    DataColumn dc4 = new DataColumn("Gender");
    dt.Columns.Add(dc1);
    dt.Columns.Add(dc2);
    dt.Columns.Add(dc3);
    dt.Columns.Add(dc4);
    DataRow dr1 = dt.NewRow();
    GridView1.DataSource = dt;
    GridView1.DataBind();
    }
    DataTable dt;

    protected void Button3_Click(object sender, EventArgs e)
    {
    DataRow dr1 = dt.NewRow();
    dr1[0] = TextBox1.Text;
    dr1[1] = TextBox2.Text;
    dr1[2] = TextBox3.Text;
    dr1[3] = TextBox4.Text;
    dt.Rows.Add(dr1);
    GridView1.DataSource = dt;
    GridView1.DataBind();
    ClearFields();

    }
    protected void ClearFields()
    {
    TextBox1.Text = "";
    TextBox2.Text = "";
    TextBox3.Text = "";
    TextBox4.Text = "";
    }
  • #767130
    Use static datatable or session for the datatable your problem will solved

    if(session["datatable"] !=null)
    {
    dt= (Datatable )session["datatable"];
    DataRow dr1 = dt.NewRow();
    dr1[0] = TextBox1.Text;
    dr1[1] = TextBox2.Text;
    dr1[2] = TextBox3.Text;
    dr1[3] = TextBox4.Text;
    dt.Rows.Add(dr1);
    session["datatable"]= dt;
    }
    else
    {
    DataRow dr1 = dt.NewRow();
    dr1[0] = TextBox1.Text;
    dr1[1] = TextBox2.Text;
    dr1[2] = TextBox3.Text;
    dr1[3] = TextBox4.Text;
    dt.Rows.Add(dr1);
    session["datatable"]= dt;
    }

    SRI RAMA PHANI BHUSHAN KAMBHAMPATI

  • #767132
    Hi,

    The reason behind is Controls won't maintain state over postback, so that the reason Microsoft introduce the concept called as StateManagement, As per Microsoft we have different types of StateManagement techniques available in .net those are

    1) ViewState
    2)Session State
    3) Cache
    4)Cookies
    etc...

    You can go through all the concepts later if you are really interested, for now as per my understanding you want to maintain state with in page so you can use ViewState to maintain the previous entered records.

    Ex:

    DataTable dt=new DataTable();
    if(ViewState["Data"]!=null)
    {
    dt= ViewState["Data"] as DataTable;
    DataRow dr=dt.NewRow();
    dr["Col1"]=lbl1.Text;
    dr["Col2"]=lbl2.Text;
    dt.Rows.Add(dr);
    dt.AcceptChanges();
    ViewState["Data"]=dt;
    }
    else
    {
    dt.Columns.Add("Col1");
    dt.Columns.Add("col2");
    dr["Col1"]=lbl1.Text;
    dr["Col2"]=lbl2.Text;
    dt.Rows.Add(dr);
    dt.AcceptChanges();
    ViewState["Data"]=dt;

    }


    Hope this will helpful to you, if you still have any doubts first go through all StateManagement concepts then try to implement your task.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/


Sign In to post your comments