assign db value to dropdownlist when its null

how do you handle null integer values from an objectdatasource.

i'm trying to assign the value of a dropdownlist, when its null, i'm happy to just pass by the statement, but nothing i'm trying works.

ie.
If Not dr.Userid Is DBNull.Value Then ddlUser.Text = dr.Userid

but i keep getting an error that null cannot be casted.  what is the proper way to do this?
Fraser_AdminAsked:
Who is Participating?
 
rajeeshmcaConnect With a Mentor Commented:
It will convert the DBNull to empty string.. Check it out

if dr("SubContract_Transfer_Number").ToString() <> String.Empty Then
    dropdownlist1.Text  = dr("SubContract_Transfer_Number").ToString()
End If
0
 
technofileConnect With a Mentor Commented:
if dr.userid.equals(system.DBNull.Value)
'nothing
else
'something
end if
0
 
technofileConnect With a Mentor Commented:
if not dr.userid.equals(system.DBNull.Value)
'something
end if
 
Should work too
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Fraser_AdminAuthor Commented:
Thought i would try first with a string field and textbox.  This does not work.

The value for column 'SubContract_Transfer_Number' in table 'Data_SubContract' is DBNull.

I get that message when I have this code:
If Not dr.SubContract_Transfer_Number.Equals(System.DBNull.Value) Then
   txtSubContractTransferNumber.Text = dr.SubContract_Transfer_Number
End If
0
 
Fraser_AdminAuthor Commented:
If Not dr.IsSubContract_Transfer_NumberNull Then
   txtSubContractTransferNumber.Text = dr.SubContract_Transfer_Number
End If

This works OK but how do you do it if the field is not accessible this way and you have to do
dr("SubContract_Transfer_Number")??

0
 
technofileConnect With a Mentor Commented:
if dr is a dataset AND by drop down list you mean combo box you do this
dim row as integer

if not dr.Tables("table").Rows(row).Item("field").Equals(System.DBNull.Value) then
ddl.Items.Add(dr.Tables("table").Rows(row).Item("field").ToString)
end if
this suedo code you are writing is very confusing
0
 
rajeeshmcaConnect With a Mentor Commented:
Hi

Just check for whether it return empty like

if dr("SubContract_Transfer_Number").ToString() <> String.Empty Then
    dropdownlist1.Text  = dr("SubContract_Transfer_Number").ToString()
End If
0
 
Kelvin McDanielConnect With a Mentor Sr. Developer/ConsultantCommented:
I think what you're looking for is...

If Not IsDBNull(dr("SubContract_Transfer_Number")) Then
   txtSubContractTransferNumber.Text = dr("SubContract_Transfer_Number").ToString
End If
0
All Courses

From novice to tech pro — start learning today.