Solved

Object Cannot be cast from DBNull to other types

Posted on 2004-08-23
7
856 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Independent Software Vendors: 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

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…
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 …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

724 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