【问题标题】:unable to retrieve data from cache in ASP.NET无法从 ASP.NET 中的缓存中检索数据
【发布时间】:2012-04-20 20:19:57
【问题描述】:

我是 ASP.NET 的新手,

我正在制作国家,州下拉列表。

例如:对于特定国家/地区,我将从缓存文件中读取该国家/地区的状态。

LOGIC : 当用户第一次访问时,此时缓存为空,因此将从XMLFile获取相应的状态并将其状态插入缓存中,下一步用户访问同一个国家的时间,因此它的状态应该从缓存中获取,而不是从XMLFile

我无法在我的下拉列表中获取所需国家/地区的状态...

这是我的代码 sn-p,

protected void DropDownListCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
           string  SelectedCountry = DropDownListCountry.SelectedItem.Text;

           XDocument doc = XDocument.Load(Server.MapPath("XMLFile.xml"));

           var query = from country in doc.Descendants("country")
                       where country.Attribute("name").Value == SelectedCountry
                       select new
                       {
                           states = from state in country.Elements("state")
                                    select new{
                                        Name = state.Attribute("name").Value
                                    }
                       };

           DataTable dt = new DataTable();
           dt.Columns.Add(new DataColumn("Name"));

            if (Cache["key"]==null)
            {
                foreach (var st in query)
                {
                    foreach (var StateName in st.states)
                    {
                        DataRow dr = dt.NewRow();
                        dr["Name"] = StateName.Name;
                        dt.Rows.Add(dr);
                        Cache.Insert("key", dr);

                    }
                }
                DropDownListState.DataSource = dt;
                DropDownListState.DataTextField = "Name";
                DropDownListState.DataBind();


            }
            else
            {
                      object obj = Cache["key"];
                      dt.Rows.Add(obj);

                DropDownListState.DataSource = dt;
                DropDownListState.DataTextField = "Name";
                DropDownListState.DataBind();
            }        
    }

提前致谢!

【问题讨论】:

    标签: asp.net xml caching drop-down-menu bind


    【解决方案1】:

    这段代码几乎没有问题,比如数据和 UI 的处理方式、缓存的使用以及一个错误,每个对象的缓存键应该是唯一的。

    所以让我们稍微重构一下代码并创建一个方法,该方法返回一个按国家/地区名称显示各州的 DataTable。如果状态已经在缓存中,则返回缓存中的DataTable。

    private static DataTable GetStates(string countryName)
    {
       DataTable dt = HttpContext.Current.Cache[countryName] as DataTable;
       if (dt != null)
           return dt;
    
       // the datatable is not in cache, let's create it then and add it to cache, next call will pick it from cache and won't read and parse the xml again
       var doc = XDocument.Load(HttpContext.Current.Server.MapPath("XMLFile.xml"));
       var query = from country in doc.Descendants("country")
                     where country.Attribute("name").Value == countryName
                     select new
                     {
                         states = from state in country.Elements("state")
                                  select new
                                  {
                                      Name = state.Attribute("name").Value
                                  }
                     };
    
      dt = new DataTable();
      dt.Columns.Add(new DataColumn("Name"));
    
     foreach (var st in query)
                {
                    foreach (var StateName in st.states)
                    {
                        DataRow dr = dt.NewRow();
                        dr["Name"] = StateName.Name;
                        dt.Rows.Add(dr);
    
                    }
                }
     // add the data table in the cache (not a row and don't use the same key)
     HttpContext.Current.Cache.Insert(countryName, dt);   
     // return this table
     return dt;
    }
    

    现在是事件处理程序

    protected void DropDownListCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
           var countryName = DropDownListCountry.SelectedItem.Text;
           var dt = GetStates(countryName);
    
           DropDownListState.DataSource = dt;
           DropDownListState.DataTextField = "Name"; // you can set it in markup also
           DropDownListState.DataBind();
        }
    

    缓存由所有用户共享,所以如果这是您的意图,那么可以使用它。

    【讨论】:

    • 它显示像object ref required for non static field system.web.caching.cache.insert(string,object)这样的错误
    • 尝试使用 HttpContext.Current.Cache,我刚刚编辑了我的答案
    • object reference is required for the nonstatic field method or property var doc 中的错误 = XDocument.Load(Server.MapPath("XMLFile.xml"));
    • 您也必须填充该下拉列表
    • 干得好,伙计..它正在工作......如果我可以投票你的回答 100 次.. :)
    【解决方案2】:

    这样的事情可能会有所帮助:

    private DataTable PopulateCountryList()
    {
      DataTable dt = (DataTable)Cache["countries"];
    
      if (dt == null)
      {
        dt.Columns.Add(new DataColumn("Name"));
    
        XDocument doc = XDocument.Load(Server.MapPath("XMLFile.xml"));
    
        var query = from country in doc.Descendants("country")
        where country.Attribute("name").Value == SelectedCountry
        select new
        {
            states = from state in country.Elements("state")
            select new {
            Name = state.Attribute("name").Value
            }
        };
    
        foreach (var st in query)
        {
            foreach (var StateName in st.states)
            {
              DataRow dr = dt.NewRow();
              dr["Name"] = StateName.Name;
              dt.Rows.Add(dr);    
            }
        }
    
        Cache.Insert("countries", dt, null, DateTime.Today.AddDays(1), TimeSpan.Zero);
      }
    
      return = dt;
    }
    

    当你想填充他的下拉列表时:

    DropDownListState.DataSource = PopulateCountryList();
    DropDownListState.DataTextField = "Name"; 
    DropDownListState.DataBind();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-28
      • 2011-04-18
      • 1970-01-01
      • 2020-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多