Ramco Ramco

Ramco Ramco

  • 416
  • 3.3k
  • 477.2k

Object reference not set to an instance of the Object

May 5 2023 9:12 AM

Hi

  In below code it gives error - TextBox txt = (TextBox)gr.Cells[gr.Cells.Count - 1].FindControl("txtDate");

In View PageSource it is showing something like this - <input name="grdPlanning$ctl108$ctl00" type="text" class="form-control" />

protected void lnkAdd_Click(object sender, EventArgs e)
{
    try
    {
        foreach (GridViewRow gr in grdPlanning.Rows)
        {
            if (gr.RowType == DataControlRowType.DataRow)
            {
                if (gr.Cells[0].Text == "146")
                {

                    TextBox txt = (TextBox)gr.Cells[gr.Cells.Count - 1].FindControl("txtDate");
                    string Ques = txt.Text;
                }
            }
        }
    }
}
protected void gr_RowDataBound(object sender, GridViewRowEventArgs e)
{
    try
    {
        if (e.Row.RowType != DataControlRowType.DataRow)
            return;
        e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Center;

        TextBox txtDate = new TextBox();
        txtDate.CssClass = "form-control";
        txtDate.DataBind();
        e.Row.Cells[e.Row.Cells.Count - 1].Controls.Add(txtDate);
    }
}

Thanks


Answers (4)