dropdownlist FindByText problem

S

Stephen Witter

I am getting the following error when setting a ddl to a value from a
recordset:
System.NullReferenceException: Object reference not set to an instance
of an object

Here is my code:
lstPART_TYPE.SelectedItem.Selected = False
lstPART_TYPE.Items.FindByText(rst("PART_TYPE").value).Selected = True

The ddl has only two list items (bulk & sterile) and the value and
text are the same.
The database field only contains one or the other values.

I have other ddls doing the same thing in the same block of code and
they work fine.
The only difference between these and the one above is these all
contain
booelan values (value = 0 or 1, text = yes or no, db field is bit)

I can't seems to get past this one.

Any help is appreciated.
 
M

Michael Tkachev

this error because your ddl does not have these text.

Maybe are you finding something value?

Are you sure that you are finding something text?

check it.

Moreover you should call ClearSelection method.

ddlContracts.ClearSelection()

ddlContracts.Items.FindByValue("val").Selected = true;
 
S

Stephen Witter

I got it top work with the following code:

if isdbnull(rst("PART_TYPE").value) then
lstPART_TYPE.Items.Insert(0, "")
else
lstPART_TYPE.SelectedItem.Selected = False
Dim t as string=rst("PART_TYPE").value
if t = "BULK" then
lstPART_TYPE.Items.FindByValue("Bulk").Selected = true
else
lstPART_TYPE.Items.FindByValue("Sterile").Selected =
true
end if
end if

One problem was in sql server the field data type was char(10) and
should have been varchar(10). The char(10) padded my string therefore
the strings didn't match. However, even though I fixed that it still
won't work unless I do it as the code above shows, which is a more
lines than I wanted.
 

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
474,077
Messages
2,570,567
Members
47,204
Latest member
abhinav72673

Latest Threads

Top