Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Select a default value from the dropdown box

Posted on 2009-04-29
16
Medium Priority
?
232 Views
Last Modified: 2012-06-27
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.
0
Comment
Question by:lulu50
  • 8
  • 5
  • 3
16 Comments
 
LVL 15

Expert Comment

by:oobayly
ID: 24261302
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
 

Author Comment

by:lulu50
ID: 24261344
oobayly,

how come if I do this it's working.

DDLAccount.SelectedIndex = "131"

it select a default value of 4400
0
 

Author Comment

by:lulu50
ID: 24261384
          <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
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
LVL 15

Expert Comment

by:oobayly
ID: 24261606
OK, can you confirm that there's no padding in the fields returned from the database?
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 24261622
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
 

Author Comment

by:lulu50
ID: 24261625
what is padding
0
 

Author Comment

by:lulu50
ID: 24261663

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
 

Author Comment

by:lulu50
ID: 24261674
Object reference not set to an instance of an object.
0
 
LVL 15

Accepted Solution

by:
oobayly earned 1000 total points
ID: 24261709
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
 
LVL 15

Expert Comment

by:oobayly
ID: 24261756
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
 
LVL 41

Assisted Solution

by:guru_sami
guru_sami earned 1000 total points
ID: 24261777
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
 

Author Comment

by:lulu50
ID: 24261906
   Dim x As Integer = DDLAccount.Items.Count

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

I did the x is equal to 257
0
 

Author Comment

by:lulu50
ID: 24261923
oobayly

I don't want to use SelectedIndex because the index may change.
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 24262025
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
 
LVL 15

Expert Comment

by:oobayly
ID: 24262034
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
 

Author Closing Comment

by:lulu50
ID: 31575970
Thank you
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question