DBNull problem in my Datalist

andycharger

Centurion
Joined
Apr 2, 2003
Messages
152
Can somone PLEASE help!

I have a datalist that is populated from a SQL statement when my page loads.

However, I have 2 date fields in my SQL database and only 1 will ever be populated.

So when my page loads and date1 is populated, my page falls over when it tries to databind date2 to a cell in my table and vice versa!

i.e.
Visual Basic:
<%# DataBinder.Eval(Container.DataItem, "date1").ToShortDateString() %>

I have to convert to a ToShortDateString otherwise my date comes out in a long format.

Is there a way to ignore binding the date if its DB null or a way to get the page to ignore DBNulls?

Any help would be appreciated!

Andy
 
Are you saying that date2 will never be populated? In that case why bind it OnLoad. Or are you saying you don't know which one will be populated?

Couldn't you query the db and set the return value to a variable and then test that variable for DBNull?

myVar = objCmd.ExecuteQuery()

if myVar = DBNull Then
'Don't bind or bind to a dummy variable
Else
'Bind'
End if

What db are you using? Sometimes working with dates can get hairy.

One thing you can do, and it's kind of a hack, is to put the bind logic into a try catch block. If it fails to bind, maybe because of the null, then you insert the value you want.
 
Back
Top