-3

I want to display values from db to grid view with image file. While using given below code it shows an error. Help me to find a proper solution.

Code:

 protected void Button1_Click(object sender, GridViewRowEventArgs e1)
    {
        ShadingAnalysisDataSetTableAdapters.tbl_SiteLayOutUploadTableAdapter sl;
        sl = new ShadingAnalysisDataSetTableAdapters.tbl_SiteLayOutUploadTableAdapter();
        DataTable dt = new DataTable();
        dt = sl.GetGridData(ddlSit.SelectedValue, int.Parse(ddlVersion.SelectedValue));
        try
        {
          if (e1.Row.RowType == DataControlRowType.DataRow && gvEdit.EditIndex == e1.Row.RowIndex)
            {
              Image Image1 = (Image)e1.Row.FindControl("Image1");
              foreach (DataRow row in dt.Rows)
               {
                  byte[] img1 = (byte[])row["ImgData"];
                  string base1 = Convert.ToBase64String(img1);
                  Image1.ImageUrl = "data:image/jpg;base64," + base1;
               }
          }
            gvEdit.DataSource = dt;
            gvEdit.DataBind();
        }
        catch (NullReferenceException ex)
        {

        }
    }

ASPX:

 <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
Rahul Nikate
  • 6,192
  • 5
  • 42
  • 54
Vipin
  • 261
  • 6
  • 20
  • 44
  • 1
    Is the button inside the gridview? – ekad Dec 02 '14 at 04:43
  • `Button_Click(Object sender, EventArgs e)` - This is the default proto for click handler for a button (from MSDN). What you have given for a button click event handler is `GridViewRowEventArgs `. Should you just use `EventArgs`? – Karthik Nishanth Dec 02 '14 at 04:45
  • 1
    Don't ever ignore exceptions, except under very rare circumstances. And don't _ever_ ignore `NullReferenceException`. – John Saunders Dec 02 '14 at 04:57

2 Answers2

3

You need to change function prototype like below:

 protected void Button1_Click(object sender, EventArgs e1)
 {
 }
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
Rahul Nikate
  • 6,192
  • 5
  • 42
  • 54
1

Parameters of your Button1_Click is wrong. It should look like below.

protected void Button1_Click(object sender, EventArgs e)
{

}
Sam
  • 2,917
  • 1
  • 15
  • 28
  • 1
    This won't solve the problem, the code will break at `if (e1.Row.RowType == DataControlRowType.DataRow` condition because you also change `e1` to `e`. – ekad Dec 02 '14 at 04:56