1

my dropdownlist HTML5

<select runat="server" id="ddlStateLegalRes" name="ddlStateLegalRes"></select>

On my page load I am binding my dropdownlist to a list collection with a list of states

protected void Page_Load(object sender, EventArgs e)
    {

     if (!Page.IsPostBack)
                {
                    List<StateList> states = ops.getStates();
                    ddlStateLegalRes.DataTextField = "StateRegionName";
                    ddlStateLegalRes.DataValueField = "StateRegionCode";
                    ddlStateLegalRes.DataSource = states;
                    ddlStateLegalRes.DataBind();

    GetAllInfo();

EDITED** There is something weird going on and I cant seem to pinpoint it when I debug. For some strange reason this was working as it is supposed to but then started throwing this Error "Object Reference not set to an instance of an object"

I know the list collection is getting the correct values and I also know that "STATEOFLEGALRESIDENCE" is being returned by my stored procedure with the value "FL" and the spelling is correct.

"""Inside my GetAllInfo() method
    while (sdr.Read())
    {
          ddlStateLegalRes.Items.FindByValue(sdr["STATEOFLEGALRESIDENCE"].ToString()).Selected = true;
    }

What i suspect is that it is trying to set my dropdownlist with a value before the code is actually done binding my dropdownlist with values. So i tried to move the GetAllInfo() method into the Page_LoadComplete event and still the same error so now I am at a lost please help

Thanks

1 Answer 1

0

It sounds like the value that is returned from the reader does not match exactly or it is missing from the list items.

I would put a break point at the point & check that these values match:

Try this but replace 0 with the value of the item.

 ddlStateLegalRes.Items[0] == sdr["STATEOFLEGALRESIDENCE"].ToString());

It sounds like a case or white space triong issue.

Sign up to request clarification or add additional context in comments.

7 Comments

I would also refactor the above by moving the populate into it's own method i.e. BindStates method which encapsulates the functionality.
Thanks hutch yes i tried this and it still returns the same error also I have refactored per your suggestion to make the code cleaner. Any other ideas?
when i debug I see the correct value being return by my reader seems like it is throwing an exception once it trys to set the dropdownlist to that value
I also see the list collection is returning and the dropdownlist is binding all the states as it is supposed to if i comment out the code that is simply telling the dropdown which state to display. This is so frustrating seems like this should be working and i cant see what I am doing wrong
Please see my other comments, give that a go. :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.