R
RobertH
Hello all. I have been hacking away trying to get a SQL image (jpeg) to
render in a control or table row Without using the Response.BinaryWrite....
I think i might be on the verge but need a little help.. below are 2
functions that i wrote to try and accomplish this.
the first one is what i would LIKE to do
the 2nd one works but it's not what i want to do..
here's the code :
private void renderPic(object sender, System.EventArgs e)
{
SqlConnection oConnect = new SqlConnection("server=192.x.x.x; uid=UID;
pwd=PWD; Database=DB");
//SqlCommand query = new SqlCommand("Select * from Pictures", oConnect);
string sql = "server=192.x.x.x; uid=uid; pwd=pwd; Database=database";
DataSet ds = new DataSet();
SqlConnection connection = new SqlConnection(sql);
SqlDataAdapter query = new SqlDataAdapter("Select * from Pictures",
oConnect);
connection.Open();
query.Fill(ds, "Pictures");
connection.Close();
if(ds.Tables["Pictures"].Rows.Count > 0)
{
for(int x = 0; x < ds.Tables["Pictures"].Rows.Count ; x++)
{
//Response.Clear();
byte[] y = (byte[])ds.Tables["Pictures"].Rows[x]["FileData"];
//y = (byte[])(ds.Tables["Pictures"].Rows[x]["FileData"]);
//Response.Write(y.ToString());
MemoryStream ms = new
MemoryStream((byte[])ds.Tables["Pictures"].Rows[x]["FileData"]);
//Response.AddHeader("Content-Type",
ds.Tables["Pictures"].Rows[x]["FileType"].ToString());
//Response.BinaryWrite(y);
//<img src="mydynamicimage.aspx">
TableRow tr1 = new TableRow();
TableCell tc1 = new TableCell();
Bitmap bmp = new Bitmap(ms);
//System.Drawing.Image tt = new System.Drawing.Image();
System.Web.UI.WebControls.Image fv = new
System.Web.UI.WebControls.Image();
fv.Attributes.Add("ContentType","image/jpeg");
fv.Equals(bmp);
//tc1.Text = @"<img src=""test.aspx"">";
tc1.Controls.Add(fv);
tr1.Cells.Add(tc1);
//placeholder "plIMG"
plIMG.Controls.Add(tr1);
}
}
}
private void testing(int id)
{
string connstr = "server=192.x.x.x; uid=uid; pwd=pwd; Database=database";
SqlConnection cnn = new SqlConnection(connstr);
SqlCommand cmd = new SqlCommand("select * from pictures where id=" + id,
cnn);
cnn.Open();
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
byte[] bindata = (byte[])dr.GetValue(3);
Response.AddHeader("Content-Type", "image/pjpeg");
Response.BinaryWrite(bindata);
cnn.Close();
}
render in a control or table row Without using the Response.BinaryWrite....
I think i might be on the verge but need a little help.. below are 2
functions that i wrote to try and accomplish this.
the first one is what i would LIKE to do
the 2nd one works but it's not what i want to do..
here's the code :
private void renderPic(object sender, System.EventArgs e)
{
SqlConnection oConnect = new SqlConnection("server=192.x.x.x; uid=UID;
pwd=PWD; Database=DB");
//SqlCommand query = new SqlCommand("Select * from Pictures", oConnect);
string sql = "server=192.x.x.x; uid=uid; pwd=pwd; Database=database";
DataSet ds = new DataSet();
SqlConnection connection = new SqlConnection(sql);
SqlDataAdapter query = new SqlDataAdapter("Select * from Pictures",
oConnect);
connection.Open();
query.Fill(ds, "Pictures");
connection.Close();
if(ds.Tables["Pictures"].Rows.Count > 0)
{
for(int x = 0; x < ds.Tables["Pictures"].Rows.Count ; x++)
{
//Response.Clear();
byte[] y = (byte[])ds.Tables["Pictures"].Rows[x]["FileData"];
//y = (byte[])(ds.Tables["Pictures"].Rows[x]["FileData"]);
//Response.Write(y.ToString());
MemoryStream ms = new
MemoryStream((byte[])ds.Tables["Pictures"].Rows[x]["FileData"]);
//Response.AddHeader("Content-Type",
ds.Tables["Pictures"].Rows[x]["FileType"].ToString());
//Response.BinaryWrite(y);
//<img src="mydynamicimage.aspx">
TableRow tr1 = new TableRow();
TableCell tc1 = new TableCell();
Bitmap bmp = new Bitmap(ms);
//System.Drawing.Image tt = new System.Drawing.Image();
System.Web.UI.WebControls.Image fv = new
System.Web.UI.WebControls.Image();
fv.Attributes.Add("ContentType","image/jpeg");
fv.Equals(bmp);
//tc1.Text = @"<img src=""test.aspx"">";
tc1.Controls.Add(fv);
tr1.Cells.Add(tc1);
//placeholder "plIMG"
plIMG.Controls.Add(tr1);
}
}
}
private void testing(int id)
{
string connstr = "server=192.x.x.x; uid=uid; pwd=pwd; Database=database";
SqlConnection cnn = new SqlConnection(connstr);
SqlCommand cmd = new SqlCommand("select * from pictures where id=" + id,
cnn);
cnn.Open();
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
byte[] bindata = (byte[])dr.GetValue(3);
Response.AddHeader("Content-Type", "image/pjpeg");
Response.BinaryWrite(bindata);
cnn.Close();
}