Hi
C#
protected void lnkAdd_Click(object sender, EventArgs e)
{
try
{
//string sessionDate = "";
Boolean success = false;
BALGroup objGroup = new BALGroup();
Group Result = objGroup.GetGroupDetail(Convert.ToInt32(hdfGroupId.Value));
foreach (GridViewRow gr in grdPlanning.Rows)
{
if (gr.RowType == DataControlRowType.DataRow)
{
string sessionDate = gr.DataItem.ToString();
if (sessionDate != null)
{}
}
}
}
}
--------------
protected void gr_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType != DataControlRowType.DataRow)
return;
e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Center;
TextBox txtSessionDate = new TextBox();
txtSessionDate.Text = "";
txtSessionDate.CssClass = "form-control daterange-single";
e.Row.Cells[10].Controls.Add(txtSessionDate);
e.Row.Cells[10].HorizontalAlign = HorizontalAlign.Center;
string Remark = DataBinder.Eval(e.Row.DataItem, "Remarks").ToString();
if (Remark == "Done" || Remark == "Scheduled")
{
if (txtSessionDate != null)
{
txtSessionDate.Enabled = false;
}
}
e.Row.DataItem = txtSessionDate;
}
}Thanks

Ramco RamcoRank #462 · 5.4K reputationPosted May 8, 2023, 4:09 AM EDT
Hi Naimish
What does this line does
e.Row.DataItem = txtSessionDate;
Thanks
Naimish MakwanaRank #324 · 7.7K reputationPosted May 8, 2023, 3:07 AM EDT
The error "Object reference not set to an instance of the object" typically occurs when you try to use an object that has not been instantiated. In the code you provided, the error could be caused by several factors, including:
The variable "gr.DataItem" in the lnkAdd_Click method may be null, causing an exception when ToString() method is called.
The txtSessionDate control may not have been initialized in the gr_RowDataBound method.
The DataItem property of the GridViewRowEventArgs parameter in the gr_RowDataBound method may be null, causing an exception when it is accessed.
To fix the issue, you could add some null checks before using the objects, like the following:
protected void lnkAdd_Click(object sender, EventArgs e) { try { //string sessionDate = ""; Boolean success = false; BALGroup objGroup = new BALGroup(); Group Result = objGroup.GetGroupDetail(Convert.ToInt32(hdfGroupId.Value)); foreach (GridViewRow gr in grdPlanning.Rows) { if (gr.RowType == DataControlRowType.DataRow) { if (gr.DataItem != null) { string sessionDate = gr.DataItem.ToString(); if (sessionDate != null) { //... } } } } } catch (Exception ex) { // Handle the exception } } protected void gr_RowDataBound(object sender, GridViewRowEventArgs e) { try { if (e.Row.RowType != DataControlRowType.DataRow) return; e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Center; TextBox txtSessionDate = new TextBox(); txtSessionDate.Text = ""; txtSessionDate.CssClass = "form-control daterange-single"; e.Row.Cells[10].Controls.Add(txtSessionDate); e.Row.Cells[10].HorizontalAlign = HorizontalAlign.Center; string Remark = DataBinder.Eval(e.Row.DataItem, "Remarks")?.ToString(); if (Remark == "Done" || Remark == "Scheduled") { if (txtSessionDate != null) { txtSessionDate.Enabled = false; } } if (txtSessionDate != null) { e.Row.DataItem = txtSessionDate; } } catch (Exception ex) { // Handle the exception } }Thanks
Mohamed Azarudeen ZRank #297 · 8.5K reputationPosted May 9, 2023, 1:45 PM EDT
Ramco if i may
The code line
e.Row.DataItem = txtSessionDate;is trying to assign a value to theDataItemproperty of aGridViewRowobject, which is represented by the variablee.Row.