Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Null Dates - binding to IBindingList Objects & storing in SQL Server
 

Null Dates - binding to IBindingList Objects & storing in SQL Server

Here is the scenario: A windows form has a calendar control. The user needs to have to option to either set a date in the control, or leave it blank. If the user leaves it blank, a null value needs to be saved to the database. Also, the control needs to display either a blank field or '(none)'.

The Calendar control is bound to an object that inherits from Collection Class and Implements IBindingList. The data is saved to a SQL Server 2000 database via a call to a stored procedure. (We use the ApplicationBlock for Data Access from MS for out client-side data access layer.)

Currently, the datatype in the object is a Date. It is exposed through a read/write property.
ie: 
Private mDate as Date
public property MyDate as Date
//code to get and set
end property

Question: When I try to save the date value as a null, I get a SQL Server exception. I have tried making the mDate variable a SQLTypes.SqlDate value, but this did not work.

Any suggestions on how I can accomplish this would be greatly appreciated. I know I can perform a type swap at the Property, but would prefer a more elegant solution, if one exists.

Thanks.
MigrationUser 1  Wednesday, April 30, 2003 7:07 PM
Are you putting the value into a SQLParameter and passing it to the stored proc?  If so, then you can handle dates like this:


If MyDate Is Nothing OrElse MyDate.ToString = "" Then
       cmd.Parameters.Add(New SqlParameter("@MyDate", SqlDbType.SmallDateTime, 4)).Value = DBNull.Value
Else
       cmd.Parameters.Add(New SqlParameter("@MyDate", SqlDbType.SmallDateTime, 4)).Value = MyDate 
End If


And then you shouldn't have any problems, assuming the column in the Table is nullable...

Hope this helps.
MigrationUser 1  Thursday, May 01, 2003 9:27 AM
It's also sometimes a good idea to setup functions to go and get the correct value if you are going to be doing it a lot in code.  So off of Jacob's code, you could do something like this in case you need to do the same thing again for a different field.


cmd.Parameters.Add(New SqlParameter("@MyDate", SqlDbType.SmallDateTime, 4)).Value = GetCorrectDateTime()

Private Function GetCorrectDateTime(ByVal Value As String) As Object
     If Value Is Nothing OrElse Value = "" Then
          Return DBNull.Value
     Else
          Return Value
     End If
End Function
MigrationUser 1  Thursday, May 01, 2003 9:25 PM

You can use google to search for other answers

Custom Search

More Threads

• Data Validation for a bound VB form
• ComboBox & DataBindings Problem
• HELP - data grid creating duplicate controls?
• Accessing data in a hidden column
• how to bind an array of string with DataGridView ?
• Help with strange error message - dataGrid
• Dictionary<int, IList<string>> as data-source in master-detail view
• modify connectionstring at runtime
• update dataview to dataset
• Data Validation on the DataGridView -- or, when does setting CurrentCell actually work :-)