Click here to Skip to main content
15,115,119 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
C#
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
  {
      GridView1.EditIndex = e.NewEditIndex;
      DropDownList d = (DropDownList)(GridView1.Rows[GridView1.EditIndex].FindControl("DropDownList1"));

      d.Items.Clear();
      d.DataSource = databindobj.viewdata("select * from tbl_category");
      d.DataValueField = "_id";
      d.DataTextField = "_category";
      d.DataBind();
      d.Items.Insert(0, "No Answer");
  }



this above give me the error "Object reference not set to an instance of an object."

please help.
Posted
Updated 5-Apr-13 2:46am
v2
Comments
Marco Bertschi 5-Apr-13 5:54am
   
On which line does it give the error?
[no name] 5-Apr-13 6:20am
   
d.Items.Clear();

on this line
Nicholas Marty 5-Apr-13 5:56am
   
Which line exactly raises the error?

I could spot 2 possible sources:
- The FindControl Method does not return a Control
- "databindobj" is null

Try checking if any of those objects might be null while debugging
[no name] 5-Apr-13 6:21am
   
d.Items.Clear();

on this line
Prathap Gangireddy 5-Apr-13 6:01am
   
Though 'm not sure the error may be cumin in this line
d.Items.Clear(); // just a guess
if it is so use d.Datasource=null;
Pallavi Waikar 5-Apr-13 7:56am
   
try this DropDownList d = (DropDownList)(GridView1.Rows[e.NewEditIndex].FindControl("DropDownList1"));
Mas11 5-Apr-13 8:10am
   
Same view here !
Prathap Gangireddy 5-Apr-13 9:37am
   
I guessed it right.So Archit are you able to solve the issue.
[no name] 6-Apr-13 1:11am
   
no it not work for me

1 solution

You should also indicate what is the offending line.
However, you are calling methods and not checking their return value. For instance FindoControl[^] method may return null (if it is indeed null, then the very next line will throw).
   

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



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