Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8253303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:50:07+00:00 2026-06-08T00:50:07+00:00

UPDATED FOR CLARIFICATION I posted a similar question a while back found here which

  • 0

UPDATED FOR CLARIFICATION

I posted a similar question a while back found here which used Matlab (and Matlab GUI).

However, now I am trying to do the same thing, but implemented through a Windows Form, C#, and a local, static database file which has been populated with data already.

The data in the database is as follows:

Compound_ID    Component1    Component2    Component3    Value
int            string        string        string        int

with ~24,000 rows of data.

The first column “Compound_ID” is the primary key. The next three columns, “Component1”, “Component2”, and “Component3”, contain one component each from a set of ~100 possible components. Each compound is made up of 3 different components from the set of 100. Order of components doesn’t matter, so the Compounds are combinations (as distinct from permutations) of 3 components. Example:

Compound_ID    Component1    Component2    Component3    Value
1456           a            b              c             10
1457           a            b              m             50
1458           a            c              g             25

etc. From this example, we know that there will never be another compound in the DB that is “a g c” or any other permutation thereof, since order doesn’t matter (A list of 100 values will generate ~161,000 combinations of 3, but almost 1,000,000 permutations of 3).

The user will select some number of components (with a list of checkboxes or a spreadsheet) on the Windows Form. They will press a button on the form. The button will call a method to find all of the Compounds listed in the database which can be made from the list of components the user has selected. This data will be displayed in a dataGridView on the same form.

One way it might be done (similar to the Matlab solution described by another user in my previous post): Generate 3 logical arrays, one for each column, where “1” represents the rows containing one of the 15 components. Add the columns together, and only those rows which have a value of “3” are the rows I am looking for. Then build a new table containing those rows, and display in datagridview. Any tips on how this code might look would be helpful.

I am going to give some of the solutions already provided a try. This would involve figuring out a way to call an SQL Query from C#, which I am told I can look up, and an example has already been provided.

Thanks to everyone for your help. This is a solely independent project born of curiosity, so it is not serious business, I am just trying to figure it out. I am relatively new to C# (and definitely SQL Queries), so please excuse my ignorance. Point me to sources with a little explanation if that would be a better use of everyone’s time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T00:50:08+00:00Added an answer on June 8, 2026 at 12:50 am

    I think this is it:

    UPDATE

    database table values

    PKID    Col1        Col2        Col3        Cost 
    1   Helium      Oxygen      Nitrogen    10
    2   Hydrogen    Chlorine    Sodium      10 
    3   Chlorine    Sodium      Gold        10
    4   Hydrogen    Carbon      Potassium   10 
    5   Carbon      Silicon     Boron       10
    6   Uranium     Cesium      Plutonium   10 
    7   Titanium    Iodine      Fluorine    10
    8   Helium      Neon        Argon       10 
    9   Krypton     Xenon       Radon       10
    10  Barium      Chromium    Calcium     10 
    11  Helium      Lithium     Sodium      10
    

    So if you choose Helium, Oxygen, Nitrogen, Barium, Chromium, Calcium and Uranium, you should get rows with PKIDs 1 and 10, and that’s it. Right?

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void btnFind_Click(object sender, EventArgs e)
        {            
            List<Component> results = new List<Component>();
    
            foreach (object itemChecked in checkedListBox1.CheckedItems)
            {
                var cn = new OdbcConnection(@"Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\elements.mdb;Uid=Admin;Pwd=;");
                StringBuilder sb = new StringBuilder();
                sb.Append("SELECT [PKID], [Col1], [Col2], [Col3], [Cost] ");
                sb.Append("FROM components ");
                sb.Append("WHERE ? IN (Col1, Col2, Col3) ");
                var cm = new OdbcCommand(sb.ToString(), cn);
                try
                {
                    cn.Open();
                    cm.CommandType = System.Data.CommandType.Text;
                    cm.Parameters.AddWithValue("?", itemChecked.ToString());
                    OdbcDataReader dr = cm.ExecuteReader();
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            var comp = new Component(Convert.ToInt32(dr[0].ToString()),
                                dr[1].ToString(), dr[2].ToString(), dr[3].ToString(), Convert.ToInt32(dr[4].ToString()));
    
                            Component r = results.Find(
                                delegate(Component c)
                                {
                                    return c.CompoundID == Convert.ToInt32(dr[0].ToString());
                                }
                                );
                            if (r != null)
                            {
                                //update the frequency
                                var obj = results.FirstOrDefault(x => x.CompoundID == comp.CompoundID);
                                if (obj != null) obj.Frequency++;
                            }
                            else { results.Add(comp); }
                        }
                    }
                    dr.Close();
                }
                catch (Exception ex) { Console.WriteLine(ex.Message); }
                finally { cn.Close(); }
            }
    
            //process/present results at this point
            //for each result in list with freq >= 3, output to grid
            IEnumerable<Component> rowsWithThreeHits = results.Where(cFreq => int.Equals(cFreq.Frequency, 3))
                .Select(x => new Component { CompoundID = x.CompoundID, Component1 = x.Component1, 
                    Component2 = x.Component2, Component3 = x.Component3, CompoundValue = x.CompoundValue });
            List<Component> final = new List<Component>(rowsWithThreeHits);
            dataGridView1.DataSource = final;
        }        
    }
    
    public class Component
    {
        public int CompoundID { get; set; }
        public string Component1 { get; set; }
        public string Component2 { get; set; }
        public string Component3 { get; set; }
        public int CompoundValue { get; set; }
        public int Frequency { get; set; }
    
        public Component() {}
    
        public Component(int compoundID, string component1, string component2, string component3, int compoundValue)
        {
            CompoundID = compoundID; 
            Component1 = component1;
            Component2 = component2;
            Component3 = component3;
            CompoundValue = compoundValue;
            Frequency = 1;
        }
    }
    

    I used Access because we don’t use SQL Server here, but you can swap out the ODBC object for SQL objects and it works.

    UI ScreenShot with Results

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Quick question/clarification required here. I have a DB table that will quite possibly have
UPDATED: I am setting default scope for some models in a runtime which seems
UPDATED: Added one more question (Question #4). Hi all, I'm building myself a custom
UPDATED QUESTION Since the ctor is not supported by .NETCF (public FileStream(IntPtr handle, FileAccess
Updated question, see below I'm starting a new project and I would like to
Updated Appears to be a precedence error and nothing to do with the question
Updated-2 I have interesting combination of warnings & errors. Firstly, when debugging, i get
Updated Example: http://jsfiddle.net/7Cwbn/62/ You can click on the markers Hold Ctrl to select multiple
[Updated with partial answers, some more detailed questions.] Does CouchDB support multi-domain hosting? Yes,
UPDATED: I have some table with sync_numbers, like this: --------------------- id | sync_number ---------------------

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.