Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I Have a gridview, calender and image button in gridview. While clicking on image its giving error. The code is as below.
C#
protected void imgEndDate_Click1(object sender, ImageClickEventArgs e)
        {
            
            Calendar calendDate =
                             (Calendar)grvResourceDetail.SelectedRow.Cells[1].FindControl("calEndDate");
            calendDate.Visible = true;
        }


Its giving the error
Object reference not set to an instance of an object.

Both calender control and image button are in itemtemplate field of gridview. Unable to find null object.
Posted
Updated 1-May-17 20:30pm
v2
Comments
DamithSL 12-Jan-16 3:18am    
debug and check which object is null
Telstra 12-Jan-16 3:24am    
Both calender control and image button are in itemtemplate field of gridview. Unable to find null object.
DamithSL 12-Jan-16 3:36am    
what is the full error with stack trace? we can't debug your code, you have to do that.
Richard Deeming 12-Jan-16 9:10am    
Either <codde>grvResourceDetail is null;
-or- grvResourceDetail does not have a selected row;
-or- the second cell of the selected row does not contain a control with the ID calEndDate.

You need to debug your code to find out which of these is the case, and then work out why.

Exanple: try this

ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand">
            <Columns>
                <asp:TemplateField HeaderText="Cal">
                    
                    <ItemTemplate>
                        <asp:Calendar runat="server" Visible="false"></asp:Calendar>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="img">
                    <ItemTemplate>
                        <asp:ImageButton ID="ImageButton1" CommandName="img" runat="server" text="show"/>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>

        </asp:GridView>
    </div>
    </form>
</body>
</html>






C#
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName=="img")
            {
                int index = Convert.ToInt32(e.CommandArgument.ToString());
                Calendar calendDate =
                             (Calendar)GridView1.Rows[index].FindControl("calEndDate");
            calendDate.Visible = true;
            }
           
        }
    }
}
 
Share this answer
 
Instead of SelectedRow.Cells[1]
you can try this one
Calendar calendDate =
(Calendar)grvResourceDetail.Rows[0].FindControl("calEndDate");
 
Share this answer
 
v2
Comments
CHill60 2-May-17 6:36am    
Incorrect. That code will only ever look at the first row in the GridView.
And the question was answered (correctly) over a year ago.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900