C#会话变量

时间:2014-06-10 16:21:40

标签: c# asp.net

我正在尝试将多个会话变量传递给多个asp页面。但是,只有最后一个ImageID和Extention值传入asp页面。我需要op

    int key = Convert.ToInt32(StockSummary.SelectedRow.Cells[6].Text);
    int index = 1;
    try
    {

        transportFbConn.Open();

        if (transportFbConn.State == ConnectionState.Closed)
        {
            transportFbConn.Open();
        }
        var sqlquerry = String.Format("select image_key,File_EXT from IMAGE_LIST where SOURCE_PK = {0}", key);
        transportFbCommand = new FbCommand(sqlquerry, transportFbConn);
        transportFbReader = transportFbCommand.ExecuteReader();
        if (transportFbReader.HasRows)
        {
            while (transportFbReader.Read())
            {
                ImageID = transportFbReader.GetString(0);
                extention = transportFbReader.GetString(1);
                //Open PDF:
                if (ImageID != "")
                {
                    Session.Add("IMGID", ImageID);
                    Session.Add("Ext", extention);
                    Response.Write(string.Format("<script>window.open('{0}','_blank');</script>", "Trace"+index+".aspx"));
                }
                else
                {
                    this.ErrorLabel.Text = "No Trace Information found for Part Number : " + this.TextTextBox.Text;
                }
                index++;
            }
        }

我尝试了arraylist,但我得到了对象null引用:

 int key = Convert.ToInt32(StockSummary.SelectedRow.Cells[6].Text);
        int index = 1;
        int arrayindex = 0;
        ArrayList imageid = new ArrayList();
        ArrayList extention = new ArrayList();
        try
        {

            transportFbConn.Open();

            if (transportFbConn.State == ConnectionState.Closed)
            {
                transportFbConn.Open();
            }
            var sqlquerry = String.Format("select image_key,File_EXT from IMAGE_LIST where SOURCE_PK = {0}", key);
            transportFbCommand = new FbCommand(sqlquerry, transportFbConn);
            transportFbReader = transportFbCommand.ExecuteReader();
            if (transportFbReader.HasRows)
            {
                while (transportFbReader.Read())
                {
                    imageid.Insert(arrayindex, transportFbReader.GetString(0));
                    extention.Insert(arrayindex, transportFbReader.GetString(1));
                    //Open PDF:
                    if (ImageID[arrayindex].ToString() != "")
                    {
                        Session.Add("IMGID", ImageID[arrayindex]);
                        Session.Add("Ext", extention[arrayindex]);
                        Response.Write(string.Format("<script>window.open('{0}','_blank');</script>", "Trace"+index+".aspx"));
                    }
                    else
                    {
                        this.ErrorLabel.Text = "No Trace Information found for Part Number : " + this.TextTextBox.Text;
                    }
                    index++;
                    arrayindex++;
                }
            }

3 个答案:

答案 0 :(得分:1)

如果存在具有相同键值的会话变量,则会覆盖该变量。因此,您始终使用最新值更新相同的密钥。

答案 1 :(得分:1)

您应该在会话中放置一个ImageId列表而不是单个图像,因为您只能有一个由同一个键表示的对象。它通常会在你的循环中被覆盖。

答案 2 :(得分:1)

最好创建一个数组,将所有键,值存储在临时变量中,然后将其存储在会话中。 因为使用相同的密钥会覆盖值

ArrayList ar_IMGID = new ArrayList();
ArrayList ar_Ext = new ArrayList();

while (transportFbReader.Read())
            {
                ImageID = transportFbReader.GetString(0);
                extention = transportFbReader.GetString(1);
                //Open PDF:
                if (ImageID != "")
                {

                    ar_IMGID.Add(ImageID);
                    ar_Ext.Add(extention);
                 }
             }
Session.Add("IMGID", ar_ImageID);
Session.Add("Ext", ar_Ext);