Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
hi friends
i bind dropdown and generate this error Object reference not set to an instance of an object.
and my code is

C#
if (!IsPostBack)
        {
           bind_drop_down_degree();
        }
protected void bind_drop_down_degree()
    {
        DropDownList ddldegree = gvOtherDegree.FindControl("ddl_degree") as DropDownList;
        Property_degree_master pdm = new Property_degree_master();
        pdm.faculty_id = 1;
        DataTable dt_degreess = new DataTable();
        dt_degreess = BAL_Employee.bind_degree_by_faculty_id_test_tesdt(pdm);
        ddldegree.DataSource = dt_degreess;
        ddldegree.DataTextField = "degree_name";
        ddldegree.DataValueField = "degree_id";
        ddldegree.DataBind();
    }

public static DataTable bind_degree_by_faculty_id_test_tesdt(Property_degree_master pdm)
    {
        SqlCommand cmd = new SqlCommand();
        CreatePara para = new CreatePara();
        cmd.CommandText = "get_degree_byfaculty_sp";
        cmd.Parameters.Add(para.IntInputPara("@faculty_id",pdm.faculty_id));
        DataTable dt = CreateCommand.ExecuteQuery(cmd);
        return dt;
    }


plz help me whats a problem
Posted
Updated 26-May-13 20:00pm
v2
Comments
KK Kod 27-May-13 2:02am    
In which line you are getting the error .. would you mind posting the stack trace .. so that we can diagnose properly .
[no name] 27-May-13 2:11am    
this line to gate error ddldegree.DataSource = dt_degreess;
[no name] 27-May-13 2:05am    
this line to gate error
ddldegree.DataSource = dt_degreess;
KK Kod 27-May-13 2:22am    
check dt_degreess has got data using quick watch.whether your method BAL_Employee.bind_degree_by_faculty_id_test_tesdt(pdm); is returning null.
KK Kod 27-May-13 2:23am    
i wanted to know that is that method returning data table with data or null

C#
if (!IsPostBack)
        {
           bind_drop_down_degree();
        }
protected void bind_drop_down_degree()
    {
        int rowNum=2;
		int colNum=1;
		/*
	      Some time you will not be getting the ddl_degree  control from the grid view 
		  so try this code to get the control from the grid view
		*/
		DropDownList ddldegree = (DropDownList)gvOtherDegree.Rows[rowNum].Cells[colNum].FindControl("ddl_degree");
        Property_degree_master pdm = new Property_degree_master();
        pdm.faculty_id = 1;
        DataTable dt_degreess = new DataTable();
        dt_degreess = BAL_Employee.bind_degree_by_faculty_id_test_tesdt(pdm);
        ddldegree.DataSource = dt_degreess;
        ddldegree.DataTextField = "degree_name";
        ddldegree.DataValueField = "degree_id";
        ddldegree.DataBind();
    }
 
public static DataTable bind_degree_by_faculty_id_test_tesdt(Property_degree_master pdm)
    {
        SqlCommand cmd = new SqlCommand();
        CreatePara para = new CreatePara();
        cmd.CommandText = "get_degree_byfaculty_sp";
        cmd.Parameters.Add(para.IntInputPara("@faculty_id",pdm.faculty_id));
        DataTable dt = CreateCommand.ExecuteQuery(cmd);
        return dt;
    }

If you have got ddl_degree control placed in all populated row of the grid view
loop through the grid view
C#
foreach (gvOtherDegree row in gvOtherDegree.Rows)
      {
          DropDownList ddldegree = (DropDownList)row.FindControl("ddl_degree");
          ddldegree.DataSource = dt_degreess;
          ddldegree.DataTextField = "degree_name";
          ddldegree.DataValueField = "degree_id";
          ddldegree.DataBind();
      }
 
Share this answer
 
As you are getting the error on the line
C#
ddldegree.DataSource = dt_degreess;
And it is the first reference to ddldegree after the declaration:
C#
DropDownList ddldegree = gvOtherDegree.FindControl("ddl_degree") as DropDownList;
Then either "ddl_degree" does not exist as a findable control, or it is not created as a DropDownList. Either one would give you a null value and cause the error.

Check your control name, and then check it has "runat=server" in it's definition.
 
Share this answer
 
Comments
[no name] 27-May-13 2:21am    
my asp. code is
<asp:DropDownList runat="server" ID="ddl_degree" Style="width: 150px; border: solid 1px #C9C9C9;
margin-left: ">
<asp:ListItem Value="0">Select Degree

and ddl_degree is correct

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



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