retreiving images from database and display as thumbnail images
-
hi, iam using asp.net with c# i have table images shop nvarchar(50), imgc image imgo image iam saving images in a database but while retreving it is showing blank images. i want to display as thumb nail images , can you correct my code which helps me 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(); } }
retreiving 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()) { Image1.ImageUrl = dr["imgc"].ToString(); Image2.ImageUrl = dr["imgo"].ToString(); } dr.Close(); } con.Close(); }
-
hi, iam using asp.net with c# i have table images shop nvarchar(50), imgc image imgo image iam saving images in a database but while retreving it is showing blank images. i want to display as thumb nail images , can you correct my code which helps me 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(); } }
retreiving 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()) { Image1.ImageUrl = dr["imgc"].ToString(); Image2.ImageUrl = dr["imgo"].ToString(); } dr.Close(); } con.Close(); }
Hi, Can you please mention what problem you have with your code. It would be good if you can say whether you get any error/exception or what is not happening (that was expected !!!). Please let us know which line you might be suspecting something problem!!! Unless you mention those ponts, it will not be possible for people to go through your code or compile your code and to see what is happening!!.
Thanks, Arindam D Tewary
-
hi, iam using asp.net with c# i have table images shop nvarchar(50), imgc image imgo image iam saving images in a database but while retreving it is showing blank images. i want to display as thumb nail images , can you correct my code which helps me 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(); } }
retreiving 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()) { Image1.ImageUrl = dr["imgc"].ToString(); Image2.ImageUrl = dr["imgo"].ToString(); } dr.Close(); } con.Close(); }
you are storing images in binary in sql server...n you r directly use tostring() function on it..!!!
bool flgImage = false; byte\[\] buffer = new byte\[0\]; buffer = (byte\[\])SQLHelper.ExecuteScalar("SELECT imgImage FROM dbo.MeasurementImages WHERE intImageID=id"); context.Response.ClearHeaders(); context.Response.Expires = 0; context.Response.Cache.SetNoServerCaching(); context.Response.Cache.SetMaxAge(TimeSpan.Zero); context.Response.ContentType = "image/gif"; context.Response.Cache.SetCacheability(HttpCacheability.Public); context.Response.BufferOutput = false; context.Response.OutputStream.Write(buffer, 0, buffer.Length); context.Response.Flush(); context.Response.End();
Make image handler file for retriving images from database. this is a better way.
!- F - R - I - E - N - D - S -!