Reputation: 469
Trying to display the latest image from the table, the code here working fine but the latest image can not be displayed because the passed image url id is determined as image4. I want the image id to be any id and display the latest image. How to do that?
HTML:
<asp:image ToolTip = "ASP Image Control" ID="Image" runat="server" ImageUrl ="Default.aspx?ImageID=4" Height="200px" Width="300px"></asp:image>
code behind
if (Request.QueryString["ImageID"] != null)
{
string strQuery = "select TOP 1 Name, ContentType, Data from stock where id=@id order by id DESC";
SqlCommand cmd = new SqlCommand(strQuery);
cmd.Parameters.Add("@id", SqlDbType.Int).Value = Convert.ToInt32 (Request.QueryString["ImageID"]);
DataTable dt = GetData(cmd);
if (dt != null)
{
Byte[] bytes = (Byte[])dt.Rows[0]["Data"];
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = dt.Rows[0]["ContentType"].ToString();
Response.AddHeader("content-disposition", "attachment;filename=" + dt.Rows[0]["Name"].ToString());
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
}
Upvotes: 0
Views: 178
Reputation: 326
Maybe you should pass empty parameter to Url if you want to get latest image but pass ID when you want to get specific image:
<asp:image ToolTip = "ASP Image Control" ID="Image" runat="server"
ImageUrl ="Default.aspx?ImageID=" Height="200px" Width="300px"></asp:image>
and make code depend on ImageID value:
var imageIdParam = Request.QueryString["ImageID"];
if (imageIdParam != null)
{
var imageId = imageIdParam == "" ? -1 : Convert.ToInt32 (imageIdParam);
var cmd = new SqlCommand();
var strQuery = "select TOP 1 Name, ContentType, Data from stock ";
if(imageId > 0) {
strQuery += "where id=@id ";
cmd.Parameters.Add("@id", SqlDbType.Int).Value = imageId;
}
strQuery += "order by id DESC";
cmd.CommandText = strQuery;
DataTable dt = GetData(cmd);
if (dt != null)
{
Byte[] bytes = (Byte[])dt.Rows[0]["Data"];
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = dt.Rows[0]["ContentType"].ToString();
Response.AddHeader("content-disposition", "attachment;filename=" + dt.Rows[0]["Name"].ToString());
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
}
Upvotes: 1
Reputation: 394
You can dynamically set the imageUrl in the code behind.
int id = ...;
Image.ImageUrl = "Default.aspx?ImageID=+id;
Upvotes: 0