Results 1 to 7 of 7

Thread: Problem with BindingSource (Unsolved)

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    5

    Problem with BindingSource (Unsolved)

    Hi, I'm trying to migrate from vb6 to vb .net but I have this little problem that doesn't let me keep on working. Do you remember that in vb6 with the DAO control you could reference a field by writing:

    dim Customer as string

    customer = Data1.recordset.Fields("Custromer_Name")

    and you could also modify the field's value by writing

    data1.recordset.edit
    data.recordset.fields("Customer_Name") = "Juan Lopez"
    data1.recordset.update

    How do I reference a field with BindingSource???

    Thanks for your help.
    Last edited by yigabait; Jun 22nd, 2006 at 11:58 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Problem with BindingSource

    If your BindingSource is bound to a DataTable then its items are DataRowView objects that correspond to the DataRows in the DataTable. Because a BindingSource can be bound to all sorts of different objects the items are returned as Object references, so it's up to you to cast them as the appropriate type. Let's say that you have data in a grid bound to a BindingSource that is in turn bound to a DataTable. If you want to set the Customer_Name field of the row that is currently selected in the grid you could do this:
    VB Code:
    1. Dim currentRow As DataRowView = TryCast(myBindingSource.Current, DataRowView)
    2.  
    3. If currentRow IsNot Nothing Then
    4.     'There is a row currently selected.
    5.     currentRow("Customer_Name") = "Juan Lopez"
    6. End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    5

    Re: Problem with BindingSource

    ok. So is not as simple as data1.recordset.fields("Customer_Name"), is it?

    And supose that I only want to show a messagebox with the field ("Customer_Name")

    In vb6, I would do it like this:

    msgbox (data1.recordset.fields("Customer_Name"))

    but in VB .net???

    Thanks for your help.
    Last edited by yigabait; Jun 22nd, 2006 at 09:48 PM.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Problem with BindingSource

    Have a look at this:
    VB Code:
    1. [B][U]data.recordset.fields("Customer_Name")[/U][/B] = "Juan Lopez"
    and now have a look at this:
    VB Code:
    1. msgbox ([B][U]data1.recordset.fields("Customer_Name")[/U][/B])
    You see the commonality? Now take a look at this:
    VB Code:
    1. [B][U]currentRow("Customer_Name")[/U][/B] = "Juan Lopez"
    How do you suppose you would display that field?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    5

    Unhappy Re: Problem with BindingSource

    Thank you so much for your help but I have another problem

    VB Code:
    1. BindingSource1.Current("Customer_Name") = "Juan Perez"
    2.         Me.Validate()
    3.         BindingSource1.EndEdit()
    4.         customerTableAdapter.Update(Me.CuentasDataSet.Customers)

    What's wrong? An error message appears: "Update requires that UpdateCommand is valid when it passes to collection DataRowwith modified rows" (or something similar)

    What should I do??

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Problem with BindingSource (Unsolved)

    Your TableAdapter doesn't have a valid UpdateCommand so you need to make sure it has one. I don't use TableAdapters so I don't know all the details of how they work. They are similar to DataAdapters but they are not the same. This is one of the problems with letting the IDE write a lot of your code for you. If something goes wrong you don't know how to fix it. Even if you didn't have an issue I'd suggest reading up on TableAdapters anyway. Here's a good place to start: http://msdn.microsoft.com/library/de...leadapters.asp
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    New Member
    Join Date
    Oct 2009
    Posts
    1

    Re: Problem with BindingSource

    Here it is

    BindingSource.Current("Customer_Name") = "Juan Perez"
    Me.Validate()
    BindingSource.EndEdit()
    Me.TableAdapterManager.UpdateAll(Me.DataSet)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width