Results 1 to 4 of 4

Thread: Image vs Session Variables

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2006
    Posts
    16

    Image vs Session Variables

    Hello,

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

    v_connection.Open();
    SqlDataReader dr = cmd.ExecuteReader();

    if(dr.Read())
    {
    Session["Customer_Photo"] = (byte[])dr["Photo"]; ????? is it correct ?
    }

    dr.Close();
    v_connection.Close();




    Thank you

    I really appreciate your help
    Paul

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Image vs Session Variables

    As the title says I assume you are using Asp.net ? I have requested moderator to move to Asp.net
    Please mark you thread resolved using the Thread Tools as shown

  3. #3
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Image vs Session Variables

    Try this one. Un tested..


    public void ReadImage()
    {
    string conn = ConfigurationManager.ConnectionStrings ["SqlServer"].ConnectionString;
    SqlConnection connection = new SqlConnection(conn);
    string sql = "SELECT [Photo] FROM [customers] WHERE [code] = @CODE";
    SqlCommand cmd = new SqlCommand(sql,connection);
    cmd.CommandType = CommandType.Text;
    cmd.Parameters.AddWithValue("@CODE", v_customer_code
    connection.Open();
    object img = cmd.ExecuteScalar();
    try
    {
    //Store in Session
    Session["Logo"]=img;
    }
    catch
    {
    return null;
    }
    finally
    {
    connection.Close();
    }
    }




    }



    //Show Image
    Image1.Image=Image.FromStream(new MemoryStream((byte[]) Session["Logo"]));
    Last edited by danasegarane; Aug 29th, 2011 at 09:10 PM.
    Please mark you thread resolved using the Thread Tools as shown

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Image vs Session Variables

    Hello,

    It probably makes sense to first convert the image to a byte array, and then store it in a Session Variable.

    Gary

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width