I am retrieving the image from a SQL Server database into a PictureBox in a C# Windows application. The image is a form which I don't know.
I tried this code but it throws a "parameter invalid" exception, and I also go through many answer related to this but didn't work any.
My code:
SqlConnection sql_con = new SqlConnection();
sql_con.ConnectionString = @"Server=abc-14;Database=Abcstudent;User Id=sa; Password=abc;";
if (sql_con.State == ConnectionState.Closed)
sql_con.Open();
try
{
string query = "Select grno, PICStudent from GENREG where grno = 1";
SqlCommand sql_cmd = new SqlCommand(query, sql_con);
sql_cmd.ExecuteNonQuery();
SqlDataAdapter sql_adp = new SqlDataAdapter(sql_cmd);
sql_adp.Fill(sql_ds);
}
catch(Exception ex)
{
}
// code for retrieving image to picture box
try
{
DataRow myRow;
byte[] MyData = new byte[0];
myRow = sql_ds.Tables[0].Rows[0];
MyData = (byte[])myRow["PICStudent"];
MemoryStream stream = new MemoryStream(MyData);
pictureBox1.Image = Image.FromStream(stream); // getting error here
}
catch()
{
}
