[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Object Cannot be cast from DBNull to other types

Posted on 2004-08-23
7
Medium Priority
?
871 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
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!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month17 days, 14 hours left to enroll

830 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