Session和HashTable 的疑问? ( 积分: 100 )

Y

yl9931

Unregistered / Unconfirmed
GUEST, unregistred user!
我的天空 (33706359) 12:41:16
foreach (DataListItem dl in this.DataList1.Items)//遍历集合
{
TextBox tb=(TextBox)dl.FindControl("TextBox1");/ 到文本框
int newfcode=Convert.ToInt32(tb.Text.ToString());//查出文本框里面的值
Hashtable ht=(Hashtable)Session["bus"];//把session["bus"]对象赋值给哈希表 ht
string fcode = DataList1.Items[0].ItemIndex.ToString();
int oldpid=(int)ht[fcode];//求得原来的数量
if (newfcode != oldpid)//如果文本框里的值不等于原来的数量,就用新的更换到哈希表中的值
{
ht[fcode] = newfcode;
}
Session["bus"]=ht;//最后再更新Session 对象 */
}
这段哪错了哦
为什么提示 int oldpid=(int)ht[fcode];这句错误?!?!
错误:未将对象引用设置到对象实例
--------------------------
添加到SESSION,下面这段正常
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
string fcode = this.GridView1.DataKeys[index].Value.ToString();

if (e.CommandName != "AddtoBus")
{
if (Session["bus"] == null)
{
System.Collections.Hashtable ht = new Hashtable();
ht.Add(fcode, 1);
Session["bus"] = ht;
}
else
{
System.Collections.Hashtable ht = (Hashtable)Session["bus"];
if (ht[fcode] == null)
{
ht[fcode] = 1;
}
else
{
ht[fcode] = (int)ht[fcode] + 1;
}
Session["bus"] = ht;
}
}
}
 
你要知道hushtable 和 session都是什么类型 .hushtable应该要求的是有序列表形式存储的。我认为。你把session赋予ht ,可能就有问题了, 而你先把ht 赋予 session应该是给他一个引用的地址。再把session转成ht的时候又把地址指过来了,我是这样认为的。
 
顶部