Click here to Skip to main content
13,048,035 members (75,268 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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();
    if (con.State == ConnectionState.Closed)
    if (dt.Rows.Count > 0)
        MemoryStream ms = new MemoryStream((byte[])dt.Rows[0]["ImageData"]);
        picImage.Image = Image.FromStream(ms);
        picImage.SizeMode = PictureBoxSizeMode.StretchImage;
Table name = ImageData Column name= ImageData

Store procedure ReadImage
	(@imgId int)
	SELECT ImageData FROM ImageData
	WHERE ImageID=@imgId
Posted 22-Sep-12 0:13am
Updated 22-Sep-12 0:38am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

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

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 23 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100