Solved

Object Cannot be cast from DBNull to other types

Posted on 2004-08-23
7
831 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
  • 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 75 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now