PDA

View Full Version : [Help - C#] Get images from SQL



IFS
03-09-2010, 01:45 AM
Does anyone here know how I can retrieve the images from my database into a datalist or a datagrid?

here is my code to read it into the database:



if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
{
byte[] imageSize = newbyte[FileUpload1.PostedFile.ContentLength];
HttpPostedFile uploadedImage = FileUpload1.PostedFile;
uploadedImage.InputStream.Read(imageSize, 0, (int)FileUpload1.PostedFile.ContentLength);

// Create SQL Connection
SqlConnection conn = newSqlConnection(@"User ID=sa;
Password=evolution;
Initial Catalog=PE_Reunion;
Data Source=(local)");

// Create SQL Command
SqlCommand cmd = newSqlCommand();
cmd.CommandText = "INSERT INTO Demo(DemoImageName,DemoImage)" + "
VALUES ('" + TextBox1.Text + "', @Image)";
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
SqlParameter ImageName = newSqlParameter("@ImageName", SqlDbType.VarChar, 50);
ImageName.Value = FileUpload1.FileName;
cmd.Parameters.Add(ImageName);
SqlParameter UploadedImage = newSqlParameter("@Image", SqlDbType.Image, imageSize.Length);
UploadedImage.Value = imageSize;
cmd.Parameters.Add(UploadedImage);
conn.Open();
int result = cmd.ExecuteNonQuery();
conn.Close();
if (result > 0) Label3.Text = "Demo File Uploaded and Saved";
Response.Redirect("photos.aspx");
}

Reke
03-09-2010, 04:10 PM
Try to add DataSource component on your form and connect your image with it.