Start New Discussion within our Software Development Community


I would like to know how to:

  • Save an image into a Session Variables
  • Get the image from the Session Variable and display it on the web page.

Here's the example on how to get the image from the DB:

string v_customer_code = "PAUL";

SqlConnection v_connection = newSqlConnection(ConfigurationManager.ConnectionStrings["SqlServer"].ConnectionString))

v_connection_string = "SELECT [Photo] FROM [customers] WHERE [code] = @CODE";
SqlCommand cmd = new SqlCommand(SQL, v_connection);
cmd.Parameters.AddWithValue("@CODE", v_customer_code);

SqlDataReader dr = cmd.ExecuteReader();

Session["Customer_Photo"] = (byte[])dr["Photo"]; ????? is it correct ?


Thank you

I really appreciate your help

Session variables are not intended to hold objects like images.
IE: if the image has 8MB an was stored in a session variable, the server memory will be filled with the session cookie for this requests until the session expires or you clean the session variable.

If a large number of requests are made to the server, the server memory will be overloaded and the server will crash.

Said that, Yes, your sentence is correct to store the image into a session variable. But..once you stored the photo in the session variable, it should be retrieved as a memory stream from the variable, converted to a bitmap and saved in a temp image folder in order to be presented in the web page.

IMO, there is no reason to store images in SQL server, nor in Session variables.

Hope this helps.

This article has been dead for over six months. Start a new discussion instead.