Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
SQL
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:
C#
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
Updated 28-Sep-12 16:50pm
v3

Try by changing your code as below.
C#
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();
 
Share this answer
 
Comments
fjdiewornncalwe 28-Sep-12 22:51pm    
+5. Certainly the issue.
RaisKazi 30-Sep-12 10:30am    
Thank you Marcus - Rais
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
 
Share this answer
 
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,
 
Share this answer
 

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



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