AspdotnetCodes.com
Search
Articles
Books
Resources
Asp.Net News
Downloads
Free Tech Magazines
Asp.Net Web Hosting
Archives
Softwares
Newsletter
Suggest Us
Link to Us
Feeds Subscription
Articles
Questions & Answers
Tips & Tricks
 retreving images from database
Posted by charan on  5/1/2010 4:43:02 AM Category: Asp.Net 2.0
Add to my favorites
Email to friend
hi iam using asp.net with c#

iam saving two images in database but while retreving only one image is displaying, can you correct my code

saving code
=========

protected void Button1_Click(object sender, EventArgs e)
{


if (FileUpload1.HasFile && FileUpload2.HasFile)
{



img1.ImageUrl = imgc.ImageUrl;
img2.ImageUrl = imgo.ImageUrl;
FileUpload1.SaveAs(MapPath("~/images/" + FileUpload1.FileName));
imgc.ImageUrl = "~/images/" + FileUpload1.FileName;
FileUpload2.SaveAs(MapPath("~/images/" + FileUpload2.FileName));
imgo.ImageUrl = "~/images/" + FileUpload2.FileName;

con.Open();

SqlCommand cmd = new SqlCommand("Addimgproc", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Shop", "Alsafa");
FileInfo imageinfo = new FileInfo(FileUpload1.PostedFile.FileName.Trim());
byte[] content = new byte[imageinfo.Length];
FileStream imagestream = imageinfo.OpenRead();
imagestream.Read(content, 0, content.Length);
imagestream.Close();
//2nd upload
FileInfo imageinfo1 = new FileInfo(FileUpload2.PostedFile.FileName.Trim());
byte[] content1 = new byte[imageinfo1.Length];
FileStream imagestream1 = imageinfo1.OpenRead();
imagestream1.Read(content1, 0, content1.Length);
imagestream1.Close();

cmd.Parameters.AddWithValue("@imgc", content);
cmd.Parameters.AddWithValue("@imgo", content1);
cmd.ExecuteNonQuery();
con.Close();

}

}

retreving code
===========

protected void Page_Load(object sender, EventArgs e)
{


SqlCommand cmd = new SqlCommand("select imgc,imgo from images where Shop='Alsafa'", con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{

//"image/gif";

Response.ContentType = "text/html";
Response.BinaryWrite((byte[])dr["imgc"]);

Response.BinaryWrite((byte[])dr["imgo"]);




}
}
dr.Close();
con.Close();


}


i want to display both the images side by side but it is displaying only one image ,

can you correct my code

Post Your Reply Post New Question

 Viewer's Reply
Display Image From Database
Answered By Moderator1 on 5/4/2010 8:45:24 AM
Hi,

You have not assigned the data retrieved from the database to the image control. To show image that is saved in the database, please refer to our article below.

http://www.aspdotnetcodes.com/Insert_Images_Database.aspx
 
Post Your Reply If you still have problem, post it again under this reply for better response.

 
Sponsored by
Introduction to Software Development
Introduction to Web Applications Development
The Art & Science of JavaScript - Free 100 Page Preview!