ASP.NET - Using Cache in asp.net

Asked By avula
31-Jan-12 04:59 AM
Hi Every one,

please help me for this requirement thanks in advance.

i have one text box  and one grivew , whenever i add value from text box to grid its should be added in gridview using cache , and i need delete functionality also
  James H replied to avula
31-Jan-12 05:04 AM

Caching Options in ASP.NET


ASP.NET supports three types of caching for Web-based applications:
  • Page Level Caching (called Output Caching)
  • Page Fragment Caching (often called Partial-Page Output Caching)
  • Programmatic or Data Caching

http://www.4guysfromrolla.com/articles/022802-1.aspx

  smr replied to avula
31-Jan-12 05:05 AM
hi

refer this

private const string CUSTOMER_CACHE_KEY = "CUSTOMER_DATA";
private const string CUSTOMERCOUNT_CACHE_KEY = "CUSTOMER_COUNT";
 
    public static DataTable GetCustomersSortedPage
  (int maximumRows, int startRowIndex,
    string sortExpression, string searchCriteria)
    {
      if (string.IsNullOrEmpty(sortExpression))
        sortExpression = "customerkey";
      try
      {
        if (AdvWorksDBCache.isRecordsCached(CUSTOMER_CACHE_KEY))
          return AdvWorksDBCache.GetData
      (CUSTOMER_CACHE_KEY, startRowIndex + 1,
      maximumRows, sortExpression, searchCriteria);
 
        SqlConnection dbConnection = new SqlConnection
    (ConfigurationManager.ConnectionStrings["SQLDBConString"].ToString());
        string sql = "select Cust.*,G.City from
    DimCustomer Cust inner join DimGeography G on
    G.GeographyKey = Cust.GeographyKey ";
        SqlCommand custCommand = new SqlCommand(sql, dbConnection);
 
        custCommand.CommandType = CommandType.Text;
 
        SqlDataAdapter ad = new SqlDataAdapter(custCommand);
        DataTable dtCustomers = new DataTable();
        ad.Fill(dtCustomers);
        dbConnection.Close();
 
        //Cache records
        AdvWorksDBCache.Add(CUSTOMER_CACHE_KEY,dtCustomers);
      }
      catch (Exception e)
      {
        throw;
      }
      return AdvWorksDBCache.GetData(CUSTOMER_CACHE_KEY, startRowIndex + 1,
    maximumRows, sortExpression, null);
    }


follow
http://www.codeproject.com/Articles/106678/Display-Large-Amount-of-Data-in-GridView-with-Sear
  D Company replied to avula
31-Jan-12 05:07 AM
Hello

give a try to this

You can add those TextBox value to DataTable and bind the DataTable to your GridView.

Example on button click:

DataTable dt = (DataTable)Session["CACHEDATA1"]; // cache data to session

if ( dt == null ) {

dt = new DataTable("MyTable");

dt.Columns.Add("Col1");

dt.Columns.Add("Col2");

dt.Columns.Add("Col3");

dt.Columns.Add("Col4");

}

dt.LoadDataRow(new object[]{TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text});

Session["CACHEDATA1"] = dt;

GridView1.DataSource = dt;

GridView1.DataBind();

to delete it

public void RemoveItemFromCache(Object sender, EventArgs e) {
    if(Cache["Key1"] != null)
    Cache.Remove("Key1");
}


Regards
D

  Danasegarane Arunachalam replied to avula
31-Jan-12 05:09 AM


STEP 1:
Add One TextBox, One Button and One GridView control the web form. The ASPX mark-up should look like these below

<asp:TextBox ID="TextBox1" runat="server"/>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:GridView ID="GridView1" runat="server" >
</asp:GridView>

STEP 2:
 
Create the method that will BIND the GridView based on the TextBox values and retain its values on post backs.


private void BindGrid(int rowcount)
  {
    DataTable dt = new DataTable();
    DataRow dr;
    dt.Columns.Add(new System.Data.DataColumn("TextBox1Column", typeof(String)));
  
    if (ViewState["CurrentData"] != null)
    {
      for (int i = 0; i < rowcount + 1; i++)
      {
        dt = (DataTable)ViewState["CurrentData"];
        if (dt.Rows.Count > 0)
        {
          dr = dt.NewRow();
          dr[0] = dt.Rows[0][0].ToString();
  
        }
      }
      dr = dt.NewRow();
      dr[0] = TextBox1.Text;
      dt.Rows.Add(dr);
  
    }
    else
    {
      dr = dt.NewRow();
      dr[0] = TextBox1.Text;
      dt.Rows.Add(dr);
  
    }
  
    // If ViewState has a data then use the value as the DataSource
    if (ViewState["CurrentData"] != null)
    {
      GridView1.DataSource = (DataTable)ViewState["CurrentData"];
      GridView1.DataBind();
    }
    else
    {
    // Bind GridView with the initial data assocaited in the DataTable
      GridView1.DataSource = dt;
      GridView1.DataBind();
  
    }
    // Store the DataTable in ViewState to retain the values
    ViewState["CurrentData"] = dt;
  
  }


Note that we store the DataTable in ViewState to retain the data values associated within the DataTable and use that as our GridView DataSource when it post back to the server.
 
STEP 3:
 
Binding the GridView on Button_Click event.

protected void Button1_Click(object sender, EventArgs e)
   {
     // Check if the ViewState has a data assoiciated within it. If
     if (ViewState["CurrentData"] != null)
     {
       DataTable dt = (DataTable)ViewState["CurrentData"];
       int count = dt.Rows.Count;
       BindGrid(count);
     }
     else
     {
       BindGrid(1);
     }
     TextBox1.Text = “”;
     TextBox1.Focus();
   }
  avula replied to D Company
31-Jan-12 05:29 AM
hi
thanks it works
RemoveItemFromCache where i use this, may i use in gridview_row deleting how can i call this ??
  D Company replied to avula
31-Jan-12 05:50 AM
Yes you can use this method whereever required.

In gridview ro deleting event call this like below example in MSDN

http://msdn.microsoft.com/en-us/library/system.web.caching.cache.remove.aspx
Regards
D
Create New Account
help
alot var Hi What are Master Pages in ASP.NET? or What is a Master Page? ASP.NET master pages allow you to create a consistent layout for the pages in your application. A single master page defines the look and feel and standard behavior that you want for all of the you want to display. When users request the content pages, they merge with the master page to produce output that combines the layout of the master page with the content from the content page. What are the 2 important parts of a master page? The following are the 2 important parts of a master page 1. The Master Page itself 2. One or more Content Pages Can Master Pages be nested? Yes, Master Pages
Page Directive? What is the purpose of page directive in aspx page? Directive Syntax Directives are instructions used to specify settings (related to how a page should render and processed) used by the page and user control compilers when they process ASP.NET Web Forms page (.aspx) and user control (.ascx) files. These are the essential part of every ASP.NET Page or Control. Directives can be located anywhere in an .aspx or .ascx file, but the values, same as any HTML tag) that are specific to that directive. Special Note: The @ Page directive can be used only in .aspx files, and the @ Control directive can be used
summary> / / / <param name = "command"> The SqlCommand to be prepared< / param> / / / <param name = "connection"> A valid SqlConnection, on which to execute this command< / param> / / / <param name = "transaction"> A valid SqlTransaction, or 'null was opened by the method, otherwose is false.< / param> private static void PrepareCommand(SqlCommand command, SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters, out bool mustCloseConnection ) { if( command = = null ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders"); / / / < / remarks> / / / <param name = "connectionString"> A valid connection string for a SqlConnection< / param> / / / <param name = "commandType"> The CommandType (stored procedure, text, etc.)< / param> / / / <param name = "commandText"> The PublishOrders", new SqlParameter("@prodid", 24)); / / / < / remarks> / / / <param name = "connectionString"> A valid connection string for a SqlConnection< / param> / / / <param name = "commandType"> The CommandType (stored procedure, text, etc.)< / param> / / / <param name = "commandText"> The SqlParameter[] commandParameters) { if( connectionString = = null | | connectionString.Length = = 0 ) throw new ArgumentNullException( "connectionString" ); / / Create & open a SqlConnection, and dispose of it after we are done using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); / / Call the overload that takes a connection in place of the connection ExecuteNonQuery(connString, "PublishOrders", 24, 36); / / / < / remarks> / / / <param name = "connectionString"> A valid connection string for a SqlConnection< / param> / / / <param name = "spName"> The name of the stored prcedure< / param> / / / <param name = "parameterValues"> An
Remove(e.TabPage); } _cache.Add(e.TabPage); } } private void button1_Click(object sender, EventArgs e) { TabPage page = new TabPage("TabPage" + pageNumber.ToString()); AddControlsToTabPage(page); this.tabControl1.TabPages.Add(page); pageNumber++; this.tabControl1.SelectedTab = page; } private void AddControlsToTabPage(TabPage page) { page.Controls.Add(this.tab1CheckBox1); page.Controls.Add(this.tabCheckBox2 page.Controls.Add(this.tabCheckBox3); page.Controls.Add(this.tabCheckBox4); page.Controls.Add(this.tabCheckBox5); page