My list session is not retriving the right value

Viewed 50

I have a list to save the orders the client had selected and i wanted to pass the values on the list to other page so i was trying to use

HttpContext.Current.Session["list"] = MySelected;

but I am not getting the values in my other page.

order page:

protected void ButtonCreate_Click(object sender, EventArgs e)
{
    string MyPkList = string.Join(",", MySelected);    
    SqlCommand cmdSQLCount = new SqlCommand("select Count(*) from [EncomendaTEMP] where No_ IN(" + MyPkList + ")", con);
        cmdSQLCount.Connection.Open();
        int qtd = 0;
        SqlDataReader reader = cmdSQLCount.ExecuteReader();
        while (reader.Read())
            qtd = reader.GetInt32(0);
        if (qtd > 0)
        {
            HttpContext.Current.Session["list"] = MySelected;
            Response.Redirect("Booking.aspx", false);                
        }
        else
        {
            Response.Write("<h2> Nenhum registo encontrado! </h2>");
        }

Booking page:

protected void Page_Load(object sender, EventArgs e)
{
     var list = HttpContext.Current.Session["list"];
     Response.Write("<h2> PK = "+list+" </h2>");
}

returns: enter image description here

1 Answers

To get join value of element in List<string>, try:

var list = HttpContext.Current.Session["list"] as List<string>; 
string items = string.Join(",", list);

from @Chetan Ranpariya 's comment.

Related