RE: DBnull, Date and DataAdapter by bluenirvana
bluenirvana
Wed Jan 05 14:53:02 CST 2005
Actually I found what I was needing, but you are right, dr("CloseDate") works
fine. But in sticking with the strongly type theme, the datarow exposes a
sub called dr.SetCloseDateNull() that does the work for you. I guess I did
not look hard enough for it.
Thanks
"davisonm" wrote:
> One sleazy way to do it is just sneak around the datatype conversion code
> (the source of the exception) by indexing the row instead of referring to the
> column property:
>
> dr("CloseDate") = DbNull.Value
>
> I have difficulties with this as well, and if there is a better way to
> handle it, I don't know it.
>
> Alternately, if your problem is validating and/or translating user entry
> into a bound textbox on a windows form, then you can declare at the top of
> your forms class:
>
> Private WithEvents MyBinding As Binding
>
> then copy the datetime textbox binding to it in, say, form Load:
>
> MyBinding = MyTextBox.DataBindings.Item("Text")
>
> and finally, handle the Format and Parse events for this binding:
>
> Private Sub MyBinding_Format(ByVal sender As Object, ByVal e As
> System.Windows.Forms.ConvertEventArgs) Handles MyBinding.Format
> If (e.DesiredType Is GetType(System.String)) Then
> If (e.Value Is System.DBNull.Value) Then
> e.Value = ""
> Else
> e.Value = CType(e.Value, DateTime).ToString("d")
> End If
> End If
> End Sub
>
> Private Sub MyBinding_Parse(ByVal sender As Object, ByVal e As
> System.Windows.Forms.ConvertEventArgs) Handles MyBinding.Parse
> If (e.DesiredType Is GetType(DateTime)) Then
> If (e.Value = "") Then
> e.Value = System.DBNull.Value
> Else
> Try
> e.Value = DateTime.Parse(e.Value)
> Catch ex As FormatException
> MessageBox.Show("The value '" & e.Value & "' is not a
> recognizable date.")
> End Try
> End If
> End If
> End Sub
>
> And if it's ASP code you're after, then you need someone else...
>
> Good luck,
> Mark
>
>
> "blue_nirvana" wrote:
>
> > Is there a way to assign a datetime column in SQL Server to Null using a
> > SQLDataAdapter? I know you can do it by calling a stored procedure and
> > setting a parameter equal to DBnull.Value. But it does not make since to me
> > that you can not set a datetime column to null from a SQLDataAdapter.
> >
> > Thanks