Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, All

Here i try to show two columns in dropdownlist, when i retrieve the data from DB showing Error Object Reference error !

My Code IS:

C#
DataTable Dtab_1 = new DataTable();
            Dtab_1 = DbCon.GetData("select ReasonCodeC +' /  '+ReasonNameC as ReasonD from reason");
            ddlresoncode.DataSource = Dtab_1;
            ddlresoncode.DataTextField = "ReasonD";
            ddlresoncode.DataBind();
            DataTable Dtab_2 = new DataTable();
            Dtab_2 = DbCon.GetData("select r.ReasonCodeC+' / '+r.ReasonNameC as reasonD from reason as r inner join holiday as h on r.ReasonCodeC=h.ReasonCodeC where h.HolidayNameC='" + strCmd + "'");
            ddlresoncode.Items.FindByText(Dtab_2.Rows[0]["reasonD"].ToString()).Selected = true; // Here i Got Error!
Posted

1 solution

The error is probably caused by FindByText returning NULL and you not checking it before using it.
Always check for object existence and validity before using it.

Object sel = ddlresoncode.Items.FindByText(Dtab_2.Rows[0]["reasonD"].ToString());
if ( sel != null)
sel.Selected = True
else // do something else
return;
 
Share this answer
 
Comments
prasanna.raj 11-Sep-14 8:11am    
i have confuse in that part, i passed value by datatable in dropdown but why its return null....
Sinisa Hajnal 11-Sep-14 8:17am    
Because the control is nested (find the exact html from the page)? Because your inner join didn't return anything (so that rows[0] actually fails and not FindControl)? Until you find exactly what is going on, there is nothing more I can do.

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