DataBinding to a SQL Field of type money

Has anyone ever had a problem DataBinding to this field type   I have a method that sets up all my DataBindings for my controls on the form and when I run it I get this error: "DataBinding could not find a row in the list that is suitable for all bindings"

When I comment out the DataBindings for the fields that are pulling from fields of type money, it works just fine.

Is this a bug or is it possible that I did something wrong


Answer this question

DataBinding to a SQL Field of type money

  • bday55

    Erik,

    I have seen that message before if the table is empty or if a particular field does not have a default value. Set the default value to $0.00.

    Jason

  • TeddyCineas

    I've never actually used binding to controls directly until a few days ago, so I'm sure I'm totally clueless or something (although I've used databinding a lot in other scenarios).

    Check out my other post complaining about this error that I posted yesterday: <a href="http://www.windowsforms.net/Forums/ShowPost.aspx tabIndex=1&tabId=41&PostID=1071">BindingManagerBase.ResumeBinding()</a>

    The error is totally lame if you ask me...doesn't tell me anything...I just had to start commenting out lines until I found that it was all the money fields that were giving the error!

    I guess I'll hit up google now  :( 

  • lwrich

    yup, i already went down that checklist...you'll notice the line that says MessageBox.Show(Data.Tables("tblBusiness").Rows(0).Item("StorageInOut"))  that displays "0"
  • GDogius

    awesome, thanx a lot, i will try that and let you know...thanx again!  :) 
  • Dav? S. A???????

    ok, unfortunately, giving the field a default value didn't work  :'( 

    so now this is where I get desperate and start pasting code like a mad man!


    'add the bindings to a control
    Private Sub AddFieldBinding(ByRef Control As Control, ByVal PropertyName As String, ByVal DataTableName As String, ByVal FieldName As String, ByVal AddHandlers As Boolean)
    Dim Binding As Binding = Control.DataBindings.Add(PropertyName, Data, DataTableName & "." & FieldName)
    If AddHandlers Then
    With Binding
    AddHandler .Parse, AddressOf BindingParser
    AddHandler .Format, AddressOf BindingFormatter
    End With
    End If
    End Sub

    'remove the bindings from a control
    Private Sub RemoveFieldBinding(ByRef Control As Control, ByVal PropertyName As String, ByVal RemoveHandlers As Boolean)
    If Not Control.DataBindings(PropertyName) Is Nothing Then
    If RemoveHandlers Then
    With Control.DataBindings(PropertyName)
    RemoveHandler .Parse, AddressOf BindingParser
    RemoveHandler .Format, AddressOf BindingFormatter
    End With
    End If
    Control.DataBindings.Remove(Control.DataBindings.Item(PropertyName))
    End If
    End Sub

    'Format Event Handler
    Private Sub BindingFormatter(ByVal sender As Object, ByVal e As ConvertEventArgs)
    If e.DesiredType Is GetType(Boolean) Then
    e.Value = Utilities.Global.NullToBoolean(e.Value)
    End If
    If e.DesiredType Is GetType(String) Then
    e.Value = Utilities.Global.NullToEmpty(e.Value)
    End If
    If e.DesiredType Is GetType(Decimal) Then
    e.Value = CType(e.Value, Decimal).ToString("c")
    End If
    End Sub

    'Parse Event Handler
    Private Sub BindingParser(ByVal sender As Object, ByVal e As ConvertEventArgs)
    If e.DesiredType Is GetType(String) AndAlso e.Value = "" Then
    e.Value = Utilities.Global.EmptyToNull(e.Value)
    End If
    If e.DesiredType Is GetType(Decimal) Then
    e.Value = Decimal.Parse(e.Value.ToString, Globalization.NumberStyles.Currency)
    End If
    End Sub

    'this is the sub that gives the error on the line where the try catch is
    'and it's because of the 4 lines after the 'error comment line
    'the field types for all 4 are of sql type money
    Private Sub SetupFieldBindings()
    Me.BindingContext(Data, "tblBusiness").SuspendBinding()
    AddFieldBinding(txtSolomonBusinessId, "Text", "tblBusiness", "SolomonBusinessId", False)
    AddFieldBinding(txtBusinessName, "Text", "tblBusiness", "BusinessName", True)
    AddFieldBinding(cboBusinessType, "SelectedItem", "tblBusiness", "BusinessType", True)
    AddFieldBinding(cboBusinessStatus, "SelectedItem", "tblBusiness", "BusinessStatus", True)
    AddFieldBinding(chkCustomerFlag, "Checked", "tblBusiness", "CustomerFlag", True)
    AddFieldBinding(chkCarrierFlag, "Checked", "tblBusiness", "CarrierFlag", True)
    AddFieldBinding(chkWarehouseFlag, "Checked", "tblBusiness", "WarehouseFlag", True)
    AddFieldBinding(chkVendorFlag, "Checked", "tblBusiness", "VendorFlag", True)
    AddFieldBinding(txtComments, "Text", "tblBusiness", "Comments", True)
    AddFieldBinding(chkFirstMonthStorageFlag, "Checked", "tblBusiness", "FirstMonthStorageFlag", True)

    'error
    MessageBox.Show(Data.Tables("tblBusiness").Rows(0).Item("StorageInOut"))
    AddFieldBinding(txtStorageInOut, "Text", "tblBusiness", "StorageInOut", True)
    AddFieldBinding(txtStorageTransfer, "Text", "tblBusiness", "StorageTransfer", True)
    AddFieldBinding(txtStorageMonthlyCoil, "Text", "tblBusiness", "StorageMonthlyCoil", True)
    AddFieldBinding(txtStorageFinishedGoods, "Text", "tblBusiness", "StorageFinishedGoods", True)

    AddFieldBinding(txtMailingAddress1, "Text", "tblBusiness", "MailingAddress1", True)
    AddFieldBinding(txtMailingAddress2, "Text", "tblBusiness", "MailingAddress2", True)
    AddFieldBinding(txtMailingCity, "Text", "tblBusiness", "MailingCity", True)
    AddFieldBinding(txtMailingState, "Text", "tblBusiness", "MailingState", True)
    AddFieldBinding(txtMailingCountry, "Text", "tblBusiness", "MailingCountry", True)
    AddFieldBinding(txtMailingPhoneNumber, "Text", "tblBusiness", "MailingPhoneNumber", True)
    AddFieldBinding(txtMailingExtension, "Text", "tblBusiness", "MailingExtension", True)
    AddFieldBinding(txtMailingFaxNumber, "Text", "tblBusiness", "MailingFaxNumber", True)
    AddFieldBinding(txtMailingWebsite, "Text", "tblBusiness", "MailingWebsite", True)

    'terms

    AddFieldBinding(txtPhysicalAddress1, "Text", "tblBusiness", "PhysicalAddress1", True)
    AddFieldBinding(txtPhysicalAddress2, "Text", "tblBusiness", "PhysicalAddress2", True)
    AddFieldBinding(txtPhysicalCity, "Text", "tblBusiness", "PhysicalCity", True)
    AddFieldBinding(txtPhysicalState, "Text", "tblBusiness", "PhysicalState", True)
    AddFieldBinding(txtPhysicalCountry, "Text", "tblBusiness", "PhysicalCountry", True)
    AddFieldBinding(txtPhysicalPhone, "Text", "tblBusiness", "PhysicalPhone", True)
    AddFieldBinding(txtPhysicalFaxNumber, "Text", "tblBusiness", "PhysicalFaxNumber", True)
    Try
    Me.BindingContext(Data, "tblBusiness").ResumeBinding()
    Catch Ex As Exception
    Utilities.Prompt.ShowError(Ex.ToString)
    End Try
    End Sub

  • Mohit Gupta

    yes there are, but that's why I'm using the SuspendBinding and ResumeBinding.  Nulls are working just fine in other field types actually, it's just those money fields that are having a hissy fit.

    I sort of understand the problem, because a money field is translated to the native Decimal type and Decimals can not ever be Nothing, where as a String can, but even with values in those money fields, the Binding still doesn't work  :(  And as I mentioned before, the values that are coming in for the money fields are all "0"

     :@  grrrr

  • seanslyvinia

    Are any of the money fields null  Check that first. Also, make sure that you are returning records before the bind actually takes place.

    Jason

  • SJS19859

    So all money fields contain at least 0  That is strange then. Did you try searching Google  What is the exact error  Did you try using a Try..Catch block to see what the error really is

    Jason

  • Jimdbc-LLC

    Are there any null values in your recordset
  • Fernando Celarino

    No problem Erik! I'm trying to help out more in this forum!  ;) 
  • DataBinding to a SQL Field of type money