Click here to Skip to main content
12,242,812 members (40,041 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET SQL-Server
Sir,

I am trying to upload image into database using store proc m having a fileupload control and upload button on upload button click image should be inserted into database but m getting error..pls help thanks in advance..
getting this error
"Procedure or function 'upload_image' expects parameter '@Property_Img', which was not supplied."

Database design and store proc:
create table tbl_property_img
(
   Image_Id int identity(1,1)primary key,
   Property_Img image null,
)
 

create procedure dbo.upload_image
   @Property_Img image
as
begin
   insert into tbl_property_img(Property_Img)values(@Property_Img)
end
Code Behind:
int filelength = FileUpload1.PostedFile.ContentLength;
        byte[] imagebytes = new byte[filelength];
        FileUpload1.PostedFile.InputStream.Read(imagebytes,0,filelength);
        SqlConnection cn = new SqlConnection(str);
        SqlCommand cmdinsimg = new SqlCommand("dbo.upload_image", cn);
        cmdinsimg.CommandType = CommandType.StoredProcedure;
        //cmdinsimg.Parameters.AddWithValue("@Property_Img", FileUpload1.);
        SqlParameter[] prms = new SqlParameter[1];
        prms[0] = new SqlParameter("@Property_Img",SqlDbType.Image);
        prms[0].Value = imagebytes;
        cn.Open();
        cmdinsimg.ExecuteNonQuery();
        cn.Close();
Posted 28-Sep-12 10:39am
Edited 28-Sep-12 17:50pm
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Try by changing your code as below.
int filelength = FileUpload1.PostedFile.ContentLength;
            byte[] imagebytes = new byte[filelength];
            FileUpload1.PostedFile.InputStream.Read(imagebytes,0,filelength);
            SqlConnection cn = new SqlConnection(str);
            SqlCommand cmdinsimg = new SqlCommand("dbo.upload_image", cn);
            cmdinsimg.CommandType = CommandType.StoredProcedure;
            //cmdinsimg.Parameters.AddWithValue("@Property_Img", FileUpload1.);
            SqlParameter[] prms = new SqlParameter[1];
            prms[0] = new SqlParameter("@Property_Img",SqlDbType.Image);
            prms[0].Value = imagebytes;
            cmdinsimg.Parameters.Add(prms);
            cn.Open();
            cmdinsimg.ExecuteNonQuery();
            cn.Close();
  Permalink  
Comments
Marcus Kramer 28-Sep-12 22:51pm
   
+5. Certainly the issue.
RaisKazi 30-Sep-12 10:30am
   
Thank you Marcus - Rais
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Though you have declared the parameter, you made a mistake in adding parameter to sqlcommand object.

cmdinsimg.Parameters.Add("@Property_Img",SqlDbType.Image);
cmdinsimg.Parameters["@Property_Img"].Value = imagebytes;

Hope this helps.
cheers
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Hi Raj,

You are just missing to add the Parameter to the Command.

Any of the solutions solves your problem!

You can also use the AddWithValue method in the Command.Parameters collection.

Regards,
  Permalink  

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.160426.1 | Last Updated 28 Sep 2012
Copyright © CodeProject, 1999-2016
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