Click here to Skip to main content
15,911,132 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello sir,
I want to ask that I have to call values that is saved in my backend I have to call them in a dropdown that is inside an itemtemplate field in gridview

I m using this code

C#
protected void grdDaily_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataTable dt = new DataTable();
        dt = DelayworkReport.fillProjects();
        ListItem lst = new ListItem("Select", "0");
        DropDownList list = (DropDownList)grdDaily.FindControl("ddlProject");
        list.DataTextField = "ProjectABR";
        list.DataValueField = "ProjectId";
        list.DataSource = dt;
        list.DataBind();
        list.Items.Insert(0, lst);
    }

When I debug this code using F10 the debugging stop at
list.DataTextField = "ProjectABR";
the error comes of object Reference not set to an instance of the object Please guide me in my code
Posted
Updated 30-Nov-11 20:01pm
v2
Comments
Sergey Alexandrovich Kryukov 1-Dec-11 2:08am    
What does it mean "call values"? You call only methods, functions, procedures...
--SA
Sergey Alexandrovich Kryukov 1-Dec-11 2:09am    
Where is your exception information?
--SA
Sergey Alexandrovich Kryukov 1-Dec-11 2:14am    
I see. Is the debugger broken?
--SA
Prashant Srivastava LKO 1-Dec-11 5:30am    
Yes sir I solved by myself this line is wrong

DropDownList list = (DropDownList)grdDaily.FindControl("ddlProject");
agai I debug I found the exception and understand its meaning
Sergey Alexandrovich Kryukov 19-Dec-11 23:54pm    
Great. Debugger is a good thing.
Good luck,
--SA

C#
 protected void grdDaily_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //Projects values in drodpown
            dt = DelayworkReport.fillProjects();
            ListItem lst = new ListItem("Select", "0");
            DropDownList list = (DropDownList)e.Row.Cells[5].FindControl("ddlProject");
            list.DataTextField = "ProjectABR";
            list.DataValueField = "ProjectId";
            list.DataSource = dt;
            list.DataBind();
            list.AutoPostBack = true;
            list.Items.Insert(0, lst);
}
}


I have solved by myself the above question I asked thank u for help
 
Share this answer
 
v2
Not again! Is it a problem to run it under Debugger and see what's going on. If your report is correct, your call to FindControl returned null. A note for you: using FindControl for regular UI development is a sign of incorrect or bad design. Normally, you never need to search for any controls. All your controls are accessible to you, why searching. Also, using any immediate constants like your "dllProject" is really bad. If you misspell it, compiler cannot help to find a problem. Everything is wrong from the very beginning; if you fix this bug, some other will haunt you. You need to review the code design.

—SA
 
Share this answer
 

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