locked
Project using only temporary table. RRS feed

  • Question

  • User1015862149 posted

    I need to develop the project by using the temporary table (without using any database) and then display in Gridview and update and deleting the data in grid is possible using C# or VB.net

    Saturday, September 7, 2013 5:48 AM

Answers

  • User697462465 posted

    Hi raj,

    According to your description, my understanding is that you would like to implement update, delete in GridView by dynamic data.

    If so, please try to refer to the following code:

    In the aspx file:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="GridViewForDynamicData.WebForm1" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <asp:GridView ID="TaskGridView" runat="server" 
            AutoGenerateEditButton="True" 
            AllowPaging="true"
            OnRowEditing="TaskGridView_RowEditing"         
            OnRowCancelingEdit="TaskGridView_RowCancelingEdit" 
            OnRowUpdating="TaskGridView_RowUpdating"
            OnPageIndexChanging="TaskGridView_PageIndexChanging"
            AutoGenerateColumns="false"
            OnRowDataBound="TaskGridView_DataBound"
            OnRowDeleting="TaskGridView_Deleting"
            AutoGenerateDeleteButton="true">
              <Columns>
                  <asp:BoundField DataField="Price" HeaderText="Price" DataFormatString="{0:C}" />
                  <asp:BoundField DataField="Description" HeaderText="Description" />
                  <asp:CheckBoxField DataField="IsComplete" HeaderText="IsPay" />
                  <asp:TemplateField>
                      <ItemTemplate>
                          <asp:Label ID="Label1" runat="server" Text='<%#Eval("Price")%>'></asp:Label>
                      </ItemTemplate>
                      <EditItemTemplate>
                          <asp:TextBox runat="server" ID="txtBox" Text='<%#Eval("Price") %>' Height="22px" Width="128px"></asp:TextBox>
                      </EditItemTemplate>
                  </asp:TemplateField>
              </Columns>
          </asp:GridView>
        </div>
        </form>
    </body>
    </html>
    

    In the aspx.cs file:

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace GridViewForDynamicData
    {
        public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
                if (!Page.IsPostBack)
                {
                    // Create a new table.
                    DataTable taskTable = new DataTable("TaskList");
    
                    // Create the columns.
                    taskTable.Columns.Add("Price", typeof(int));
                    taskTable.Columns.Add("Description", typeof(string));
                    taskTable.Columns.Add("IsComplete", typeof(bool));
    
                    //Add data to the new table.
                    for (int i = 0; i < 20; i++)
                    {
                        DataRow tableRow = taskTable.NewRow();
                        tableRow["Price"] = i;
                        tableRow["Description"] = "Task " + i.ToString();
                        tableRow["IsComplete"] = false;
                        taskTable.Rows.Add(tableRow);
                    }
    
                    //Persist the table in the Session object.
                    Session["TaskTable"] = taskTable;
    
                    //Bind data to the GridView control.
                    BindData();
                }
    
            }
    
            protected void TaskGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                TaskGridView.PageIndex = e.NewPageIndex;
                //Bind data to the GridView control.
                BindData();
            }
    
            protected void TaskGridView_RowEditing(object sender, GridViewEditEventArgs e)
            {
                //Set the edit index.
                TaskGridView.EditIndex = e.NewEditIndex;
                //Bind data to the GridView control.
                BindData();
            }
    
            protected void TaskGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
            {
                //Reset the edit index.
                TaskGridView.EditIndex = -1;
                //Bind data to the GridView control.
                BindData();
            }
    
            protected void TaskGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
            {
                //Retrieve the table from the session object.
                DataTable dt = (DataTable)Session["TaskTable"];
    
                //Update the values.
                GridViewRow row = TaskGridView.Rows[e.RowIndex];
                dt.Rows[row.DataItemIndex]["Price"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
                dt.Rows[row.DataItemIndex]["Description"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
                dt.Rows[row.DataItemIndex]["IsComplete"] = ((CheckBox)(row.Cells[3].Controls[0])).Checked;
    
                //Reset the edit index.
                TaskGridView.EditIndex = -1;
    
                //Bind data to the GridView control.
                BindData();
            }
            protected void TaskGridView_DataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    if (((CheckBox)(e.Row.Cells[3].Controls[0])).Checked)
                    {
                        e.Row.BackColor = System.Drawing.Color.Yellow;
                    }
                }
            }
            private void BindData()
            {
                TaskGridView.DataSource = Session["TaskTable"];
                TaskGridView.DataBind();
            }
    
            protected void TaskGridView_Deleting(object sender, GridViewDeleteEventArgs e)
            {
                //Retrieve the table from the session object.
                DataTable dt = (DataTable)Session["TaskTable"];
    
                //Update the values.
                GridViewRow row = TaskGridView.Rows[e.RowIndex];
               dt.Rows.Remove(dt.Rows[row.DataItemIndex]);
                //Bind data to the GridView control.
                BindData();
            }
        }
    }
    

    Hope it can help you, if there have anything unclear, please let me know.

    Best Regards,
    Terry Guo

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 9, 2013 8:34 AM
  • User-2129951835 posted

    There are mainly two type temporary table : (except global temporary tables)

    temp table who store on tempdb, the table will drop when session is end.

    Another type is table variables, just usable in a query.

    both of them are not suitable use for dynamic data or entity framework.

     

    On desktop application or web application are work if you are using traditional SQL, but not work if using entity framework similar above condition.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 11, 2013 8:12 AM

All replies

  • User697462465 posted

    Hi raj,

    According to your description, my understanding is that you would like to implement update, delete in GridView by dynamic data.

    If so, please try to refer to the following code:

    In the aspx file:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="GridViewForDynamicData.WebForm1" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <asp:GridView ID="TaskGridView" runat="server" 
            AutoGenerateEditButton="True" 
            AllowPaging="true"
            OnRowEditing="TaskGridView_RowEditing"         
            OnRowCancelingEdit="TaskGridView_RowCancelingEdit" 
            OnRowUpdating="TaskGridView_RowUpdating"
            OnPageIndexChanging="TaskGridView_PageIndexChanging"
            AutoGenerateColumns="false"
            OnRowDataBound="TaskGridView_DataBound"
            OnRowDeleting="TaskGridView_Deleting"
            AutoGenerateDeleteButton="true">
              <Columns>
                  <asp:BoundField DataField="Price" HeaderText="Price" DataFormatString="{0:C}" />
                  <asp:BoundField DataField="Description" HeaderText="Description" />
                  <asp:CheckBoxField DataField="IsComplete" HeaderText="IsPay" />
                  <asp:TemplateField>
                      <ItemTemplate>
                          <asp:Label ID="Label1" runat="server" Text='<%#Eval("Price")%>'></asp:Label>
                      </ItemTemplate>
                      <EditItemTemplate>
                          <asp:TextBox runat="server" ID="txtBox" Text='<%#Eval("Price") %>' Height="22px" Width="128px"></asp:TextBox>
                      </EditItemTemplate>
                  </asp:TemplateField>
              </Columns>
          </asp:GridView>
        </div>
        </form>
    </body>
    </html>
    

    In the aspx.cs file:

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace GridViewForDynamicData
    {
        public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
                if (!Page.IsPostBack)
                {
                    // Create a new table.
                    DataTable taskTable = new DataTable("TaskList");
    
                    // Create the columns.
                    taskTable.Columns.Add("Price", typeof(int));
                    taskTable.Columns.Add("Description", typeof(string));
                    taskTable.Columns.Add("IsComplete", typeof(bool));
    
                    //Add data to the new table.
                    for (int i = 0; i < 20; i++)
                    {
                        DataRow tableRow = taskTable.NewRow();
                        tableRow["Price"] = i;
                        tableRow["Description"] = "Task " + i.ToString();
                        tableRow["IsComplete"] = false;
                        taskTable.Rows.Add(tableRow);
                    }
    
                    //Persist the table in the Session object.
                    Session["TaskTable"] = taskTable;
    
                    //Bind data to the GridView control.
                    BindData();
                }
    
            }
    
            protected void TaskGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                TaskGridView.PageIndex = e.NewPageIndex;
                //Bind data to the GridView control.
                BindData();
            }
    
            protected void TaskGridView_RowEditing(object sender, GridViewEditEventArgs e)
            {
                //Set the edit index.
                TaskGridView.EditIndex = e.NewEditIndex;
                //Bind data to the GridView control.
                BindData();
            }
    
            protected void TaskGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
            {
                //Reset the edit index.
                TaskGridView.EditIndex = -1;
                //Bind data to the GridView control.
                BindData();
            }
    
            protected void TaskGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
            {
                //Retrieve the table from the session object.
                DataTable dt = (DataTable)Session["TaskTable"];
    
                //Update the values.
                GridViewRow row = TaskGridView.Rows[e.RowIndex];
                dt.Rows[row.DataItemIndex]["Price"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
                dt.Rows[row.DataItemIndex]["Description"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
                dt.Rows[row.DataItemIndex]["IsComplete"] = ((CheckBox)(row.Cells[3].Controls[0])).Checked;
    
                //Reset the edit index.
                TaskGridView.EditIndex = -1;
    
                //Bind data to the GridView control.
                BindData();
            }
            protected void TaskGridView_DataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    if (((CheckBox)(e.Row.Cells[3].Controls[0])).Checked)
                    {
                        e.Row.BackColor = System.Drawing.Color.Yellow;
                    }
                }
            }
            private void BindData()
            {
                TaskGridView.DataSource = Session["TaskTable"];
                TaskGridView.DataBind();
            }
    
            protected void TaskGridView_Deleting(object sender, GridViewDeleteEventArgs e)
            {
                //Retrieve the table from the session object.
                DataTable dt = (DataTable)Session["TaskTable"];
    
                //Update the values.
                GridViewRow row = TaskGridView.Rows[e.RowIndex];
               dt.Rows.Remove(dt.Rows[row.DataItemIndex]);
                //Bind data to the GridView control.
                BindData();
            }
        }
    }
    

    Hope it can help you, if there have anything unclear, please let me know.

    Best Regards,
    Terry Guo

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 9, 2013 8:34 AM
  • User-2129951835 posted

    If you are looking for ASP.NET Dynamic Data application.

    You can't.

    ASP.NET Dynamic Data need a concrete database and concrete table to create the data context and the feild.

    By the way, i am not quite understand what is using the temporary table (without using any database)

    Monday, September 9, 2013 12:32 PM
  • User1015862149 posted

    hi dude

        Thank U for reply me... this the question was asked for my job interview (For praticle session)....

    Wednesday, September 11, 2013 1:41 AM
  • User1015862149 posted

    Hi Dude

       thank u for u repling. weather this task is possible in Desktop application.....

     

    Wednesday, September 11, 2013 1:47 AM
  • User-2129951835 posted

    There are mainly two type temporary table : (except global temporary tables)

    temp table who store on tempdb, the table will drop when session is end.

    Another type is table variables, just usable in a query.

    both of them are not suitable use for dynamic data or entity framework.

     

    On desktop application or web application are work if you are using traditional SQL, but not work if using entity framework similar above condition.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 11, 2013 8:12 AM