Click here to Skip to main content
15,885,876 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
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 :
C#
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
Updated 3-Oct-12 11:25am
v3

1 solution

Try to use DataSource property for your names combobox.
C#
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:
C#
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";
}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900