none
Not sinking in. RRS feed

  • Question

  • I can get the controls populated with the Business Entity, but I don't see how to bind it to the controls.  I guess I just am not understanding what to do next!  Simply setting the control's .text property to the bo Object doesn't make sense, since I'd like to be able to add an UPDATE method in the BuinessEntity (via the dataSet that gets submited back to the DAL).

    One thing at a time I guesss. 

    Can you show me how to bind Me.txtOwner to the objLocation object?

    UI:
    Private Sub frmStormsMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
     
    Dim stormsLocations As New StormsBLC.objLocations(s_ParcelID)  
            loadCntrlsSTORMS(stormsLocations)  
            stormsLocations = Nothing 
     
        End Sub  
     
    Private Sub loadCntrlsSTORMS(ByVal bo As StormsBLC.objLocations)  
     
            Me.txtOwner.Text = bo.OwnerName  
    End Sub  
     

    BusinessEntity:
    Public Class objLocations  
     
    Private dt As DataTable  
    Private theRow As Data.DataRow  
    Private rowNumber As Integer  
    Private OwnerNameValue As String  
     
    Public Sub New(ByVal parcelID As String)  
            MyBase.New()  
            Me.GetLocationData(parcelID)  
            ParcelIDValue = parcelID 
        End Sub  
     
    Public Sub New(ByVal OwnerName As String)  
     
    OwnerNameOwnerNameValue = OwnerName  
     
    End sub  
     
    Public Property OwnerName() As String  
            Get  
                OwnerNameValue = theRow.Item("OwnerName")  
                Return OwnerNameValue  
            End Get  
            Set(ByVal value As String)  
                OwnerNameValue = value 
            End Set  
        End Property  
     
    Private Function GetLocationData(ByVal parcelID As String) As DataSet  
            Dim stormsDAL As New StormsDAL.locationsDALC(GetStormsConnectionString)  
            Dim DSstormsLocations As DataSet = stormsDAL.GetLocations(parcelID)  
            dt = DSstormsLocations.Tables("stormsLOCATIONS")  
            rowNumber = 0 
            theRow = dt.Rows.Item(rowNumber)  
        End Function  
     
     

    DataAccessEntity:
    Public Class locationsDALC  
     
    Public Sub New(ByVal cnnString As String)  
     
            MyBase.New()  
            Me.openConnection(cnnString)  
     
        End Sub  
        
    Private Sub openConnection(ByVal cnnString As String)  
            If sqlCn.State <> ConnectionState.Open Then  
                Try  
                    If Me.sqlCn Is Nothing Then  
                        Me.sqlCn = New SqlConnection  
                    End If  
                    sqlCn.ConnectionString = cnnString 
                    sqlCn.Open()  
                Catch sqlEx As SqlException  
                    Me._LastException = sqlEx 
                Catch ex As Exception  
                    MsgBox(ex.ToString)  
                End Try  
     
            End If  
        End Sub  
        Private Sub CloseConnection()  
            If sqlCn.State = ConnectionState.Open Then  
                sqlCn.Close()  
            End If  
            Me.Dispose()  
        End Sub  
     
        Public Function GetLocations(ByVal m_parcelID As String) As DataSet  
     
     
            Dim cmdText, s_paramName As String  
            cmdText = "Storms_DALC_GetLocations" 
            s_paramName = "@parcelID" 
     
            Dim sp_Cmd As SqlCommand = New SqlCommand()  
            sp_Cmd.CommandText = cmdText 
            sp_Cmd.CommandType = CommandType.StoredProcedure  
     
            'add the input parameter and set its properties  
            Dim param As New SqlParameter()  
            param.ParameterName = s_paramName 
            param.SqlDbType = SqlDbType.NVarChar  
            param.Direction = ParameterDirection.Input  
            param.Value = m_parcelID 
            sp_Cmd.Parameters.Add(param)  
     
            Dim ds As New DataSet()  
            Dim da As New SqlDataAdapter  
     
            Try  
                Using sp_Cmd  
                    sp_Cmd.Connection = Me.sqlCn  
                    da.SelectCommand = sp_Cmd 
                    da.Fill(ds, "stormsLOCATIONS")  
                    m_StormsDataAdapter = da  
                    da.Dispose()  
     
                End Using  
                Me.CloseConnection()  
                Return ds  
     
            Catch sqlEx As SqlException  
                Return Nothing  
            Catch ex As Exception  
                Return Nothing  
     
            End Try  
        End Function 
     
    jfc
    Tuesday, January 6, 2009 5:31 PM

Answers

  • You can add binding at runtime (my perferred method) using the following:

    Me.TextBox1.DataBindings.Add (New Binding("Text",myObject,"MyProperty"))

    This will bind the MyProperty of MyObject to the Text property of TextBox1.  Note that the property names are passed as string, while the object being bound to is passed as itself.

    So if you want to bind me.txtOwner to the OwnerName of the location object, it would look like this:

    Me.txtOwner.DataBindings.Add(New Binding("Text", bo, "OwnerName"))

    hope this helps.

    -tg

    • Marked as answer by jamesfreddyc Tuesday, January 6, 2009 6:47 PM
    Tuesday, January 6, 2009 6:35 PM

All replies

  • Hi James.  Have you tried adding the object class as a data source to your project.  Try using the "Add New Data Source" option under the Data menu option in Visual Studio.  Choose "Object" for the source type and then you'll be presented with a list of libraries from your project references and from within your project.  If the class is in a different library then you'll need to add a reference to its compiled dll (this can be done from a button on the screen we left off at above -- or through the project properties > References tab).  Once you have selected a class from the treeview provided in the wizard click Finish and now you'll have a data source added.

    If you go to your form and view the DataSources window (you can select it from Data > Show Data Sources) you can expand the treeview of the class you just added.  Each of the properties should be represented.  there is a drop down next to the property names that allows you to configure the type of control used by that property.  Now just drag one of the property items on to your form and you shoudl see a control generated.  Also you should see a strongly-typed binding source object generated in your form designer's component tray.  The control will be bound to that binding source.  You may need to set the dta source for the binding source manually in your Load event, but you have that covered in the code already presented -- in teh form of your "bo" object.
    Tuesday, January 6, 2009 6:02 PM
  • You can add binding at runtime (my perferred method) using the following:

    Me.TextBox1.DataBindings.Add (New Binding("Text",myObject,"MyProperty"))

    This will bind the MyProperty of MyObject to the Text property of TextBox1.  Note that the property names are passed as string, while the object being bound to is passed as itself.

    So if you want to bind me.txtOwner to the OwnerName of the location object, it would look like this:

    Me.txtOwner.DataBindings.Add(New Binding("Text", bo, "OwnerName"))

    hope this helps.

    -tg

    • Marked as answer by jamesfreddyc Tuesday, January 6, 2009 6:47 PM
    Tuesday, January 6, 2009 6:35 PM
  •  Ah ha!

    That's what I was attempting to do originally, except I was missing the "New Binding" portion.  I had it like this:

    Me.txtOwner.DataBindings.Add("Text", bo, bo.OwnerName)

    We'll see if that works.

     

     

    Also: Dig-Boy...  Thanks for the tip.  Not sure if it's appropriate since I already have the form built with all of the controls established.  But maybe I can just drag and drop the object items right onto the appropriate control.

     

    Thanks!

     

    j


    jfc
    Tuesday, January 6, 2009 6:44 PM
  • Ok, that seems to work (well, it shows the form with the txtOwner control filled correctly!).

    Does this mean that any changes made to the textOwner.Text, it automatically updates the objLocations object?

    Also: this objLocations object should have an Update method to send to the DAL.  Is this somewhat correct?

    Public Class objLocations  
     
    Private OwnerNameValue As String  
    Private locationsDataSet As DataSet  
     
    Public Sub New(ByVal parcelID As String)  
            MyBase.New()  
            Me.GetLocationData(parcelID)  
            ParcelIDValue = parcelID 
        End Sub  
     
     Public Sub New()  
     
            OwnerNameValue = OwnerName  
    End Sub  
     
    Private Function GetLocationData(ByVal parcelID As String)  
            Dim stormsDAL As New StormsDAL.locationsDALC(GetStormsConnectionString)  
            Dim DSstormsLocations As DataSet = stormsDAL.GetLocations(parcelID)  
            dt = DSstormsLocations.Tables("stormsLOCATIONS")  
            rowNumber = 0 
            theRow = dt.Rows.Item(rowNumber)  
            locationsDataSet = DSstormsLocations 
            Return locationsDataSet  
        End Function  
     
    Public Property OwnerName() As String  
            Get  
                OwnerNameValue = theRow.Item("OwnerName")  
                Return OwnerNameValue  
            End Get  
            Set(ByVal value As String)  
                OwnerNameValue = value 
            End Set  
        End Property  
     
    Public Sub UpdateLocations()  
            Dim stormsDAL As New StormsDAL.locationsDALC(GetStormsConnectionString)  
            stormsDAL.UpdateStormsLocationsDS(locationsDataSet)  
     
        End Sub 

    jfc
    Tuesday, January 6, 2009 7:10 PM
  • >>Does this mean that any changes made to the textOwner.Text, it automatically updates the objLocations object?

    Yes and no.... it will ONCE focus has left and the Validation event has fired.... I found this out the hard way. Had something that wasn't saving right. Once I tracked it through, it was because focus was still inside the textbox (it turns out Toolbar buttons don't actually get focus when you click on them??? At least that was the case I was running into. I don't remember how I got around it, but it wasn't very clever or elegant at the time (this was back in the FW1.1 days.) -- It's possible things have changed with 2.0/3.0/3.5 but haven't had the opportunity to find out....I think I'll make time tonight.

    -tg
    Tuesday, January 6, 2009 7:36 PM
  • >>>It's possible things have changed with 2.0/3.0/3.5 but haven't had the opportunity to find out....I think I'll make time tonight


    tg,

    Thanks for your input.  Please comment if you ever get the chance to look into it. 
    jfc
    Tuesday, January 6, 2009 10:31 PM