user3314399
user3314399

Reputation: 325

C# Session Variables

I am trying to pass multiple session variables to multiple asp pages. However, only the last ImageID and Extention values pass into the asp pages. I need to 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++;
            }
        }

I tried the arraylist but I get the object null reference:

 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++;
                }
            }

Upvotes: 0

Views: 269

Answers (3)

Hazem Torab
Hazem Torab

Reputation: 379

it's better to create an array that store all keys, values in a temp variable then to store it on the session. because using the same key will overwrite the value

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);

Upvotes: 1

TGH
TGH

Reputation: 39248

You should put a list of ImageIds in session rather than individual images since you can only have one object represented by the same key. It basically gets overwritten every time through your loop.

Upvotes: 1

dotnetom
dotnetom

Reputation: 24901

If there is a session variable with the same key value it is overwritten. So you always update the same key with the newest value.

Upvotes: 1

Related Questions