Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Object Cannot be cast from DBNull to other types

Posted on 2004-08-23
7
Medium Priority
?
868 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

636 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