Session Management in ASP.Net MVC using SQLServer mode in the SessionState RRS feed

  • Question

  • User-1928001151 posted

    How Can I Get The Session Value from ASPState Db, Where I Am Storing The Session Details Using Sqlserver Mode.

    This is what i have tried to fetch data from ASPState database.

    public ActionResult Home()
    ViewBag.Result =Session["username"].ToString();

    SqlCommand cmd = new SqlCommand("select SessionId from
    ASPStateTempSessions", con);

    byte[] bytdata = new byte[50];
    SqlDataReader dr = cmd.ExecuteReader();
    var list = new List<LoginUser>();
    string obj = dr["SessionId"].ToString();
    = System.Text.Encoding.UTF8.GetBytes(obj);
    System.IO.MemoryStream ms = new System.IO.MemoryStream(bytdata);
    BinaryFormatter bin = new BinaryFormatter();
    //bin.Serialize(ms, bytdata);
    //list = (List<LoginUser>)bin.Deserialize(ms);

    string session = Convert.ToString(bin.Deserialize(ms));
    ViewBag.Data = list;
    return View();



    This is what i have configured ion Web.config

    <sessionState mode="SQLServer" customProvider="DefaultSessionProvider">
        <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=, 
    Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="SessionSqlCon" cookieless="false" timeout="10" /> </providers> </sessionState>




    </div> </div>
    Tuesday, August 4, 2015 2:01 AM

All replies

  • User-219423983 posted

    Hi Sunil Revankar,

    You could refer to the following link that explains the Session State Providers. Through this article, you could know to get the session and how to write the value to database. Especially the “Writing Sessions to the Database” section would be helpful to you.


    I hope it’s useful to you.

    Best Regards,

    Weibo Zhang

    Wednesday, August 5, 2015 1:53 AM