?
Solved

Object Cannot be cast from DBNull to other types

Posted on 2004-08-23
7
Medium Priority
?
864 Views
Last Modified: 2008-01-09

Hai All,

I am having a DateTimePicket control on a form which is bound to a column in a dataset. This is the binding of DateTimePicker control:

 dateTimeGradDate.DataBindings.Add(New Binding("Value", dsPersonalInfo.Tables("personalInfo"), "gradDate"))

The gradDate will be NULL in the data base. But, I used a default value of current date when filling the dataset. That code is as follows:

dsPersonalInfo.Tables("personalInfo").Columns("gradDate").DefaultValue = System.DateTime.Today

When I run the program, I get "Object Cannot be cast from DBNull to other types" exception

How do I handle this exception?

Thanks


0
Comment
Question by:karthikeyanTP
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 3

Expert Comment

by:DJ_Back-Q
ID: 11870606
If Not [Database Field] Is DBNull.Value Then
          .... your code here ...
Else
          .... code here if it's null
End if



Andre,
MCSD
0
 
LVL 2

Author Comment

by:karthikeyanTP
ID: 11870774
I already thought of that idea. I am confused about  how could I get Database Field? and where should I place that code?

 If Not [Database Field] Is DBNull.Value Then
          .... your code here ...
Else
 
I tried...

sqldaPersonalInfo.Fill(dsPersonalInfo, "personalInfo")
'The following code won't work
If dsPersonalInfo.Tables("personalInfo").Columns("gradDate").value is DBNull.value Then  
....
End If

But, there is no value propetry listed in intellisense. Can you explain more? and how?

Thanks,
0
 
LVL 3

Expert Comment

by:DJ_Back-Q
ID: 11870857
You should go more like this.

dsPersonalInfo.Tables("personalInfo").Rows([currentrow]).Item("gradDate")


replace current row by 0 if you want the first row.... or make a loop if you want them all.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:karthikeyanTP
ID: 11871001
My dataset will have just only one row. So, I tried the following

If dsPersonalInfo.Tables("personalInfo").Rows(0).Item("gradDate") Is DBNull Then
                dsPersonalInfo.Tables("personalInfo").Columns("gradDate").DefaultValue = System.DateTime.Today
End If

Still, I get the same exception (Object Cannot be cast from DBNull to other types) at the line

 dateTimeGradDate.DataBindings.Add(New Binding("Value", dsPersonalInfo.Tables("personalInfo"), "gradDate"))

If I uncommet the line, everything works fine. Am I doing the binding for datetimepicker right? Is there any way to set a defult value to datetimepicker?
(P.S. After I click "ok" button to the exception, it displays today's date, but other fields were not displayed correctly)
0
 
LVL 3

Expert Comment

by:DJ_Back-Q
ID: 11871622
You forgot the Not..



If Not dsPersonalInfo.Tables("personalInfo").Rows(0).Item("gradDate") Is DBNull Then
                dsPersonalInfo.Tables("personalInfo").Columns("gradDate").DefaultValue = System.DateTime.Today
End If
0
 
LVL 3

Accepted Solution

by:
DJ_Back-Q earned 225 total points
ID: 11871705
Your whole code should be has followed:


If Not dsPersonalInfo.Tables("personalInfo").Rows(0).Item("gradDate") Is DBNull Then
       dateTimeGradDate.DataBindings.Add(New Binding("gradDate", dsPersonalInfo, "personalInfo"))
Else
       dateTimeGradDate.Value = Now()
End If
0
 
LVL 2

Author Comment

by:karthikeyanTP
ID: 11872238
Your code contains error (maybe you didn't notice it)
dateTimeGradDate.DataBindings.Add(New Binding("gradDate", dsPersonalInfo, "personalInfo"))

There is no property "gradDate". Either we need to use Value or Text property of DateTimePicker

The following is the correct code. The idea is to bind "Text" property of datatimepicker instead of "Value" property.  The "value" property is not handling DBNull values.

If dsPersonalInfo.Tables("personalInfo").Rows(0).Item("gradDate") Is DBNull Then
                dateTimeGradDate.Value = System.DateTime.Today
                dateTimeGradDate.DataBindings.Add(New Binding("Text", dsPersonalInfo.Tables("personalInfo"), "gradDate"))
Else
                dateTimeGradDate.DataBindings.Add(New Binding("Text", dsPersonalInfo.Tables("personalInfo"), "gradDate"))
End If

I want to give you points for assisting me to solve the problem. Thanks for your time and effort.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question