I have built some back-end code to retrieve an image from a SQL Server database. I want to display this image on my web form based on the currenct selected value of a gridview control.
I have a page called GetImage.aspx which takes two querystring parameters, entityId and entityType. entityId is the GridView.SelectedValue and entityType is simply “T”. My code for GetImage.aspx looks like this:
protected void Page_Load (Object server, EventArgs e)
{
if ((Request.QueryString["entityId"] != null) && (Request.QueryString["entityType"] != null))
{
int entityId = int.Parse(Request.QueryString["entityId"]);
string entityType = Request.QueryString["entityType"];
DBUtil DB = new DBUtil();
DataTable dt = DB.GetScreenshot(entityId, entityType);
if (dt != null)
{
Byte[] bytes = (Byte[])dt.Rows[0]["screenshot"];
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]["fileName"].ToString());
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
}
}
What ASP.NET code do I need to display this on my calling form?
Thanks.
EDIT
I have added this code to my form (I’m not trying to display the image IN the GridView, just on my form under the GridView):
<img id="tradeScreenshot" runat="server" alt="screenshot" />
and added this code:
protected void grdTrades_SelectedIndexChanged(object sender, EventArgs e)
{
string tradeId = grdTrades.SelectedDataKey.Value.ToString();
tradeScreenshot.Src = "GetImage.aspx?entityId=" + tradeId + "&entityType=T";
}
The image is not displayed, however I can see the stored procedure being executed on the database server with the correct ID when I select an item in the GridView, so that part is working, but the display of the image isn’t. What am I missing?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Depends on what you show in gridview, and how rows are selected. Grid doesn’t have selectedvalue – it has selected item, which can have multiple data keys with it.
ASP.NET:
<img src="" runat="server" id="selectedimage" /> <asp:GridView OnSelectedIndexChanged="ChangeItem" DataKeyNames="ImageId" runat="server" id="MyGrid"></asp:GridView>
C#:
public void ChangeItem(object o,EventArgs e){
string ImageId = MyGrid.SelectedDataKey.Value.ToString();
selectedimage.Src = "GetImage.aspx?entityId="+ImageId+"&entityType=T";
}
Method 2
I have faced same problem with you i used Stored Procedure in my program
here is how i have called image to the DataGridView in the button click .
pbxImage.Image = GetPhoto((byte[])dgvUrun.CurrentRow.Cells[8].Value);
And here is the GetPhoto Method:
private Image GetPhoto(byte[] photo)
{
MemoryStream ms = new MemoryStream(photo);
return Image.FromStream(ms);
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0