Hello ladies and gentlemen of the forum. I bear grevious news. I am able to execute the following code which saves an image file to my database without error. I am confident that the image is saved as expected. Yet to my horror I find then when I try to display my image in a gridview it does not appear in the same way Santa Clause does. I am all a tither! Alas and alack! Oh, who will save me?
httphandler (generic handler)>>
i have an uploader control , the image is inserted into the db successfully , but cant retrieve it>>
httphandler (generic handler)>>
Code:
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=HOME-PC\\SQLEXPRESS;Initial Catalog=imagek;Integrated Security=True";
con.Open();
SqlCommand command = new SqlCommand("SELECT * from image ", con);
SqlDataReader dr = command.ExecuteReader();
dr.Read();
context.Response.BinaryWrite((Byte[])dr[0]);
//context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
con.Close();
context.Response.End();
}
public bool IsReusable {
get {
return false;
}
}
}
default page>>
Code:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Windows.Forms;
//using System.Web.
public partial class _Default : System.Web.UI.Page
{
//con.ConnectionString =SqlConnection con = new SqlConnection();
public SqlConnection con = new SqlConnection();
protected void Page_Load(object sender, EventArgs e)
{
con.ConnectionString = "Data Source=HOME-PC\\SQLEXPRESS;Initial Catalog=imagek;Integrated Security=True";
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
con.Open();
byte[] img = new byte[FileUpload1.PostedFile.ContentLength];
HttpPostedFile myimg = FileUpload1.PostedFile;
myimg.InputStream.Read(img, 0, FileUpload1.PostedFile.ContentLength);
SqlCommand cmd = new SqlCommand("insert into image values(22,'"+ img +"')",con);
cmd.ExecuteNonQuery();
con.Close();
}
}
protected void Button2_Click(object sender, EventArgs e)
{ DataSet ds = new DataSet();
con.Open();
SqlCommand command = new SqlCommand("SELECT id from image ", con);
// SqlCommand command = new SqlCommand("SELECT imagename,ImageID from [Image]", connection);
SqlDataAdapter daimages = new SqlDataAdapter(command);
DataTable dt = new DataTable();
daimages.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
GridView1.Attributes.Add("bordercolor", "black");
}
}
Comment