Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have save image in ms sql database with datatype Image but when i try to fetch image from database it show me error parameter is not valid
Code Is:
C#
string str = "Select * from Document where CustId=" + cmbCustomerName.SelectedValue.ToString() + "";
                  con.Open();
                SqlCommand cmd = new SqlCommand(str,con);
                SqlDataReader dr = null;
                dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        byte[] picarr = (byte[])dr["Photo"];
                        MemoryStream ms = new MemoryStream(picarr);
                        ms.Seek(0, SeekOrigin.Begin);
                        picPhoto.Image = Image.FromStream(ms);
                        
                    }
                }
                con.Close();
Posted
Updated 24-Mar-14 21:21pm
v2
Comments
Nirav Prabtani 25-Mar-14 3:21am    
have you debug code???

try this:
C#
using System.Drawing;
if (dr["Photo"].ToString() != "")
{
    Byte[] image = (Byte[])(dr["Photo"]);
    string src = "data:image/jpeg;base64," + Convert.ToBase64String(image);
    picPhoto.ImageUrl = src;
}
else
{
 //if you want to set some default image
picPhoto.ImageUrl = "~/images/DefaultImg.png";
}
 
Share this answer
 
 
Share this answer
 
Have u tried with Bitmap... Whose namespace is "System.Drawing.Bitmap"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900