日期:2014-05-18  浏览次数:20480 次

把 ArrayList 的值存入Session 当从Session 中取值时遇到问题;
向Session存入数据
C# code

  myconnection.Open();
            //详细内容分页
            SqlDataAdapter da = new SqlDataAdapter(mycommand);
            DataSet ds = new DataSet();
            da.Fill(ds, "KHGL_Customer");
            ArrayList myArraylist = new ArrayList();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                myArraylist.Add(ds.Tables[0].Rows[0]["iCustomerId"]);
            }
            Session["Arry"] = myArraylist;
            myconnection.Close();


==========================================================
Session取出数据
C# code

 protected void Page_Load(object sender, EventArgs e)
    {
        ArrayList temp = new ArrayList();
        temp = (ArrayList)Session["Array"];
        for (int i = 0; i < temp.Count; i++)
        {
            Response.Write(temp[i].ToString ());
        }
    }
错误提示
Response.Write(temp[i].ToString ())
[color=#FF0000]未将对象引用设置到对象的实例。[/color]





------解决方案--------------------
建议在向Session存入数据前加个判断
 
C# code

da.Fill(ds, "KHGL_Customer");
            ArrayList myArraylist = new ArrayList();
            if(ds.Tables[0].Rows.Count > 0)//判断
            {
              for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
              {
                myArraylist.Add(ds.Tables[0].Rows[0]["iCustomerId"]);
              }
            }

------解决方案--------------------
myconnection.Open();
//详细内容分页
SqlDataAdapter da = new SqlDataAdapter(mycommand);
DataSet ds = new DataSet();
da.Fill(ds, "KHGL_Customer");
ArrayList myArraylist = new ArrayList();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
myArraylist.Add(ds.Tables[0].Rows[0]["iCustomerId"]);
}
Session["Arry"] = myArraylist;//你存入Session["Arry"] myconnection.Close();

取Session的时候 

protected void Page_Load(object sender, EventArgs e)
{
ArrayList temp = new ArrayList();
temp = (ArrayList)Session["Array"]; //你取的Session["Array"]
for (int i = 0; i < temp.Count; i++)
{
Response.Write(temp[i].ToString ());
}
}


楼主以后写程序还是仔细检查一下吧!
------解决方案--------------------
lz向Sesson里存值写的是
Session["Arry"] = myArraylist;
从Sesson里取值写的是
ArrayList temp = new ArrayList();
temp = (ArrayList)Session["Array"];


Session["Arry"] 与 Session["Array"]是两个Session,key不同

给分吧