Howto bind a date to a textbox from a strongly typed dataset

  • Thread starter Thread starter AMDRIT
  • Start date Start date
A

AMDRIT

Hello,

I am trying to bind a date to a textbox. The date will be null initially
and may need to stay null. How is this done?

Is there a way to tell the textbox how to handle null values like in the
datagrid?

Thanks,
 
Cor,

Thanks for the response. My code is similare to what you have, and I am
still getting this exception:

An unhandled exception of type 'System.Reflection.TargetInvocationException'
occurred in system.dll

Additional information: Property accessor 'ExpirationDate' on object
'DelphiCommercial.DataDictionary+ApplicationsRow' threw the following
exception:'Cannot get value because it is DBNull.'


It doesn't appear to get to ever get to the format handler.

Here is what I have in place:

.... form code ...

b = New Binding("Text", dr, "ExpirationDate")
AddHandler b.Parse, AddressOf DataBindingManager.CurrencyStringtoDate
AddHandler b.Format, AddressOf DataBindingManager.DateToCurrencyString
Me.txtExpirationDate.DataBindings.Add(b)

.... DataBindingManager ...

Public Shared Sub DateToCurrencyString(ByVal sender As Object, ByVal e As
System.windows.forms.ConvertEventArgs)

Trace.WriteLineIf(Globals.ThisTrace.TraceVerbose, "begin date binding
formating")

If e.Value Is System.DBNull.Value Then
e.Value = ""
Else
e.Value = CType(e.Value, Date).ToShortDateString.ToString
End If

Trace.WriteLineIf(Globals.ThisTrace.TraceVerbose, "end date binding
formating")

End Sub

Public Shared Sub CurrencyStringtoDate(ByVal sender As Object, ByVal e As
System.windows.forms.ConvertEventArgs)

Trace.WriteLineIf(Globals.ThisTrace.TraceVerbose, "date parsing")

If e.Value.ToString = "" Then
e.Value = DBNull.Value
Else
e.Value = CDate(e.Value)
End If

Trace.WriteLineIf(Globals.ThisTrace.TraceVerbose, "end date parsing")

End Sub
 
I made a simple test to reproduce my issue:

Create a windows application
Create a new dataset, one table, two fields (RowID Int(Auto, -1,-1);
SomeDate Date)
Add a commandbutton and a textbox to form1
Paste in the following form code:

Private m_dsTest As Dataset1

Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnTest.Click

Try

Dim dr As Dataset1.SimpeTableRow
Dim b As Binding

dr = m_dsTest.SimpeTable.NewSimpeTableRow
m_dsTest.SimpeTable.AddSimpeTableRow(dr)

b = New Binding("Text", dr, "SomeDate")
AddHandler b.Parse, AddressOf DBdateTextbox
'DataBindingManager.CurrencyStringtoDate
AddHandler b.Format, AddressOf TextBoxDBdate
'DataBindingManager.DateToCurrencyString
Me.txtDate.DataBindings.Add(b)

Catch ex As System.Reflection.TargetInvocationException
Debug.WriteLine("Boom!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" & vbCrLf &
ex.ToString)
Stop
Catch ex As Exception
Debug.WriteLine(ex.ToString)
End Try

End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
m_dsTest = New Dataset1
End Sub

Private Sub DBdateTextbox(ByVal sender As Object, ByVal cevent As
ConvertEventArgs)
If cevent.Value Is DBNull.Value Then
cevent.Value = ""
Else
Dim datum As Date
datum = CDate(cevent.Value)
cevent.Value = datum.ToString("d")
End If
End Sub
Private Sub TextBoxDBdate(ByVal sender As Object, ByVal cevent As
ConvertEventArgs)
If cevent.Value.ToString = "" Then
cevent.Value = DBNull.Value
Else
Dim datum As Date
datum = CDate(cevent.Value)
Cevent.Value = datum
End If
End Sub

.................. Data set XML.................

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="Dataset1" targetNamespace="http://tempuri.org/Dataset1.xsd"
elementFormDefault="qualified"
attributeFormDefault="qualified" xmlns="http://tempuri.org/Dataset1.xsd"
xmlns:mstns="http://tempuri.org/Dataset1.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Dataset1" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="SimpeTable">
<xs:complexType>
<xs:sequence>
<xs:element name="RowID" type="xs:int" minOccurs="0"
msdata:AutoIncrement="true" msdata:AutoIncrementSeed="-1"
msdata:AutoIncrementStep="-1" />
<xs:element name="SomeDate" type="xs:date" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:key name="Dataset1Key1">
<xs:selector xpath=".//mstns:SimpeTable" />
<xs:field xpath="mstns:RowID" />
</xs:key>
</xs:element>
</xs:schema>
 
Not to nit pick, but I am faced with turning off databinding and doing all
the mapping manually or superclassing the textbox control. I am not eager
to begin this step, so I am scrutinizing your sample looking for
differences.

I think your sample has an inherint error though, you are binding to a
datatable and not the datarow. Is this what you had intended?

'Shouldn't it be this
New Binding("Text", ds.Tables(mytableindex).rows(myrowindex), "mydatfield")

'Instead of
New Binding("Text", ds.Tables(mytableindex), "mydatfield")
 
Back
Top