The EntitySpaces Community

Share and learn about the EntitySpaces Architecture.
Welcome to The EntitySpaces Community Sign in | Join | Help
in
Home Forums Photos

Databind Tutorial

Last post 03-08-2007, 9:09 AM by liffey. 5 replies.
Sort Posts: Previous Next
  •  02-21-2007, 7:57 AM 448

    Databind Tutorial

    This is a request.  I am on a team writing a Window Forms project using databinding and ES.  The combination works great.  The only issue currently is when there are no rows in the collection and I want to add the first.

     What is the best practice for adding the first row?

    Here is my current code:

    On form load::

    Code:
        moCustomers = New HJ.DataAccess.CustomersCollection
        With moCustomers.Query
          .OrderBy( _
            .CustomerName.Ascending _
          )
          .Load()
        End With
    
        Me.CustomersCollectionBindingSource.DataSource = moCustomers

    When the add new button is clicked::

    Code:
    	Me.ContactsCollectionBindingSource.AddNew()

    When the save button is clicked::

    Code:
    	Me.ContactsCollectionBindingSource.EndEdit()
    
    	moContacts.Save()
    

    Here is where I get the error, because there were no rows in the original collection the CustomersCollectionBindingSource doesn't create a bound record and there is nothing to save?  If the collection has at least one record the entire form works just as advertised.  What extra steps should I take when the initial collection is empty?

    Thanks in advance from our whole team!

    Filed under:
  •  02-21-2007, 8:53 AM 452 in reply to 448

    Re: Databind Tutorial

    Ad the "Read Me Before you Post" thread states Wink use the code button when posting, it's the last toolbar button on the right when editing a post, it makes the posted code look terrific.

    Hmmm, you could add a new blank row via AddNew, but to be honest, let's see what others say, we will be doing an exhaustive review of this stuff very soon as 1.6 is all about DataBinding.

    EntitySpaces | Twitter | BLOG | Please honor our Software License
  •  02-21-2007, 9:02 AM 453 in reply to 452

    Re: Databind Tutorial

    Still learning this new forum stuff, I think it will catch on!
  •  02-22-2007, 9:12 PM 496 in reply to 453

    Re: Databind Tutorial

    I'm not sure I understand the relation between CustomersCollectionBindingSource in the Load and ContactsCollectionBindingSource in Add/Save.
    David Neal Parsons
    www.entityspaces.net
  •  03-08-2007, 8:54 AM 883 in reply to 448

    Re: Databind Tutorial

    Glenn,

    Did you solve this problem? I have just run into the same thing and can't fugure why, it was working, or so I thought!

    I have now created a clean test project to try and see what is wrong. It uses a simple table in Nwind -  sql create below.

    Using:

    EntitySpaces Version # 2007.0.0304.0
    MyGeneration Version # 1.2.0.2

    I have also used 1.5.3 and see no difference.

    If the table contains no rows the first row added will be blank. Adding more rows works just fine. The problem seems to be that the current row on the bindingsource is at -1 after the addNew and not at position 0 as it seems to expect. Why might the binding source not reposition to the added row after the AddNew?

    In the interest of simplicity, I have no binding navigator, just a few buttons.

    Declan

    Code:
    1    Imports EntitySpaces.Core
    2    Imports EntitySpaces.Interfaces
    3    
    4    Imports ES_NorthWind.BusinessObjects
    5    
    6    Public Class DataBindingTest
    7    
    8    
    9        Dim collNewCustomers As NewCustomersCollection = New NewCustomersCollection
    10   
    11       Private Sub DataBindingTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    12   
    13           collNewCustomers.LoadAll()
    14           Me.NewCustomersCollectionBindingSource.DataSource = collNewCustomers
    15   
    16       End Sub
    17   
    18       Private Sub addNewItem()
    19           NewCustomersCollectionBindingSource.AddNew()
    20       End Sub
    21   
    22       Private Sub saveItem()
    23   
    24           ShowPosition()
    25   
    26           NewCustomersCollectionBindingSource.EndEdit()
    27           collNewCustomers.Save()
    28       End Sub
    29   
    30       Private Sub deleteItem()
    31   
    32           ShowPosition()
    33   
    34           NewCustomersCollectionBindingSource.RemoveCurrent()
    35           collNewCustomers.Save()
    36       End Sub
    37   
    38       Private Sub ShowPosition()
    39           Console.WriteLine("Count {0}; Position {1}", NewCustomersCollectionBindingSource.CurrencyManager.Count, _
    40                                                NewCustomersCollectionBindingSource.CurrencyManager.Position)
    41   
    42       End Sub
    43   
    44       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    45           Me.NewCustomersCollectionBindingSource.MoveFirst()
    46       End Sub
    47   
    48       Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    49           Me.NewCustomersCollectionBindingSource.MovePrevious()
    50       End Sub
    51   
    52       Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    53           Me.NewCustomersCollectionBindingSource.MoveNext()
    54       End Sub
    55   
    56       Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    57           Me.NewCustomersCollectionBindingSource.MoveLast()
    58       End Sub
    59   
    60       Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
    61           addNewItem()
    62       End Sub
    63   
    64       Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
    65           deleteItem()
    66       End Sub
    67   
    68       Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
    69           saveItem()
    70       End Sub
    71   End Class
    

     

    Code:
    1    USE [NorthWind]
    2    GO
    3    /****** Object:  Table [dbo].[NewCustomers]    Script Date: 03/08/2007 16:52:52 ******/
    4    SET ANSI_NULLS ON
    5    GO
    6    SET QUOTED_IDENTIFIER ON
    7    GO
    8    CREATE TABLE [dbo].[NewCustomers](
    9    	[CustomerID] [int] IDENTITY(1,1) NOT NULL,
    10   	[CompanyName] [nvarchar](40) COLLATE Latin1_General_CI_AI NULL,
    11   	[ContactName] [nvarchar](30) COLLATE Latin1_General_CI_AI NULL,
    12    CONSTRAINT [PK_NewCustomers] PRIMARY KEY CLUSTERED 
    13   (
    14   	[CustomerID] ASC
    15   )WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
    16   ) ON [PRIMARY]
    

     

  •  03-08-2007, 9:09 AM 885 in reply to 883

    Re: Databind Tutorial

    Why does this always happen just after I post a cry for help? Embarrassed I guess I need a site of my own somewhere where I can blow off steam for a while.

    I found a solution, though I am surprised I need this.

    Adding ResetBindings immediately after the AddNew seems to solve the problem

    Declan

    Code:
    1        Private Sub addNewItem()
    2            NewCustomersCollectionBindingSource.AddNew()
    3    
    4            NewCustomersCollectionBindingSource.ResetBindings(False)
    5    
    6        End Sub
    
View as RSS news feed in XML