Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating desktop application in which i have created a form in which there are three comboboxes namely cboproduct,cborawmaterial_id and cborawmaterial.
I have products namely steamiron,mobile,juicer with respective product id.
I have rawmaterials thermostat,inlay,ic,cuplor,jar,led.

I want that when i select one product at a time from cboproduct,i should get its respective rawmaterials in cborawmaterial.
I created a 2 tables
product_info with field product_id and product
rawmaterial_info with field product_id ,rawmaterial_id and rawmaterial

I tried this coding

C#
SqlConnection con;
           // SqlCommand com;
           String message;
           con = new SqlConnection(@"Data Source=AKMINDER-PC\SQLEXPRESS;Initial Catalog=elin appliances(Comaker of PHILIPS);Integrated Security=true");
           SqlCommand com = new SqlCommand();
           message = "Select product_id, product_name from product_info";
           con.Open();
           DataSet DS = new DataSet();
           SqlDataAdapter DA = new SqlDataAdapter();

           DA.SelectCommand = new SqlCommand(message, con);
           // com.ExecuteNonQuery();

           DA.Fill(DS);
           con.Close();
           if (DS.Tables[0].Rows.Count > 0)
           {
               combo1.DataSource = DS.Tables[0];
               combo1.DisplayMember = "product_name";
               combo1.ValueMember = "product_id";

               //combo1.Items.Insert(0, "--Select--");
           }
           else
           {
               lblMsg.Text = "No product found";
           }

               }
                private void fillrawmaterialid(string proid)

       {
           SqlConnection con;
          // SqlCommand com;
          // String message;
           con=new SqlConnection(@"Data Source=AKMINDER-PC\SQLEXPRESS;Initial Catalog=elin appliances(Comaker of PHILIPS);Integrated Security=true");
           SqlCommand com = new SqlCommand();
           //message = "Select Raw_Material_ID ,Raw_Material from Raw_Info where product_id='"+combo1.SelectedIndex+"'";
           com.Connection = con;
           com.CommandText = "Select Raw_Material_ID ,Raw_Material from Raw_Info where product_id='" + proid + "'";
         com.Parameters.AddWithValue("@proid", proid);

           DataSet DS = new DataSet();
           SqlDataAdapter DA = new SqlDataAdapter();


           DA.SelectCommand = com;
           con.Open();

          // com.ExecuteNonQuery();

           DA.Fill(DS);
           con.Close();
           if (DS.Tables[0].Rows.Count > 0)
           {
               comborawid.DataSource = DS.Tables[0];
               comborawid.DisplayMember = "Raw_Material";
               comborawid.ValueMember = "Raw_Material_ID";

               //combo1.Items.Insert(0, "--Select--");
           }
           else
           {
               lblMsg.Text = "No product found";
           }
private void combo1_SelectedIndexChanged(object sender, EventArgs e)
       {
          string proid=(combo1.SelectedIndex.ToString());
           fillrawmaterialid(proid);


       }

Now with valuemember and Display member i am getting values in combo1 but i also want the list of raw material corressponding to selected index of Combo1 in cborawmaterial.
e.g if I select Product laptop in combo1_SelectedIndexChanged then only I should get specific raw material related to laptop not all rawmaterials which is stored in database
but i am getting whole list of raw material

do tell me about select query ...what change is need?
Posted
Comments
Varun Sareen 24-Feb-12 3:17am    
the code seems to be right..show us the error please.

Hi Ak

I don't think you need the line:

C#
com.Parameters.AddWithValue("@proid", proid);


Since you are already inserting a value for proid in the string:

C#
com.CommandText = "Select Raw_Material_ID ,Raw_Material from Raw_Info where product_id='" + proid + "'";


The select query to get the raw materials looks correct to me.

I was thinking how do you initialise the rawid combo box? So when the combo boxes are initially displayed do they contain anything? It might be worth clearing the contents of the raw material combo before you populate it in fillrawmaterialid().
 
Share this answer
 
v3
In my view the better solution is to use BindingSource with DataRelation

An example is given here

http://msdn.microsoft.com/en-us/library/c12c1kx4.aspx[^]

In your case set the combo1.DataSource = MasterBindingSource and
comborawid.DataSource = DetailsBindingSource

This way whenever a product is selected in combo1, the corresponding raw materials will be displayed in comborawid, with the datarelation.

If your problem is solved, you may accept and vote the solution, otherwise please post your queries.

PES
 
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