Select a default value from the dropdown box

Hi,

How can I set the dropdown box to default a value 4400 .

I am doing this but I have an error.

DDLAccount.Items.FindByValue("4400").Selected = "true"


the error says this:

Object reference not set to an instance of an object.
NullReferenceException was unhandled by user code.

I am not sure what that means.
lulu50Asked:
Who is Participating?
 
oobaylyConnect With a Mentor Commented:
Any whitespace for example. Though I think guru_sami has got it right. I made the (wrong) assumption that setting the list has been databound if setting the SelectedIndex succeeds. It turns out that setting the SelectedIndex is kept even if items are bounds to the list afterwards. I'm mainly a Windows Form person, and if doing it that way with a ComboBox would result in an ArgumentOutOfRangeException
0
 
oobaylyCommented:
It appears that the value "4400" doesn't exist in the DropDownList. Where are you calling this code from? It's probable that you're calling it before any databinding has been done.
0
 
lulu50Author Commented:
oobayly,

how come if I do this it's working.

DDLAccount.SelectedIndex = "131"

it select a default value of 4400
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
lulu50Author Commented:
          <td align="left" style="width: 155px;">
                                                                                                                                <asp:DropDownList   ID="DDLAccount" runat="server" DataSourceID="SqlDataSource2" DataTextField="Acct"
                                                                                                                                    DataValueField="Acct" Width="130px">
                                                                                                                                </asp:DropDownList>
                                                                                                                                <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:prodConnectionString %>"
                                                                                                                                    SelectCommand="SELECT [Acct] FROM [Account] ORDER BY [Acct]"></asp:SqlDataSource>
                                                                                                                            </td>
0
 
oobaylyCommented:
OK, can you confirm that there's no padding in the fields returned from the database?
0
 
guru_samiCommented:
You should use DataBound event of your dropdownlist to set that value.
I think you are doing that in Page_Load
Like this:
 protected void DDLAccount_DataBound(object sender, EventArgs e)
    {
        DDLAccount.Items.FindByValue("4400").Selected = true;
    }
0
 
lulu50Author Commented:
what is padding
0
 
lulu50Author Commented:

I still have the same error message.


Protected Sub DDLAccount_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles DDLAccount.DataBound

        DDLAccount.Items.FindByValue("4400").Selected = True


    End Sub
0
 
lulu50Author Commented:
Object reference not set to an instance of an object.
0
 
oobaylyCommented:
Can you set the selected item by index in the DataBound event handler, then add a breakpoint at End Sub and check the value of DDLAccount.SelectedValue & the boolean value
Protected Sub DDLAccount_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles DDLAccount.DataBound
  ' Don't need quotes as SelectedIndex is an Integer
  DDLAccount.SelectedIndex = 131
 
  Dim b As Boolean = (DDLAccount.SelectedValue = "4400")
End Sub

Open in new window

0
 
guru_samiConnect With a Mentor Commented:
Hmm lets debug now
A little modification to the code above. Adding one more line for debugging:

       int x = DropDownList1.Items.Count; //set a breakpoint at this line
        DropDownList1.Items.FindByValue("Tofu").Selected = true;

Now set a breakpoint, run your program and press F11 once it hits the line. note what is the value of x when your control moves to next line.
0
 
lulu50Author Commented:
   Dim x As Integer = DDLAccount.Items.Count

        DDLAccount.Items.FindByValue("4400").Selected = True

I did the x is equal to 257
0
 
lulu50Author Commented:
oobayly

I don't want to use SelectedIndex because the index may change.
0
 
guru_samiCommented:
alrite then I think
FindByValue("4400") is what it is referencing null ...

So now as suggested by "oobayly" make sure 4400 is trimmed i.e. no white-space padding.

also try using FindByText("4400")
Or try using some value other than 4400 and see if it is making any difference

0
 
oobaylyCommented:
I'm not expecting you to use SelectedIndex permamently, just for testing to make sure the value of 4400 is in fact equal to "4400".

My reasoning is that just because a variable looks like 4400, it doesn't mean it is equal to "4400". ie.
"4400   " <> "4400". Remember, we're doing string comparisons here.
0
 
lulu50Author Commented:
Thank you
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.