Results 1 to 10 of 10

Thread: [RESOLVED] remove duplicate from combo box connected to database

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2003
    Location
    netherlands
    Posts
    163

    Resolved [RESOLVED] remove duplicate from combo box connected to database

    I am trying to remove duplcates from a combobox however this doesn't seem to result in a correct combobox, since he fill the combobox with the following text.

    "System.Linq.Enumerable+d__64`1[System.Char]"

    I have the following code and are using a access database with generated datasets.

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Linq.Expressions;
    
    namespace Generator
    {
            private void matGroupBindingNavigatorSaveItem_Click(object sender, EventArgs e)
            {
                this.Validate();
                this.matGroupBindingSource.EndEdit();
                this.tableAdapterManager.UpdateAll(this.matGroupDataSet);
            }
            private void frmImport_Load(object sender, EventArgs e)
            {
                this.matGroupTableAdapter.Fill(this.materialGroupDataSet.MatGroup);
    
                //Distinct
                var BaseM = materialGroupDataSet.MatGroup.Select(row => row["BaseMs"].ToString().Distinct());
                foreach (var BaseMs in BaseM)
                {
                   cmbBaseMat.Items.Add(BaseMs);
                }
    
            }
    I have 3 comboboxes that i want to fill with data from the database. CoboBox 1 is filled with data and then it the removes the doubles (duplicates). When i select a value from ComboBox 1 it will update CoboBox2 with the possible choices based on ComboBox 1 and if I select a value from ComboBox 2 it fills ComboBox3 with resulting data.

    I'm now first trying to filter out the duplicates.

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

    Re: remove duplicate from combo box connected to database

    Think about the logic and then write code to implement that logic. That is not what you have done. If you need to, pick up a pen and paper and write the logic out. What you want to do is select the BaseMs values from the rows and then get the distinct values from the result. Is that what you're doing? No it is not. That Distinct is being executed before the Select, so how can that do what you need it to? The Select must come before the Distinct.
    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,689

    Re: remove duplicate from combo box connected to database

    Also, if you're going to use LINQ to DataSet then you should use it everywhere, i.e.
    vb.net Code:
    1. row["BaseMs"].ToString()
    should be:
    vb.net Code:
    1. row.Field(Of String)("BaseMs")
    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

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

    Re: remove duplicate from combo box connected to database

    Finally, you should also call AddRange or else set the DataSource rather than calling Add in a loop. You can call ToArray on your results first and then use that with either.
    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
    Addicted Member
    Join Date
    Dec 2003
    Location
    netherlands
    Posts
    163

    Re: remove duplicate from combo box connected to database

    The following seems to work for me.

    Code:
     cmbBaseMat.DataSource = materialGroupDataSet.MatGroup.Select(x => x.BaseMs).Distinct().ToList();
    now is the question how do i make 2 other comboboxes dependent on the selected value from the first choice. Thus filter the combobox based on the selection made.

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

    Re: remove duplicate from combo box connected to database

    Quote Originally Posted by rakker View Post
    now is the question how do i make 2 other comboboxes dependent on the selected value from the first choice. Thus filter the combobox based on the selection made.
    Look at the title of this thread:
    remove duplicate from combo box connected to database
    What has that question got to do with that subject? Nothing, so it doesn't belong in this thread. The issue you asked about is now resolved, so you should use the Thread Tools menu to mark this thread Resolved. If you have a different question on a different topic, create a new thread with a title that reflects that topic and a post that contains all and only the information relevant to that topic. One topic per thread and one thread per topic keeps the forum orderly and easier for everyone to use.
    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

    Thread Starter
    Addicted Member
    Join Date
    Dec 2003
    Location
    netherlands
    Posts
    163

    Re: remove duplicate from combo box connected to database

    J*sus, man sorry for writing that, you dont need to react like that. I wrote both questions, som i'm not outside of my original question.

    On topic thank you for pointing me in the right drection.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,689

    Re: remove duplicate from combo box connected to database

    Quote Originally Posted by rakker View Post
    you dont need to react like that
    Like what? By pointing out what you did wrong and what you should do instead? Oh, you poor thing. Seriously, so many people around here react to the simplest bit of criticism like you're stabbing their mother. Get over it.

    BTW, I see that you still haven't bothered to mark this thread Resolved. Maybe you'll get to that when your mother recovers.
    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

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Dec 2003
    Location
    netherlands
    Posts
    163

    Re: remove duplicate from combo box connected to database

    No, you could just have reacted normal without sounding condescending. I included this question in the first place so i thought to continue in the same topic, that i assumed wrong is on me, but I would have understood if you just had said it at a normal tone. Just writing that my question was answered and that i should start a new thread would have been enough.

    This is a forum where people ask questions opinion. You don't know me, i'm not easily offended or have a hard time with critique so this comment from me is totally on you.

    I appreciate the help you gave, so nothing wrong with that.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,689

    Re: remove duplicate from combo box connected to database

    Quote Originally Posted by rakker View Post
    You don't know me, i'm not easily offended or have a hard time with critique so this comment from me is totally on you.
    Seems you've misjudged yourself considerably.
    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

Tags for this Thread

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