Page 1 of 2 12 LastLast
Results 1 to 40 of 57

Thread: [RESOLVED] refresh combobox values after updating its datatable and adapter

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Location
    Burlington, ON, Canada
    Posts
    343

    Resolved [RESOLVED] refresh combobox values after updating its datatable and adapter

    hi all - on a windows form i have a combobox called currency, it is populated by a tableadapter called exchangerate which is turn populated by a dataset called mydataset.

    on form load, i issue update commands to the backend database that holds the table which populates the dataset - they work. if i look at the update to the combobox, they are not reflected until i exit and restart the app - then they're fine.

    further down the formload i've tried

    Code:
    Me.exchangerate.Update(Me.DataSet.Currency)
    Me.CurrencyComboBox.Refresh()
    am i missing something silly to have the values in the combobox change and refresh at the time of the update to the database? as i said, an exit and restart of the app displays the accurate data....i can't figure this out for the life of me.

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

    Re: refresh combobox values after updating its datatable and adapter

    I keep seeing this sort of question over and over. All the Refresh method of any control does is redraw it on-screen. It doesn't do anything else. If the binding hasn't been updated then redrawing it will have no effect.

    I would have thought that if your data was bound to the ComboBox then the control should update automatically. If it's not then you can call the ResetBindings method of the associated BindingSource.
    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
    Hyperactive Member
    Join Date
    Mar 2008
    Location
    Burlington, ON, Canada
    Posts
    343

    Re: refresh combobox values after updating its datatable and adapter

    i tried both

    Code:
    Me.exchangerate.Update(Me.DataSet.Currency)
    me.currencybindingsource.resetbindings(false)
    and

    Code:
    Me.exchangerate.Update(Me.DataSet.Currency)
    me.currencybindingsource.resetbindings(true)
    wondering if it was a schema change (wanted to be sure) - and same thing - on the opening round, the selected value of my combobox (what i'm tracking on) - is the old value on form load - exit the app, go back in, and the selected value is what it's supposed to be.....

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Location
    Burlington, ON, Canada
    Posts
    343

    Re: refresh combobox values after updating its datatable and adapter

    oh and this is a sqlexpress 2005 mdf that has the table with the data in it if that makes any difference...

    the combobox properties being used are data source, display member and valuemember - I'm still very green at this but also noticed a plus sign entitled 'databindings' above combobox name, but all are blank - will begin researching what those are all about next....

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

    Re: refresh combobox values after updating its datatable and adapter

    You wouldn't use the DataBindings property at all. That's for bind specific properties of the ComboBox to values from a list or an object. That is used more in situations like binding the Text property of a TextBox.

    For a ComboBox you would use, as you said, the DataSource, DisplayMember and ValueMember. The DataSource is the list containing the data to bind. The DisplayMember is the name of the member of the bound objects to display in the control. The ValueMember is the name of the member of the bound objects whose value should be returned by the control's SelectedValue property. A common scenario is binding a DataTable via the DataSource and setting the DisplayMember and ValueMember to "Name" and "ID" respectively. That way the user sees a list of names they can choose and you can get the ID that corresponds to the selected name from the SelectedValue property.

    Now, in your situation, you should be binding your DataTable to a BindingSource and then binding that to the ComboBox. It can be done in code or, if you added the DataSet in the designer, then it can also be done in the designer. You can either assign the DataTable to the BindingSource's DataSource property or else assign a DataSet to the DataSource and then the name of the DataTable to the DataMember. The BindingSource then gets assigned to the ComboBox's DataSource. If it's set up that way then any changes you make to the DataTable will be reflected in the ComboBox.

    Now, reading your posts a bit more carefully, there's no reason that calling Update on a TableAdapter should cause the selection in the ComboBox to change. Update will save the changes in the DataTable to the database but those changes should already be reflected in the ComboBox because they're already in the DataTable.

    I think you need to explain more clearly exactly what you're trying to do, how you're trying to do it, what you expect to happen and what does happen.
    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

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Location
    Burlington, ON, Canada
    Posts
    343

    Re: refresh combobox values after updating its datatable and adapter

    thanks for the databindings heads up - you saved me several hours of googling I'm sure.....

    the short version (or at least as short as I can make it, so forgive the excessive verbiage) is as follows:

    - created a sql2005 express database with a table called currency in it - fields are Currency, Rate, As_of

    - i call a public function named getexchangerate() that hits yahoo finance and grabs the exchange rate .csv file for that particular currency and time of day and parses the values in to their respective pieces that correspond with the above fields in currency

    - i fire up a connection to the database and call the transact-sql update command using the values retrieved above - the table updates with the values successfully

    - i launch form1 which contains a combobx entitled Exchange_CB - it's display value is Currency, It's value item is Rate

    - also, as a sidebar....i have a string i pass from the getexchangerate function that updates a label that contains all of the retrieved values - this also works correctly on form load (and is how i discovered the update wasn't happening on the comboboxes selectedvalue)

    - when i dropdown and change the combobox, in this case from CDN to USD, the previous valuemember is displayed (ie from the last successful download) - i exit the app and go back in, and change it again and it's correct. Logic seems to tell me that my call to refresh the data is happening before the adapter is filled, but of course i don't know at this point and hence this posting....

    - The combobox was created simply by dragging it on to form1 from the toolbox and then in properties i added it's datasource and display member and value member from there - not programatically....

    for the record, i am far from an expert and have been at this for the last whopping 6 months now - you actually were instrumental in resolving my very first problem and several others by the way.....

    - oh and lastly, the "why" i'm doing it this way - I'm doing this because the machine that this app is running on is sometimes online and sometimes not and the exchange rate is used in a calc for quotes, hence i want both the end user to actually see what the exchange rate WAS before they went offline or to see what it IS if they are, and also to store it.

    I can equate this issue to the following example: "on Form A a user has a combobox called customers and a button called add customer. The Add customer button launches new form that allows the user to enter a new customer name and click save. When the user hits save and form B closes and the focus goes back to form A and they drop down the combobox, the newly added customer does not show up, yet when checking the table, it is there. if they exit the app and restart the previously added customer now shows up in the combobox list." - so ideally, how do you get the combobox to reflect the newly added record without exiting the app?

    - again, just an example above - but those are my symptoms as well.....
    Last edited by trevorjeaton; Jul 9th, 2008 at 10:19 AM.

  7. #7
    New Member
    Join Date
    Jul 2008
    Posts
    2

    Re: refresh combobox values after updating its datatable and adapter

    You may have figured this out already but I thought I would post this to help someone else that may be having this issue. If you swap out my arraylist code with your tableadapter I believe it will work.

    Dim arlItems As ArrayList = combobox.DataSource
    arlItems.Add(New clsCombo(nNextID, "New", "", "", "", ""))

    combobox.BindingContext(combobox.DataSource).SuspendBinding()
    combobox.DataSource = arlitems
    combobox.BindingContext(combobox.DataSource).ResumeBinding()

    'Set the new item as the selected item
    combobox.SelectedValue = nNextID.ToString

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: refresh combobox values after updating its datatable and adapter

    can' you just reset the .datasource?

    combobox.DataSource = ?datasource

  9. #9
    New Member
    Join Date
    Jul 2008
    Posts
    2

    Re: refresh combobox values after updating its datatable and adapter

    I thought the same thing at first but when you just try to set the datasource again it will not show any new items that you have added in the combobox.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Location
    Burlington, ON, Canada
    Posts
    343

    Re: refresh combobox values after updating its datatable and adapter

    got it - simply use the invalidate method - works great
    Code:
    'refresh the datasource
    combobox.datasource=?datasource
    'select the new field from the table to populate the combobox with
    combobox.displaymember = "New Display Member"
    'redraw the combobox to reflect the values
    combobox.invalidate()
    what a pain this one turned out to be for something so simple.....that's usually the way though - too close to the problem to see it clearly - as you can see by the date on this latest post, i stepped away from this one for quite a while.

    hope this helps anybody else who has stumbled upon the same issue.

  11. #11
    New Member
    Join Date
    Jun 2009
    Posts
    1

    Smile Re: [RESOLVED] refresh combobox values after updating its datatable and adapter

    Hey people its very simple what u have to do is
    Form1_Load(Nothing, Nothing)



    ie make a call to form load method which reloads everything


    samething works in c# too

    belive me it works gr8 na

  12. #12
    New Member
    Join Date
    Mar 2012
    Posts
    2

    Re: [RESOLVED] refresh combobox values after updating its datatable and adapter

    I think you guys are missing the big picture. There is a difference between a dataset and the database. A dataset is a copy of the data in the database. If you update the data in the Database's datatable you have to reload the data into the dataset datatable. Also, if you update the data in a dataset, you have to save it using the update method. Otherwise you are only changing the copy of the data, not the data. When you reload the dataset, the data you added will be missing.

    This code obviously doesn't work for every scenario, just keep in mind that you have to reload the data from the table before you can see it.
    Me.MachinesTableAdapter.InsertMachineName(tbxMachineName.Text)
    Me.MachinesBindingSource.EndEdit()
    Me.MachinesTableAdapter.Update(Me.FlowMetersDataSet.Machines)
    Me.MachinesTableAdapter.Fill(Me.FlowMetersDataSet.Machines)


    'Returning From a dialog
    Dim Result As DialogResult
    Result = dlgNewMachine.ShowDialog()
    If Result = DialogResult.OK Then Me.MachinesTableAdapter.Fill(Me.FlowMetersDataSet.Machines)
    Last edited by TrippingCobras; Mar 6th, 2012 at 07:14 PM.

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [RESOLVED] refresh combobox values after updating its datatable and adapter

    Quote Originally Posted by TrippingCobras View Post
    There is a difference between a dataset and the datatable. A dataset is a copy of the data in the datatable.
    a dataset is a collection of datatables + if you change the datatable it changes

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

    Re: [RESOLVED] refresh combobox values after updating its datatable and adapter

    Quote Originally Posted by TrippingCobras View Post
    I think you guys are missing the big picture. There is a difference between a dataset and the datatable.
    There's a difference alright, but not the one you seem to think there is. A DataSet is essentially an in-memory representation of a database. A DataTable is basically an in-memory representation of a database table. Just as a database contains tables and relations between them, so a DataSet contains DataTables and DataRelations. A DataSet has a Tables property, which is a collection of DataTables, and a Relations property, which is a collection of DataRelations.

    When you call Fill on a DataAdapter or TableAdapter you are populating a DataTable. Even if you pass a DataSet as an argument, you are populating one of the DataTables in its Tables collection. When you call Update on a DataAdapter or TableAdapter you are saving changes from a DataTable. Even if you pass a DataSet as an argument, you are saving changes from one of the DataTables in its Tables collection.

    Just as a database table has columns to describe the data and rows to contain it, so a DataTable has a Columns property, which is a collection of DataColumns, and a Rows property, which is a collection of DataRows.
    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

  15. #15
    New Member
    Join Date
    Mar 2012
    Posts
    2

    Re: [RESOLVED] refresh combobox values after updating its datatable and adapter

    Quote Originally Posted by jmcilhinney View Post
    There's a difference alright, but not the one you seem to think there is. A DataSet is essentially an in-memory representation of a database. A DataTable is basically an in-memory representation of a database table. Just as a database contains tables and relations between them, so a DataSet contains DataTables and DataRelations. A DataSet has a Tables property, which is a collection of DataTables, and a Relations property, which is a collection of DataRelations.

    When you call Fill on a DataAdapter or TableAdapter you are populating a DataTable. Even if you pass a DataSet as an argument, you are populating one of the DataTables in its Tables collection. When you call Update on a DataAdapter or TableAdapter you are saving changes from a DataTable. Even if you pass a DataSet as an argument, you are saving changes from one of the DataTables in its Tables collection.

    Just as a database table has columns to describe the data and rows to contain it, so a DataTable has a Columns property, which is a collection of DataColumns, and a Rows property, which is a collection of DataRows.
    I meant database smart guy. You can manipulate the dataset without committing the changes to the actual database. Many people don't understand this and find themselves wondering why all of their data dissapeared. And Paul apparently hasn't figure this out either.

    Chances are the people who originally asked the question are using table adapters (as in ADO.Net). People make the mistake of adding the data to the table adapter, then can't see it on the dataset because they didn't perform a fill method. Another thing that happens is that someone changes the dataset, then doesn't write the the information back to the database.

    Why do I bother with you guys? The post was never really properly addressed, but you guys are sure to jump on someone adding input.

  16. #16
    Hyperactive Member
    Join Date
    Nov 2013
    Posts
    292

    Re: [RESOLVED] refresh combobox values after updating its datatable and adapter

    ....
    Last edited by larrycav; Nov 10th, 2014 at 08:44 PM.

  17. #17
    Hyperactive Member
    Join Date
    Nov 2013
    Posts
    292

    Re: [RESOLVED] refresh combobox values after updating its datatable and adapter

    Quote Originally Posted by omkarlawande View Post
    Hey people its very simple what u have to do is
    Form1_Load(Nothing, Nothing)



    ie make a call to form load method which reloads everything


    samething works in c# too

    belive me it works gr8 na

    I have the same problem with a combobox not reflecting data changes to the database. I have beat myself silly with this problem on and off for 3 days... I tired the solution that fixed the OP's issues but my VB2010 doesn't like that code. Please explain your solution to me...

  18. #18
    Hyperactive Member
    Join Date
    Nov 2013
    Posts
    292

    Re: refresh combobox values after updating its datatable and adapter

    ....
    Last edited by larrycav; Nov 10th, 2014 at 08:44 PM.

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

    Re: refresh combobox values after updating its datatable and adapter

    Quote Originally Posted by larrycav View Post
    I have the same problem with a combobox not showing new data unless I exit the debugger and run it again. I have checked, double checked, deleted, recreated the binding sources....all to no avail. I tried the code supplied here but my VB2010 choked on this line

    combobox.datasource=?datasource

    Also followed jmcilhinney's advice to the letter...it didn't fix my problem either..

    Help appreciated greatly...
    Firstly, please don't post the same thing three times.

    Secondly, if you had the same problem then the same solution would have worked. Obviously there is something different about your situation so please describe your situation FULLY and CLEARLY and then we can address it specifically.
    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

  20. #20
    Hyperactive Member
    Join Date
    Nov 2013
    Posts
    292

    Re: refresh combobox values after updating its datatable and adapter

    Quote Originally Posted by jmcilhinney View Post
    Firstly, please don't post the same thing three times.

    Secondly, if you had the same problem then the same solution would have worked. Obviously there is something different about your situation so please describe your situation FULLY and CLEARLY and then we can address it specifically.
    Yea..well the forum kept hanging when I was trying to post.... sat there for several minutes doing nothing... so I closed Chrome, went back and reposted... so that's why it tripple posted...

  21. #21
    Hyperactive Member
    Join Date
    Nov 2013
    Posts
    292

    Re: refresh combobox values after updating its datatable and adapter

    Situation: 2 forms , Access 2003 database
    Combobox on form 1 not showing changed or new records unless I exit debugger and then run it again.

    form2 contains datagrid, dataset, tableadapter, binding source, load code, save code, add record code

    [Binding source]
    Datasource = ProFlow1DataSet
    Datamember = orificedata

    [Datagrid]
    Bindingsource = Proflow1DatSetBindingSource

    Load code
    Code:
     Me.OrificedataTableAdapter.Fill(Me.ProFlow1DataSet.orificedata)
    Save code
    Code:
     Me.OrificedataTableAdapter.Update(ProFlow1DataSet.orificedata)
    If I edit data in the datagrid and click save, the datagrid show the changed value.

    BUT if I add a new item to the datagrid I notice the ID [primary key in the .mdb table] shows a negative number rather than a sequential positive number after I click the Add button.

    If I close that form and reopen it, the ID number is then sequential as it should be. I have a feeling this may be tied in with the main problem of the combobox on form1 now showing updated values. Here's the code that adds a new record to the datagrid

    Code:
    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ' Adds Orifice Information to Datagrid by writing to the orificedata table in the db and updating the datagridview
            Dim dsNewRow As DataRow
            Dim userMsg As String
            'Prompt the user to name the new orifice
            userMsg = Microsoft.VisualBasic.InputBox("Enter a Name for the ORIFICE", "C&D ProFlow Orifice Data", "Example: Intake-1 or Exhaust-1")
            'Do the updating work to the database table
            dsNewRow = ProFlow1DataSet.orificedata.NewRow
            dsNewRow.Item("diameter") = Me.TextBox3.Text
            dsNewRow.Item("area") = Me.TextBox7.Text
            dsNewRow.Item("Cd") = Me.TextBox5.Text
            dsNewRow.Item("Range") = Me.TextBox8.Text
            dsNewRow.Item("Name") = userMsg
            ProFlow1DataSet.orificedata.Rows.Add(dsNewRow)
            'Update the datagridview with the new data
            OrificedataTableAdapter.Update(ProFlow1DataSet)
        End Sub
    Form 1 has the combobox that is not updating.

    [combobox]
    datasource = Proflow1DatSetBindingSource
    Display member = name

    Bindingsource [underneath the form] = Bindingsource = Proflow1DatSetBindingSource

    [Proflow1DatSetBindingSource]
    Datasource = ProFlow1DataSet
    Datamember = orificedata

    Form1 load code

    Code:
    Me.OrificedataTableAdapter.Fill(Me.ProFlow1DataSet.orificedata)

    Ok..that's the layout... I hope I included everything.
    Last edited by larrycav; Nov 10th, 2014 at 09:22 PM.

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

    Re: refresh combobox values after updating its datatable and adapter

    Quote Originally Posted by larrycav View Post
    Situation: 2 forms , Access 2003 database
    Combobox on form 1 not showing changed or new records unless I exit debugger and then run it again.

    form2 contains datagrid, dataset, tableadapter, binding source, load code, save code, add record code

    [Binding source]
    Datasource = ProFlow1DataSet
    Datamember = orificedata

    [Datagrid]
    Bindingsource = Proflow1DatSetBindingSource

    Load code
    Code:
     Me.OrificedataTableAdapter.Fill(Me.ProFlow1DataSet.orificedata)
    Save code
    Code:
     Me.OrificedataTableAdapter.Update(ProFlow1DataSet.orificedata)
    If I edit data in the datagrid and click save, the datagrid show the changed value.

    BUT if I add a new item to the datagrid I notice the ID [primary key in the .mdb table] shows a negative number rather than a sequential positive number after I click the Add button.

    If I close that form and reopen it, the ID number is then sequential as it should be. I have a feeling this may be tied in with the main problem of the combobox on form1 now showing updated values. Here's the code that adds a new record to the datagrid

    Code:
    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ' Adds Orifice Information to Datagrid by writing to the orificedata table in the db and updating the datagridview
            Dim dsNewRow As DataRow
            Dim userMsg As String
            'Prompt the user to name the new orifice
            userMsg = Microsoft.VisualBasic.InputBox("Enter a Name for the ORIFICE", "C&D ProFlow Orifice Data", "Example: Intake-1 or Exhaust-1")
            'Do the updating work to the database table
            dsNewRow = ProFlow1DataSet.orificedata.NewRow
            dsNewRow.Item("diameter") = Me.TextBox3.Text
            dsNewRow.Item("area") = Me.TextBox7.Text
            dsNewRow.Item("Cd") = Me.TextBox5.Text
            dsNewRow.Item("Range") = Me.TextBox8.Text
            dsNewRow.Item("Name") = userMsg
            ProFlow1DataSet.orificedata.Rows.Add(dsNewRow)
            'Update the datagridview with the new data
            OrificedataTableAdapter.Update(ProFlow1DataSet)
        End Sub
    Form 1 has the combobox that is not updating.

    [combobox]
    datasource = Proflow1DatSetBindingSource
    Display member = name

    Bindingsource [underneath the form] = Bindingsource = Proflow1DatSetBindingSource

    [Proflow1DatSetBindingSource]
    Datasource = ProFlow1DataSet
    Datamember = orificedata

    Form1 load code

    Code:
    Me.OrificedataTableAdapter.Fill(Me.ProFlow1DataSet.orificedata)

    Ok..that's the layout... I hope I included everything.
    You don't have a problem at all. You simply have two copies of the data from the database. Changing one of those copies doesn't affect the other. Saving the changes from one of the copies to the data doesn't change the other. If you have a ComboBox bound to a DataTable and you never actually change what's in that DataTable, why would what you see in the ComboBox change?

    The contents of your DataTable is representative of what was in the database when you queried it. If the contents of the database changes and you want your DataTable to reflect those changes then you have to query the database again to populate the DataTable again. It's that simple.

    Alternatively, don't use two DataTables. If you only use one DataTable on both forms then both forms will inherently reflect the changes made on the other.
    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

  23. #23
    Hyperactive Member
    Join Date
    Nov 2013
    Posts
    292

    Re: refresh combobox values after updating its datatable and adapter

    Quote Originally Posted by jmcilhinney View Post
    You don't have a problem at all. You simply have two copies of the data from the database. Changing one of those copies doesn't affect the other. Saving the changes from one of the copies to the data doesn't change the other. If you have a ComboBox bound to a DataTable and you never actually change what's in that DataTable, why would what you see in the ComboBox change?

    The contents of your DataTable is representative of what was in the database when you queried it. If the contents of the database changes and you want your DataTable to reflect those changes then you have to query the database again to populate the DataTable again. It's that simple.

    Alternatively, don't use two DataTables. If you only use one DataTable on both forms then both forms will inherently reflect the changes made on the other.
    I"m sure you are correct. However I don't see where the error is. If you could be so kind as to point it out..... I'm shot looking at these screens tonight... Will pick this up tomorrow again..

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

    Re: refresh combobox values after updating its datatable and adapter

    Quote Originally Posted by larrycav View Post
    I"m sure you are correct. However I don't see where the error is. If you could be so kind as to point it out..... I'm shot looking at these screens tonight... Will pick this up tomorrow again..
    There is no "error'. It's all doing exactly what it should do. If you want to update the data in the ComboBox then you have to query the database to populate the DataTable that it's bound to. You already know how to do that because you're already doing it. You're only doing it when the form first loads though, so the data won't change after that. If you want the data to change later then you have to query the database again later.
    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

  25. #25
    Hyperactive Member
    Join Date
    Nov 2013
    Posts
    292

    Re: refresh combobox values after updating its datatable and adapter

    Quote Originally Posted by jmcilhinney View Post
    There is no "error'. It's all doing exactly what it should do. If you want to update the data in the ComboBox then you have to query the database to populate the DataTable that it's bound to. You already know how to do that because you're already doing it. You're only doing it when the form first loads though, so the data won't change after that. If you want the data to change later then you have to query the database again later.
    I have tried this code ....it doesn't work...

    Code:
    Private Sub cboFlowRanges_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
     Me.OrificedataTableAdapter.Fill(Me.ProFlow1DataSet.orificedata)
    End Sub
    Last edited by larrycav; Nov 11th, 2014 at 07:30 AM.

  26. #26
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    311

    Re: [RESOLVED] refresh combobox values after updating its datatable and adapter

    First, why are you filling the DataTable when the user selects a new item in the combobox? By that point, it's too late since the user will only be selecting what is currently in the ComboBox's DataSource.

    I think the problem that you're having is that you don't realize that each Form is a different class, so unless you made the DataTable / TableAdapter as global objects (like by making a separate module that declares those objects) or declare the DataTable for one Form and then refer to the Form's DataTable from the second Form, then as jmcilhinney mentioned, you are actually referring to 2 separate objects in your program and therefore have to refresh each separately. I think that you should just declare the DataTable in Form1 and then refer to that from Form2.

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

    Re: refresh combobox values after updating its datatable and adapter

    Quote Originally Posted by larrycav View Post
    I have tried this code ....it doesn't work...

    Code:
    Private Sub cboFlowRanges_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
     Me.OrificedataTableAdapter.Fill(Me.ProFlow1DataSet.orificedata)
    End Sub
    That code is going to add all the data from the database to the data you already have every time the user selects something different in the ComboBox. Does that make any sense at all? Firstly, if you're getting new data then logic would dictate that you don't want the old data any more. Where are you getting rid of the old data? Secondly, when do you actually want to refresh the data? Surely not every time the user selects an item in the ComboBox. Put some thought into where you put the code based on what event is raised at the time that you want to perform the action.
    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

  28. #28
    Hyperactive Member
    Join Date
    Nov 2013
    Posts
    292

    Re: refresh combobox values after updating its datatable and adapter

    Quote Originally Posted by jmcilhinney View Post
    That code is going to add all the data from the database to the data you already have every time the user selects something different in the ComboBox. Does that make any sense at all? Firstly, if you're getting new data then logic would dictate that you don't want the old data any more. Where are you getting rid of the old data? Secondly, when do you actually want to refresh the data? Surely not every time the user selects an item in the ComboBox. Put some thought into where you put the code based on what event is raised at the time that you want to perform the action.
    Go back and read your other comment... You said I needed to requery the database again from the combobox.... How else would I do it?

    If you want to update the data in the ComboBox then you have to query the database to populate the DataTable that it's bound to. You already know how to do that because you're already doing it. You're only doing it when the form first loads though

  29. #29
    Hyperactive Member
    Join Date
    Nov 2013
    Posts
    292

    Re: refresh combobox values after updating its datatable and adapter

    Quote Originally Posted by jmcilhinney View Post
    That code is going to add all the data from the database to the data you already have every time the user selects something different in the ComboBox. Does that make any sense at all? Firstly, if you're getting new data then logic would dictate that you don't want the old data any more. Where are you getting rid of the old data? Secondly, when do you actually want to refresh the data? Surely not every time the user selects an item in the ComboBox. Put some thought into where you put the code based on what event is raised at the time that you want to perform the action.
    Just take my word for it. The app's use is designed exactly how it has to be to be useful. I have already built this in VBA with Excel and it's been used GLOBALLY for several years... I am trying to redo the app in a stand alone application...

  30. #30
    Hyperactive Member
    Join Date
    Nov 2013
    Posts
    292

    Re: [RESOLVED] refresh combobox values after updating its datatable and adapter

    Quote Originally Posted by Pyth007 View Post
    First, why are you filling the DataTable when the user selects a new item in the combobox? By that point, it's too late since the user will only be selecting what is currently in the ComboBox's DataSource.

    I think the problem that you're having is that you don't realize that each Form is a different class, so unless you made the DataTable / TableAdapter as global objects (like by making a separate module that declares those objects) or declare the DataTable for one Form and then refer to the Form's DataTable from the second Form, then as jmcilhinney mentioned, you are actually referring to 2 separate objects in your program and therefore have to refresh each separately. I think that you should just declare the DataTable in Form1 and then refer to that from Form2.
    I didn't know that. I assumed, perhaps wrongly that once a dataset is created that it's globally available to every form. If not, why bother to have it in the DataSource tool bar? Considering that you can drag and drop items from a dataset onto a form I assumed the myriad of adapters, binding sources, et al.. would work by default and point to the correct sources.... BUT HEY... assuming is not usually a great idea

    I don't know how to make them global objects. Which is easier to declare them global or create a module and make it public?


    Also... so what you're sayhing is that on any form I currently attempt to use an existing dataset, that the GUI doesn't use the actual dataset but instead creates a new instance but with the same name? ...

    Here's something else that confuses me.. IF the combobox on form one and the dgv on the other form are in fact pointed to different datasets [even though they have the same name] then it would make perfect sense that data from one is not shown in the other... I get that..

    BUT, when i exit the debugger and restart it, the same data is displayed in both [if they truly are different] datasets.. WHY? Because both forms use a .fill in the load code. So that means they are both filling from the actual database...correct?

    So does it not stand to reason that I should be able to tell the combobox to FILL again via an index changed sub and get all new or changed data from the database table...pulled into both datasets... After all, it works that way on from load...
    Last edited by larrycav; Nov 11th, 2014 at 06:20 PM.

  31. #31
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: [RESOLVED] refresh combobox values after updating its datatable and adapter

    Talk about grave digging. This thread is like 6 years old lol.

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

    Re: refresh combobox values after updating its datatable and adapter

    Quote Originally Posted by larrycav View Post
    Go back and read your other comment... You said I needed to requery the database again from the combobox.... How else would I do it?
    I know exactly what I said. I said that you need to requery the database if you want new data. I certainly didn't say that you need to do it "from the combobox", which isn't what you're doing anyway. I also didn't say that you should keep the old data when you got the new data. I assumed that that went without saying. Apparently not.

    First, you have to think about WHEN you need to get new data and handle the appropriate event. You certainly do NOT want to get new data every time the user selects an item so obviously the SelectedIndexChanged event handler of the ComboBox is the wrong place for the code. You also need to clear out the old data before getting the new.
    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

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

    Re: refresh combobox values after updating its datatable and adapter

    Quote Originally Posted by larrycav View Post
    Just take my word for it. The app's use is designed exactly how it has to be to be useful. I have already built this in VBA with Excel and it's been used GLOBALLY for several years... I am trying to redo the app in a stand alone application...
    I'll take your word for that. It's completely irrelevant but your word is taken.
    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

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

    Re: [RESOLVED] refresh combobox values after updating its datatable and adapter

    Quote Originally Posted by larrycav View Post
    I didn't know that. I assumed, perhaps wrongly that once a dataset is created that it's globally available to every form. If not, why bother to have it in the DataSource tool bar? Considering that you can drag and drop items from a dataset onto a form I assumed the myriad of adapters, binding sources, et al.. would work by default and point to the correct sources.... BUT HEY... assuming is not usually a great idea

    I don't know how to make them global objects. Which is easier to declare them global or create a module and make it public?


    Also... so what you're sayhing is that on any form I currently attempt to use an existing dataset, that the GUI doesn't use the actual dataset but instead creates a new instance but with the same name? ...

    Here's something else that confuses me.. IF the combobox on form one and the dgv on the other form are in fact pointed to different datasets [even though they have the same name] then it would make perfect sense that data from one is not shown in the other... I get that..

    BUT, when i exit the debugger and restart it, the same data is displayed in both [if they truly are different] datasets.. WHY? Because both forms use a .fill in the load code. So that means they are both filling from the actual database...correct?

    So does it not stand to reason that I should be able to tell the combobox to FILL again via an index changed sub and get all new or changed data from the database table...pulled into both datasets... After all, it works that way on from load...
    The Data Source is a way for you to easily interact with the database. When I say easily, I mean with very little code or even other effort. For instance, if you drag a table from the Data Sources window, the IDE will create a DataGridView, BindingNavigator, BindingSource, DataSet and TableAdapter as well as generate some code. That's the point.

    The typed DataSet generated is a class, just like any other. As such, to be able to use that DataSet you need to create an instance of it, just as would any other class. Don't try to imbue it with any special powers. It's just a class and you use it like any other class.
    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

  35. #35
    Hyperactive Member
    Join Date
    Nov 2013
    Posts
    292

    Re: refresh combobox values after updating its datatable and adapter

    Quote Originally Posted by jmcilhinney View Post
    I know exactly what I said. I said that you need to requery the database if you want new data. I certainly didn't say that you need to do it "from the combobox", which isn't what you're doing anyway. I also didn't say that you should keep the old data when you got the new data. I assumed that that went without saying. Apparently not.

    First, you have to think about WHEN you need to get new data and handle the appropriate event. You certainly do NOT want to get new data every time the user selects an item so obviously the SelectedIndexChanged event handler of the ComboBox is the wrong place for the code. You also need to clear out the old data before getting the new.
    If using index changed on the combobox isn't the answer, then SPECIFICALLY what is?

  36. #36
    Hyperactive Member
    Join Date
    Nov 2013
    Posts
    292

    Re: [RESOLVED] refresh combobox values after updating its datatable and adapter

    Quote Originally Posted by jmcilhinney View Post
    The Data Source is a way for you to easily interact with the database. When I say easily, I mean with very little code or even other effort. For instance, if you drag a table from the Data Sources window, the IDE will create a DataGridView, BindingNavigator, BindingSource, DataSet and TableAdapter as well as generate some code. That's the point.

    The typed DataSet generated is a class, just like any other. As such, to be able to use that DataSet you need to create an instance of it, just as would any other class. Don't try to imbue it with any special powers. It's just a class and you use it like any other class.
    It's got it's own special powers and they're not working well for me at the moment.. What are you driving at with your reply? You're suggesting something perhaps?

  37. #37
    Hyperactive Member
    Join Date
    Nov 2013
    Posts
    292

    Re: refresh combobox values after updating its datatable and adapter

    Quote Originally Posted by jmcilhinney View Post
    I know exactly what I said. I said that you need to requery the database if you want new data. I certainly didn't say that you need to do it "from the combobox", which isn't what you're doing anyway. I also didn't say that you should keep the old data when you got the new data. I assumed that that went without saying. Apparently not.

    First, you have to think about WHEN you need to get new data and handle the appropriate event. You certainly do NOT want to get new data every time the user selects an item so obviously the SelectedIndexChanged event handler of the ComboBox is the wrong place for the code. You also need to clear out the old data before getting the new.
    Actually, I do want the user to get all of the data in that combobox, every single time they select it. Old data, new data...doesn't matter I want them to get all of it... There is a reason for this...

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

    Re: refresh combobox values after updating its datatable and adapter

    Quote Originally Posted by larrycav View Post
    If using index changed on the combobox isn't the answer, then SPECIFICALLY what is?
    It's your application. You tell me. Here's how it works. You ask yourself exactly what happens in the application to prompt the data refresh. You then determine what event is raised or the like when that happens and that tells you where to put the code. I would guess that it's when the second form, i.e. the form that modifies and saves the data, closes but that is based on very limited information. It's your application; you know how you want it to work; you decide.
    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

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

    Re: refresh combobox values after updating its datatable and adapter

    Quote Originally Posted by larrycav View Post
    Actually, I do want the user to get all of the data in that combobox, every single time they select it. Old data, new data...doesn't matter I want them to get all of it... There is a reason for this...
    So, lets' say that you have 10 records in the database to begin with. You populate the ComboBox with those 10 records. The user then adds a new record so you have 11 records in the database. How many items do you want in the ComboBox? Do you really want the original 10 plus the currect 11, i.e. 21 in total, or do you actually just want the current 11 records, as I was saying?
    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

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

    Re: [RESOLVED] refresh combobox values after updating its datatable and adapter

    Quote Originally Posted by larrycav View Post
    It's got it's own special powers and they're not working well for me at the moment.. What are you driving at with your reply? You're suggesting something perhaps?
    I was suggesting that you seem not to understand how Data Sources and typed DataSets work and you might benefit from a bit of an explanation.
    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

Page 1 of 2 12 LastLast

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