Solved

FindByValue() error? Object - Object reference not set to an instance of an object.

Posted on 2004-08-19
6
1,150 Views
Last Modified: 2012-05-05
Ok,
  I've been coding for the past few days without a hitch and then all of a sudden I run across this problem.
I have a drop down list that gets populated with a dataset that is returned from my business tier.

I've debugged this thing for 5 hours and I still cant figure it out. I've tested the dataset to make sure that it gets populated. I've checked to make sure that the page will load properly if I just bind the Dataset to the Drop Down List. Everything works fine when I'm loading the Dataset, binding it to the Drop Down list, but when I try to do a dropdownlist.items.findbyvalue("12345")   (where 12345 is a product # in the drop down list) I get a: "Object reference not set to an instance of an object.".

I've tried hardcoding the value (that I KNOW for a fact is in the DropDownList), I"ve tried dynamically setting it through the Ds, I've tried weird looping options. I've even tried removing the DropDownList and replacing i with a listbox. But again, my dreams and aspirations are cursed by this darn error.

Here is the code i'm using:

        Dim DS As New DataSet()
        Dim DS2 As New DataSet()
        Dim itemNumb As String

        m_bus = New business()

        DS = m_bus.ReturnAvailableItems()
        DS2 = m_bus.ReturnCurrentFeaturedItems()

        ddlFeat1.DataSource = DS
        ddlFeat1.DataTextField = "ITEMDESC"
        ddlFeat1.DataValueField = "item_number"
        ddlFeat1.DataBind()

        itemNumb = DS2.Tables(0).Rows(0)("item_number")
        ddlFeat1.Items.FindByValue(DS2.Tables(0).Rows(0)("item_number")).Selected = True

The last line is where i get the error.

Does anyone see what might be causing this?
0
Comment
Question by:sneidig
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 20

Expert Comment

by:ihenry
ID: 11842769
you can do this to avoid the error,

If Not IsDbNull(itemNumb)
   If ddlFeat1.Items.Count > 0 Then
      ddlFeat1.Items.FindByValue(itemNumb).Selected = True
   End If
End If
0
 

Author Comment

by:sneidig
ID: 11843026
I know that the ddlFeat1 is getting populated. I know that the ItemNumb is populated.

And i still get the error. That seems to be my issue.

Thanks again.
0
 
LVL 20

Accepted Solution

by:
ihenry earned 500 total points
ID: 11843197
Or that might because there's no ddlFeat1.Items.FindByValue(itemNumb) found.

Try this to check if item with value equal itemNumb exists.

Dim ListItem item As ListItem = ddlFeat1.Items.FindByValue(itemNumb)
If Not IsNull( item)
   item.Selected = True
End If
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:sneidig
ID: 11843396
it says the object is null.

So check it out though...

I comment out the ".findbyvalue()" and just let the page load.

The page then loads up all three DropDownLists perfectly.

If i look at the source, the data is found in there.

OH jeez... i have no idea...
0
 
LVL 20

Expert Comment

by:ihenry
ID: 11843612

Right click on the browser and view its source code. Do you see value there?
0
 

Author Comment

by:sneidig
ID: 11843825
I started doing a trace ... and I found my problem (your code helped me thinka bout it in a different way so I'll reward you the points).

The problem was that the item numbers in the DB are stored as Varchar(20). When i'd get the data from one table it would return an item number that looked like this "11355" and then the other tablle (which was what the DDL was bound to) looked like this "11355          "   It had a ton of white space.

I trimmed it in SQL Server and blammo, it works.

Thanks for the help though.

Now if i could only get these last 6 hours back!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

739 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