Click here to Skip to main content
15,919,132 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai

I want to update a database image from my website. in my web page i have file upload, dropdownlist,one button field. this is my code..
C#
protected void Button1_Click(object sender, EventArgs e)
   {
       if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
       {
           byte[] imagesize = new byte[FileUpload1.PostedFile.ContentLength];
           HttpPostedFile uploadedImage = FileUpload1.PostedFile;
           uploadedImage.InputStream.Read(imagesize, 0, (int)FileUpload1.PostedFile.ContentLength);
           SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["wavoo"].ConnectionString);
           con.Open();
           int i =Convert.ToInt32(DropDownList1.SelectedItem.Text);
           SqlCommand cmd = new SqlCommand("update picture set image=@image  where ID=" + i, con);
           SqlParameter param = new SqlParameter("@Image", SqlDbType.Image, imagesize.Length);
           cmd.Parameters.Add(uploadedImage);
           cmd.ExecuteNonQuery();
           MessageBox.Show("sdfdf");

       }
   }


actually i don't know about parameters... and my error is

The SqlParameterCollection only accepts non-null SqlParameter type objects, not HttpPostedFile objects.


Please help me
Posted
Updated 14-Aug-12 21:24pm
v2

You can't send HttpPostadFile as a parameter value to your database. You will have to convert it to a bytearray first.

This article explains everything you'll need to know:
C# Save and Load Image from Database[^]
 
Share this answer
 
Comments
Member 9327701 15-Aug-12 3:41am    
Thank You so much i got the answer..

Another one doubt..

my grid view doesn't display the data.. why...
StianSandberg 15-Aug-12 3:44am    
I have no idea.. Could you post your code?
Your passing parameter is incorrect. You created param parameter but adding uploadedImage to the command cmd.

Instead of this.

C#
SqlParameter param = new SqlParameter("@Image", SqlDbType.Image, imagesize.Length);
           cmd.Parameters.Add(uploadedImage);


try,

C#
SqlParameter param = new SqlParameter("@Image", SqlDbType.Image, imagesize);
           cmd.Parameters.Add(param );
 
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