Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

[2005] Null Values When Using Databinding

Status
Not open for further replies.

Jonny1409

Technical User
Jan 15, 2006
6
GB
Hello,

I am using Databinding to bring values from an SQL Server DB through onto my form.

The code is :

Code:
txtName.DataBindings.Clear()
txtName.DataBindings.Add("Text", ds.Tables(0), "Name")

This works well in most cases, but as I have a couple of DateTimePickers on my form aswell, and these don't work so well.

Basically they are fine as long as there isn't a null value in the Database.
If one of the Date fields is null (i.e. Date Of Birth), then I get thrown an error of :

"Object cannot be cast from DBNull to other types."

What is the best way to resolve this ? (Apart from ensure there are no Null fields in the DB, as this isn't possible at the moment.)

I'd be happy for the DateTimePickers to be populated with a default date or be blank, whichever is easier really.

Thanks,
J.
 
In your SQL query, use the IsNull function to return a default date if the date is null:

Select Field1, Field2, IsNull(DateField1, Cast('1/1/1900' as DateTime)) from SomeTable

the query above will return 1/1/1900 as a DateTime for any null values in DateField1.



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top