Retrieving/Decoding base64 string from database to image and display in image control

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • David Kawlni
    New Member
    • Nov 2010
    • 1

    Retrieving/Decoding base64 string from database to image and display in image control

    Database backend:Postgre SQL
    Database Name: MoviesDirectory
    Table Name: picture
    Column: img DataType:bytea

    First, I store my uploaded images which may be either .jpg,.gif,.png with the following codes:
    Code:
    ....
    ....
    ....
    if (FileUpload1.PostedFile.ContentLength != 0)
                {
                    byte[] b = new byte[FileUpload1.PostedFile.ContentLength];
                    FileUpload1.PostedFile.InputStream.Read(b, 0, b.Length);
                    string basestring = System.Convert.ToBase64String(b, 0, b.Length);
                    TextBox1.Text = basestring;
                }
    ....
    ...
    ...
    ConnStr = ConfigurationManager.ConnectionStrings["connect2MD"].ToString();
                Conn = new NpgsqlConnection(ConnStr);
                Conn.Open();
                try
                {
                   
                    strSQL = new StringBuilder();
                    strSQL.Append("INSERT INTO picture(img) VALUES (@img)");
                    command = new NpgsqlCommand(strSQL.ToString(), Conn);
                    
                    param = new NpgsqlParameter();
                    param.ParameterName = "@img";
                    param.Value = TextBox1.Text;
                    command.Parameters.Add(param);
    
                    command.ExecuteNonQuery();
    
    
                }
                catch (Exception ex)
                {
                    throw (ex);
                }
                finally
                {
                    if (Conn.State == ConnectionState.Open)
                        Conn.Close();
                }
    ...
    ...
    ..
    The question is that - I want to retrieve back that I have already stored the base64 string and display it with the IMAGE CONTROL on my form.
    Last edited by Dormilich; Nov 11 '10, 07:00 AM.
Working...