checking to see if DDL value exists

D

Darrel

I'm trying to add an extra layer of error checking on a Drop Down List. The
list is populated from one table, and then I select the selectedValue from
another DB.

While it SHOULDN'T ever happen, just incase, I want to check that the
selected value actually exists as a value in the dropDownList before I
actually try to select it.

I've tried this:

if
(DropDownList_category1.Items.FindByValue(ds.Tables(0).Rows(0)("listingCategory1")).Value.ToString
= "") then...

But that just gives me a " Object reference not set to an instance of an
object. " error.

-Darrel
 
L

Lowell Heddings

What you would probably want to do is do the checking against the
dataset before you actually do the binding. Even if the selected value
doesn't exist, I'm not sure that anything will happen other than nothing
will be selected. You could easily test this by manually setting the
selectedvalue to something that you know does not exist.

Lowell
 
D

Darrel

What you would probably want to do is do the checking against the dataset
before you actually do the binding. Even if the selected value doesn't
exist, I'm not sure that anything will happen other than nothing will be
selected. You could easily test this by manually setting the selectedvalue
to something that you know does not exist.

Well, the catch is if the item doesn't exist, I actually want it to select a
differerent item.

As for checking in the dataset, I'm still not quite sure how to go about
that. I could grab the list of categories from table1 and put it in an
array, then, when I grab the record from table2, I could compare the
pertinent field with that array. I'm trying to come up with a simpler
solution though.

Let me sleep on it ;o)

-Darrel
 
W

weichung[MCSD,MCDBA]

Hi,

before this code

if
(DropDownList_category1.Items.FindByValue(ds.Tables(0).Rows(0)("listingCategory1")).Value.ToString
= "") then...

add

if DropDownList_category1.Items.count > 0 then

remember to add the "End If" tag if you are in VB.NET

hope it helps

weichung

hope
 
D

darrel

add
if DropDownList_category1.Items.count > 0 then

remember to add the "End If" tag if you are in VB.NET


I don't think I'm quite explaining this correctly. Let's say my DDL has the
following value options:

3
4
5
8
9
12
0

These would be all the active options pulled from Table 1.

Then, in table 2, I grab the data for a specific record, who's foreign key
for Table 1 is '2'.

Since 2 doesn't exist as an option in the DDL, I can't select it. So,
instead, I want to select '0'.

However, all this said, I'm thinking there really should be no reason to do
this error checking here. I really need to spot the problem prior to this
step. That said, I am curious if there is a simple way to see if a DDL.item
exists.

-Darrel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top