Almacene varios valores en una variable de sesión

Before I ask my question, please consider this scenario: a user on my website has a profileID. Hay algunos pluginID's associated with this profileID.

P.ej: User1 might have 2, 3 and 5 plugins associated with his profile.

When the user logs in, I store the profileID of the user in a session variable cod. ON a certain page, the user tries to edit the plugins associated with his profile. So, on that page, I have to retrieve those pluginID's from the DB.

I have applied this code but this fetches only the maximum pluginID from the DB and not all the pluginIDes

SqlCommand cmd1 = new SqlCommand("select plugin_id from profiles_plugins where id=(select id from profiles_plugins where profile_id=" + Convert.ToInt32(Session["cod"]) + ")", con);
SqlDataReader dr1 = cmd1.ExecuteReader();
if (dr1.HasRows)
{
  while (dr1.Read())
  {
    Session["edp1"] = Convert.ToInt32(dr1[0]);
  }
}
dr1.Close();
cmd1.Dispose();

I was trying to figure out how can I store multiple pluginID's in this session variable?

Muchas Gracias

preguntado el 09 de enero de 11 a las 09:01

Aside: you should have a using around each of the command, reader, and connection. -

Y deberías parametrizar your query! As a general rule, nunca concatenate together your SQL queries - this opens the door to SQL injection attacks - just don't do it. -

And you shouldn't mix data access and session handling in the same piece of code either. A lot of weird things going on here... Also this might be a duplicate of the "question" varias filas de una sola tabla. -

2 Respuestas

Read the value to a list or array first and then store the list or array:

using(SqlCommand cmd1 = new SqlCommand("select plugin_id from profiles_plugins where id=(select id from profiles_plugins where profile_id=" + Convert.ToInt32(Session["cod"]) + ")", con))
{

    SqlDataReader dr1 = cmd1.ExecuteReader();
    var yourList = new List<int>();
    if (dr1.HasRows)
    {
        while (dr1.Read())
        {
           yourList.Add(Convert.ToInt32(dr1[0]));
        }
    }
    Session["edp1"] = yourList;
    dr1.Close();
}

Also, read Marc Gravell's comment on your question.

respondido 14 mar '13, 00:03

Ok, I was able to store the values in the session using the List but how do I have to retrieve these values from the session now? - Cifra

var yourList = (List<int>Session["edp1"]); should work. But I would really refactor your code so you don't do the data access and session handling in the same place. - Tomas Jansson

Typo in last comment... should be: var yourList = (List<int>)Session["edp1"]; - Tomas Jansson

You can store whatever objects you like under a session key, not only scalar values. So simply define a custom type containing all the values you would like to store, create an instance of this type and put it into the session.

Respondido el 09 de enero de 11 a las 12:01

If you're going to store custom types in session they should be marked as Serializable if you're going to use ASP.NET State Server or SQL State Server - sh1rts

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.