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

x
?
Solved

Handling SQL Server Fields with Null Values on VB .Net Windows Forms

Posted on 2004-10-29
6
Medium Priority
?
749 Views
Last Modified: 2012-06-27
I have a SQL Server 2000 database which contains tables with nullable fields.  One such field is [Date Complete].  In my code (VB .Net 2003), I bind the field to a textbox control using the code below.

----
txtDateComplete.DataBindings.Add(New Binding("Text", drProject, "Date_Complete"))
----

When the application runs, I receive the following error as a result.

----
Additional information: Property accessor 'Date_Complete' on object 'Project_Tracker.sqldsProject+ProjectRow' threw the following exception:'Cast from type 'DBNull' to type 'Date' is not valid.'
----

I do not have any formatting attributes applied to the textbox control as that doesn't appear to be an option in the properties window for the control.

Does anyone know how I can get controls on my Windows forms to accept NULL values from fields?  Any help would be greatly appreciated.  Hopefully this is a simple question for soem of you.  I am learning the .Net technology and for me, this issue has been a stumbling block for several days now.
0
Comment
Question by:Edward Stevens
[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
  • 2
6 Comments
 
LVL 28

Expert Comment

by:iboutchkine
ID: 12444912
what is drProject?
I fthis is datarow you can determine if this is null or not

something like that

If IsDBNull(drProject("colname")) Then
    lblWhatever.text="nill"
Else
    lblWhatever.text=drProject("colname")
End If
0
 
LVL 1

Author Comment

by:Edward Stevens
ID: 12445481
Yes, drProject is a datarow.  From your example - it sounds like I would have to manually set the text value of the control and then manually set the column value before any updates took place on the dataset.

Isn't there a way to directly "bind" the field to the control so that this is done automatically?

I have even tried using the Format event of the Binding class to handle this but that did not work either.  Below is my code for my attempt with the Format event.

----
            dbDataBinding = New Binding("Text", drProject, "Date_Complete")
            AddHandler dbDataBinding.Format, AddressOf DateReFormat
            txtDateComplete.DataBindings.Add(dbDataBinding)



      Private Sub DateReFormat(ByVal Sender As Object, ByVal CEvent As ConvertEventArgs)
            If Not CEvent.DesiredType Is GetType(String) Then
                  Exit Sub
            End If
            CEvent.Value = DatePart(DateInterval.Month, CEvent.Value).ToString("00") + _
            "/" + DatePart(DateInterval.Day, CEvent.Value).ToString("00") + _
            "/" + DatePart(DateInterval.Year, CEvent.Value).ToString("0000")
      End Sub
----

The problem I have with the above code is that I do not know how to test for a DBNull value.  The code blows up just as the binding does in the previous example when it encounters a null value in the field.

How do you ask if CEvent.Value is DBNull?
0
 
LVL 1

Author Comment

by:Edward Stevens
ID: 12445495
iboutchkine

Never mind.  I see how to ask about DBNull in the example you gave.  I am a dummy.  Sorry.
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 1

Author Comment

by:Edward Stevens
ID: 12445508
iboutchkine

I am going to try my Format code with the IsDBNull logic to see if I can use that to resolve my problem.  I'll post another response here shortly with the outcome of that test.  Thanks
0
 
LVL 1

Author Comment

by:Edward Stevens
ID: 12445595
iboutchkine

I modified my code as shown below and I still get an error at run-time when the application tries to bind the field to the control.  The Format event is not doing the trick.  I guess I'll have to manually set the control's value and then manually push the value back to the record.

----
      Private Sub DateReFormat(ByVal Sender As Object, ByVal CEvent As ConvertEventArgs)
            If Not CEvent.DesiredType Is GetType(String) Then
                  Exit Sub
            End If
            If IsDBNull(CEvent.Value) Then
                  CEvent.Value = " "
            Else
                  CEvent.Value = DatePart(DateInterval.Month, CEvent.Value).ToString("00") + _
                  "/" + DatePart(DateInterval.Day, CEvent.Value).ToString("00") + _
                  "/" + DatePart(DateInterval.Year, CEvent.Value).ToString("0000")
            End If
      End Sub
----
0
 
LVL 28

Accepted Solution

by:
iboutchkine earned 750 total points
ID: 12445871
I am afraid so
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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 …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

596 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