Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
private void button1_Click(object sender, EventArgs e)
{
// Show Image
this.Cursor = Cursors.WaitCursor;
con = new SqlConnection("Data Source=GT-9\\SQLEXPRESS;Initial Catalog=BMS_Express;User ID=sa;Password=123");
MemoryStream stream = new MemoryStream();
try
{
DataSet ds = new DataSet();
con.Open();
SqlDataAdapter ad = new SqlDataAdapter("select photo from tbl_image where id=1", con);
ad.Fill(ds);
DataTable dt = ds.Tables[0];
if (dt.Rows.Count > 0)
{
byte[] image = (byte[])dt.Rows[0]["photo"];
MemoryStream ms1 = new MemoryStream(image);
ms1.Seek(0, SeekOrigin.Begin);
pictureBox1.Image = Image.FromStream(ms1); //(Error : Parameter is not valid)
}

}
finally
{
con.Close();
stream.Close();
}
this.Cursor = Cursors.Default;
}
Posted
Updated 12-Dec-14 0:48am

1 solution

1.You do not need to use SqldataAdapter for reading image from database, you should use SqlCommand instead like in the next example:
C#
IDataReader reader = null;
byte[] imageData = null;
            //
            try
            {
                IDbCommand dbCommand = new SqlCommand(); 
                dbCommand.Connection = con;
                dbCommand.CommandText = string.Format("select photo from tbl_image where id=1);
                //
                reader = dbCommand.ExecuteReader();
                //
                if (reader.Read())
                {
                    imageData = (byte[])reader["photo"];
                }
            }
            finally
            {
                if (reader != null)
                    reader.Close();
                //
                con.Close();
            }

2.Then you should use this data similar like you did:
C#
if(imageData != null)
{
  MemoryStream ms = new MemoryStream(imageData);
  pictureBox1.Image = System.Drawing.Image.FromStream(ms);
}
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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