Click here to Skip to main content
Sign Up to vote bad
good
See more: C#4.0
Error on this code Parameter is not valid help me.
 
SqlCommand cmd = new SqlCommand("ReadImage", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@imgId", SqlDbType.Int).Value = Convert.ToInt32(cmbImageID.SelectedValue.ToString());
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
 
try
{
    if (con.State == ConnectionState.Closed)
        con.Open();
    adp.Fill(dt);
    if (dt.Rows.Count > 0)
    {
        MemoryStream ms = new MemoryStream((byte[])dt.Rows[0]["ImageData"]);
        picImage.Image = Image.FromStream(ms);
 
        picImage.SizeMode = PictureBoxSizeMode.StretchImage;
        picImage.Refresh();
    }
}
Table name = ImageData Column name= ImageData
Store procedure ReadImage
CREATE PROCEDURE dbo.ReadImage 
	(@imgId int)
AS
	SELECT ImageData FROM ImageData
	WHERE ImageID=@imgId
	RETURN
Posted 22 Sep '12 - 0:13
Edited 22 Sep '12 - 0:38


1 solution

Normally, this error occurs because you what you have saved in the database is not a valid image - so when you retrieve the information the Image.FromStream throws an exception because it cannot recognise it.
 
Check your loading code: the normal mistakes are not to convert the Image into a bytes array before saving it or building an INSERT statement by concatenating strings, which tends to insert the string "System.Drawing.Image" in the database instead of the image data.
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Rohan Leuva 230
1 OriginalGriff 228
2 Mahesh Bailwal 190
3 Ron Beyer 185
4 Abhinav S 168
0 Sergey Alexandrovich Kryukov 8,548
1 OriginalGriff 6,819
2 CPallini 3,648
3 Rohan Leuva 2,933
4 Maciej Los 2,288


Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 23 Sep 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid