0

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.

Antonio Mailtraq
  • 1,397
  • 5
  • 34
  • 82
  • Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Rahul Singh Jan 08 '16 at 10:54
  • exactly in which line of code you are getting this exception? – Bharadwaj Jan 08 '16 at 11:15
  • @Bharadwaj: thank you for reply. In this line `ddlCities.Items.FindByValue(hdnval.Value).Selected = true; ` – Antonio Mailtraq Jan 08 '16 at 11:23
  • There might be two reasons, put the break point and check if `hdnval` is null or not. – Bharadwaj Jan 08 '16 at 11:27
  • Sorry, perhaps I am missing something. Are you running exactly the same code for both drop downs? This can never work, since second DDL has values "I1", "I9" etc, so they never satisfy `Left(area,1) = 'M'`. Therefore `FindByValue(hdnval.Value)` never finds anything and returns null – Andrei Jan 08 '16 at 11:28
  • If not, then, the value which you are finding might not be exists. – Bharadwaj Jan 08 '16 at 11:28
  • @Andrei: thank you, you have right this is my problem. – Antonio Mailtraq Jan 08 '16 at 11:32

0 Answers0