Click here to Skip to main content
13,258,878 members (49,287 online)
Rate this:
 
Please Sign up or sign in to vote.
Hi ;

first i am new about c# , Second i have a access database , inside this db i have 2 form , first one Products (productid , productname) and second one ProductData(id, Productid , Model , width , height , price ) , when i select productname , i want to see that selected products model (every product have different model ) and see that selected (every model have different width and height ) models width , height and see that selected width and height price. but i dont know how to do it Frown | :(

thanks for helps
best regards


here is the code :
        OleDbConnection bag = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Database1.accdb");
        OleDbCommand kmt = new OleDbCommand();
        DataTable tablo = new DataTable();
 
        private void button1_Click(object sender, EventArgs e)
        {
            ListViewItem lvi = new ListViewItem(comboBox1.Text);
            lvi.SubItems.Add(comboBox2.Text);
            lvi.SubItems.Add(comboBox3.Text);
            lvi.SubItems.Add(comboBox4.Text);
            lvi.SubItems.Add(comboBox5.Text);
            lvi.SubItems.Add(comboBox6.Text);
            lvi.SubItems.Add(comboBox7.Text);
            lvi.SubItems.Add(textBox1.Text);
            lvi.SubItems.Add(textBox2.Text);
            listView1.Items.Add(lvi);
            comboBox1.Text = "";
            comboBox2.Text = "";
            comboBox3.Text = "";
            comboBox4.Text = "";
            comboBox5.Text = "";
            comboBox6.Text = "";
            comboBox7.Text = "";
            textBox1.Text = "";
            textBox2.Text = "";
 
 
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'database1DataSet.UrunData' table. You can move, or remove it, as needed.
            this.urunDataTableAdapter.Fill(this.database1DataSet.UrunData);
            OleDbConnection bag = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Database1.accdb");
            bag.Open();
            comboBox1.Items.Clear();
            OleDbDataReader read;
            kmt.Connection = bag;
            kmt.CommandText = "SELECT Productname FROM Products";
            read = kmt.ExecuteReader();
            while (read.Read())
            {
                comboBox1.Items.Add(read[-1]);
            }
            bag.Close();           
        }
 
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {            
        }
 
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {             
        }
 
        private void fillByToolStripButton_Click(object sender, EventArgs e)
        {
            try
            {
                this.urunDataTableAdapter.FillBy(this.database1DataSet.UrunData);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
 
        }
 
        private void fillBy1ToolStripButton_Click(object sender, EventArgs e)
        {
            try
            {
                this.urunDataTableAdapter.FillBy1(this.database1DataSet.UrunData);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
 
        }
Posted 3-Oct-12 2:33am
mcrbne348
Updated 3-Oct-12 12:25pm
v3

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Try to use DataSource property for your names combobox.
kmt = new System.Data.OleDb.OleDbCommand("SELECT ProductID, Productname FROM Products");
kmt.Connection = bag;
tablo = new DataTable("Products");
System.Data.OleDb.OleDbDataAdapter oda = new System.Data.OleDb.OleDbDataAdapter(kmt);
oda.Fill(tablo);
comboBox1.DataSource = tablo;
comboBox1.ValueMember = "ProductID";
comboBox1.DisplayMember = "ProductName";

Then add to your comboBox1_SelectedIndexChanged method the following:
    if( !System.Convert.IsDBNull(comboBox1.SelectedValue) ){
        System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand();
        string query = "SELECT id, ProductID, Model FROM ProductData\n" +
                       "WHERE ProductID = @pid";
        cmd.Parameters.AddWithValue( "@pid", comboBox1.SelectedValue );
        cmd.CommandType = CommandType.Text
        cmd.CommandText = query;
        cmd..Connection = bag;
 
        DataTable tbl2 = new DataTable("ProductData");
        System.Data.OleDb.OleDbDataAdapter od = new System.Data.OleDb.OleDbDataAdapter(cmd);
        od.Fill(tbl2);
        // fill combobox or any other control
        comboBox2.DataSource = tbl2;
        comboBox2.ValueMember = "id";
        comboBox2.DisplayMember = "Model";
    }
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web04 | 2.8.171114.1 | Last Updated 3 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100