hi when i use fileupload control ,
i need to store the image serverside,
which is correct method store image to folder or database?
how to get the stored image in database to web page?
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Marimuthu ArigovindasamyRank #5,893 · 283 reputationPosted Jun 30, 2010, 5:46 PM EDT
protected void btnUpload_Click(object sender, EventArgs e)
{
byte[] imageData = null;
string fileName = string.Empty;
if (!string.IsNullOrEmpty(FileUpload1.FileName))
{
if (FileUpload1.HasFile)
{
imageData = FileUpload1.FileBytes;
fileName = FileUpload1.FileName;
}
}
string conString = @"Data Source=WVDI1IHCL32\WVDI1IHCL32;Initial Catalog=Test;Integrated Security=True;";
SqlConnection con = new SqlConnection(conString);
con.Open();
SqlCommand cmd;
string filename = FileUpload1.FileName;
cmd = new SqlCommand("insertingImage", con);
cmd.CommandText = "INSERT INTO ProductImage(ProductImage, Description) VALUES(@ProductImage ,@Description)";
cmd.Parameters.AddWithValue("@ProductImage", imageData);
cmd.Parameters.AddWithValue("@Description", "Sample image");
cmd.ExecuteNonQuery();
con.Close();
}
protected void btnRetrieve_Click(object sender, EventArgs e)
{
string conString = @"Data Source=WVDI1IHCL32\WVDI1IHCL32;Initial Catalog=Test;Integrated Security=True;";
SqlConnection con = new SqlConnection(conString);
con.Open();
SqlCommand cmd = null;
cmd = new SqlCommand("SelectImage", con);
cmd.CommandText = "Select * from ProductImage where ID = 1";
SqlDataReader sqlReader = cmd.ExecuteReader();
sqlReader.Read();
Byte[] imageData = (byte[])sqlReader["ProductImage"];
Response.BinaryWrite(imageData);
}
Database:
create table ProductImage
(
ID int not null identity constraint PK_Image primary key,
ProductImage image,
Description varchar(50)
)
Dorababu MekaRank #457 · 5.5K reputationPosted Feb 28, 2011, 12:27 AM EST
http://www.aspsnippets.com/Articles/Display-Images-in-GridView-Control-using-the-path-stored-in-SQL-Server-database.aspx
Suthish NairRank #92 · 22.8K reputationPosted Feb 28, 2011, 8:07 AM EST