The question is different because the error is present only where particular condition and I explain the reason. Please read with attention the question.
This is the output HTML
of DropDownList
on GridView
in EditItemTemplate
mode row :
<input type="hidden" name="gvProducts$ctl09$hdnArea" id="gvProducts_hdnArea_7" value="M4" />
<select name="gvProducts$ctl09$Area" id="gvProducts_Area_7" class="ddl_Class_new" style="background-color:Orange;">
<option value="M1">M1</option>
<option value="M2">M2</option>
<option value="M3">M3</option>
<option value="M4">M4</option>
<option value="M5">M5</option>
<option value="M6">M6</option>
<option value="M7">M7</option>
<option selected="selected" value="M4">M4</option>
</select>
The list is generated and populate DropDowmList
in RowDataBound
event:
if (e.Row.RowType == DataControlRowType.DataRow && gvProducts.EditIndex == e.Row.RowIndex)
{
DropDownList ddlCities = (DropDownList)e.Row.FindControl("Area");
HiddenField hdnval = (HiddenField)e.Row.FindControl("hdnArea");
string query = " Select distinct Area ";
query += " from tbl_area WHERE Left(area,1) = 'M'; ";
OdbcCommand cmd = new OdbcCommand(query);
ddlCities.DataSource = GetData(cmd);
ddlCities.DataTextField = "Area";
ddlCities.DataValueField = "Area";
ddlCities.DataBind();
ddlCities.Items.FindByValue(hdnval.Value).Selected = true;
}
In this case I don't have problem and edit with success the interested row.
But I can have a second situation in other row of GridView
:
<input type="hidden" name="gvProducts$ctl09$hdnArea" id="gvProducts_hdnArea_6" value="I9" />
<select name="gvProducts$ctl09$Area" id="gvProducts_Area_6" class="ddl_Class_new" style="background-color:Orange;">
<option value="I1">I1</option>
<option value="I9">I9</option>
<option selected="selected" value="I9">I9</option>
</select>
And if tried to edit this row I have the error
Object reference not set to an instance of an object.
Because in the query of RowDataBound
event I search only where :
Left(area,1) = 'M'
How to resolve this?
Please help me, thank you in advance.