请检查仅存储最后一个值的代码会话 [英] Please check the code session storing only last value

查看:77
本文介绍了请检查仅存储最后一个值的代码会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

for (int i = 0; i < GridView1.Rows.Count; i++)
            {

                Label ID = (Label)GridView1.Rows[i].Cells[0].FindControl("ID");
                int lID = Convert.ToInt32(ID.Text);
                con.Open();
                using (SqlCommand command = new SqlCommand("select Vendor_Name from VandorDetail where ID=@ID", con))
                { // 2. define parameters used in command object 
                    SqlParameter param = new SqlParameter();
                    param.ParameterName = "@ID"; param.Value = lID; // 3. add new parameter to command object 
                    command.Parameters.Add(param); // // Read in the SELECT results. // 
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    { Session["Vendor_Name"] = Convert.ToString(reader["Vendor_Name"]); }
                    con.Close();
                }
            }

推荐答案

嗯,是的......会的。

你所做的相当于:

Well, yes...it would.
What you have done is the equivalent of:
string myString = "";
for (int i = 0; i < 10; i++)
   {
   myString = i.ToString();
   }

你很惊讶 myString 总是等于9...

分配时对于Session,它与分配给任何其他集合相同:您覆盖以前的内容。

And you are surprised that myString always equals "9"...
When you assign to the Session, it is the same as assigning to any other collections: you overwrite the previous content.


这篇关于请检查仅存储最后一个值的代码会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆