Click here to Skip to main content
15,886,798 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am using this code for image upload but image is not storing in database and also not showing the picture but when i press the button its shows file uploaded...
please help me...

C#
   string strImageName = TextBox3.Text.ToString(); 
            
 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);
 

 // Create SQL Connection 

  con = new SqlConnection(connstring);

 

 

 // Create SQL Command 

 

 SqlCommand cmd = new SqlCommand();

 cmd.CommandText = "INSERT INTO Images(ImageName,Image)" +

                   " VALUES (@ImageName,@Image)";

 cmd.CommandType = CommandType.Text;

 cmd.Connection = con;



 SqlParameter ImageName = new SqlParameter

                     ("@ImageName", SqlDbType.VarChar, 50);

 ImageName.Value = strImageName.ToString();

 cmd.Parameters.Add(ImageName);

 

 SqlParameter UploadedImage = new SqlParameter

               ("@Image", SqlDbType.Image, imageSize.Length);

 UploadedImage.Value = imageSize;

 cmd.Parameters.Add(UploadedImage);

 con.Open();

 int result = cmd.ExecuteNonQuery();

 con.Close();

 if (result > 0)

 Label1.Text = "File Uploaded";

 Image1.DataBind();

 }
}
Posted
Updated 23-May-13 0:02am
v2
Comments
Debug line by line ans see if you are getting all values correctly.

after browsing the file from file upload

if you want to take the file in to byte.
just write

byte[] fileBytes=FileUpload1.filebytes;

then pass the fileBytes variable to your cmd parameter which is of byte TYPE.
Hope this helps..!
 
Share this answer
 
Hi...
its may help ful to u,see this link.

How to store & retrieve images in mysql database using asp.net c#[^]

thank u.
 
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