how can we display image from sql database to new web page using sqldatareader & binarywrite?? its urgent ,plz help
thanks
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
shyamala37 reputationPosted Nov 27, 2009, 12:03 AM EST
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["SqlConnectionString"]);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from tablename";
SqlDataReader dr = cmd.ExecuteReader();
DataList1.DataSource = dt;
DataList1.DataBind();
dr.Close();
conn.Close();
Asp.net
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<img src="<%# DataBinder.Eval(Container.DataItem, "image") %>" style="height:75px; width:75px;" alt="" />
ItemTemplate>
asp:DataList>
ds ps62 reputationPosted Nov 29, 2009, 6:51 PM EST
my code is as below, i could not trace the error.. i just want to display the selected image in new web page.
protected void Page_Load(object sender, EventArgs e)
{
int PictureID = 1;
//Connect to the database and bring back the image contents & MIME type for the specified picture
string ConStr = System.Configuration.ConfigurationSettings.AppSettings.Get("SQLCON").ToString();
SqlConnection cn = new SqlConnection(ConStr);
string SQLQRY = "SELECT [MIMEType], [ImageData] FROM ContractImages WHERE [ImageId] = @PictureID";
SqlCommand cmd = new SqlCommand(SQLQRY, cn);
cmd.Parameters.AddWithValue("@PictureID", PictureID); //PictureTitle.Text.Trim());
cn.Open();
SqlDataReader imagereader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (imagereader.Read())
{
Response.ContentType = imagereader["MIMEType"].ToString ();
Response.BinaryWrite(imagereader["ImageData"]);
}
imagereader.Close();
cn.Close();
}