Mark Allison
Mark Allison

Reputation: 7228

How to display an image based on SelectedValue in a GridView?

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?

Upvotes: 0

Views: 2624

Answers (2)

cevelry
cevelry

Reputation: 49

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);
    }

Upvotes: 1

Artemiy
Artemiy

Reputation: 1979

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";
}

Upvotes: 1

Related Questions