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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
ds psPosted Nov 29, 2009, 11:51 PM
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();
}
shyamalaPosted Nov 27, 2009, 5:03 AM
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>