Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi experts,

i have 2 combobox.

if user select 1 item in combobox1 on load page ,then related items of that value will displayed in combobox 2.

i have fetch the value in combobox 1 here are the code......

SQL
HERE ARE THE TABLE1 :
PROCESS                  ID     MATERIAL
Wraping	                 1	AFA
Moulding	         2	AFA
WiperIDFinishing	 3	AFA


SQL
TABLE 2:
MATERIAL
AFA




C#
private void InventoryEntry_Load(object sender, EventArgs e)
       {

 try
            {
                SqlDataAdapter da = new SqlDataAdapter("select Material  from Master.Materialname", objConn1);
                DataTable dt = new DataTable();
                da.Fill(dt);
                DataRow dr;
                dr = dt.NewRow();
                //dr.ItemArray = new object[] { 0, "---Select an item---" };
                //dt.Rows.InsertAt(dr, 0);
                comboBox3.DisplayMember = "Material";
                comboBox3.ValueMember = "Material";
                comboBox3.DataSource = dt;
                objConn1.Close();
            }
            catch (Exception ex)
            {
            }

}
Posted
Updated 4-Nov-12 21:30pm
v4
Comments
n.podbielski 5-Nov-12 3:02am    
1. Please format your question.
2. What checkbox? There is none so far.
3. Dont use try catch in that way. This statement is not for universal bug fixing.
Master Vinu 5-Nov-12 3:36am    
yes ..its mistake...sorry for mistakenly write checkbox.... pls update if you have any hint
Nandakishore G N 5-Nov-12 3:23am    
vinayak..is your question means..as loading data to combobox2 on selection of the combobox1 ..or to checkboxlist ?.none can understand the question.so it is better to reframe the question, as said by pod...change it..i think there are lots of example on the above question..google it properly..
Master Vinu 5-Nov-12 3:36am    
sorry for sorry for mistakenly write checkbox.... pls update if you have any hint
Master Vinu 5-Nov-12 3:31am    
sorry for mistakenly write checkbox

Just write sql with parameter
SQL
select Material  from Child.Materialname
where Child.Master = @master


Use this sql to reload second combobox on change event of first combo. Since you did not write witch combobox you are using I can't tell how this event is named I would guess ValueChanged

C#
protected void combo1_ValueChanged(object source , ValueChangedArgs args)
{
 FetchForCombo2WithMasterParam(combo1.Value);
}


And in FetchForCombo2WithMasterParam you would execute sql above with param from combo1.Value.
 
Share this answer
 
Comments
Master Vinu 5-Nov-12 4:27am    
string sSql = "Select id from Master.Materialname where Material = '" + comboBox3.SelectedValue + "'";
SqlCommand cmd = new SqlCommand(sSql,objConn1);
int mid = Convert.ToInt32(cmd.ExecuteScalar());
string reffSql = "Select Process from Master.ProductionProcess where id = '" + mid + "'";
SqlCommand rcmd = new SqlCommand(reffSql, objConn1);
comboBox4.SelectedValue = Convert.ToString(rcmd.ExecuteScalar());

i have used above one but not working
n.podbielski 5-Nov-12 5:05am    
And what error do you having?
Master Vinu 5-Nov-12 5:06am    
there is no error but combo4 does not contain any value
n.podbielski 5-Nov-12 5:33am    
Check value of comboBox3.SelectedValue maybe its empty or have value non existing in second table?
Master Vinu 5-Nov-12 5:09am    
here are the updated table :first table :MaterialName
Material mid
AFA 1
AFB 2
AFC 3
AFD 4
AFE 5
AFJ 6
AFL 7
AFQ 8


second table:ProductionProcess
process mid Material
Wraping 1 AFA
Moulding 2 AFA
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
string sql2= "select process from Master.ProductionProcess as a inner join Master.Materialname as b on a.Material = b.mid WHERE b.material ='" + comboBox3.SelectedValue.ToString()+ "'";
SqlDataAdapter da = new SqlDataAdapter(sql2,objConn1);
DataTable dt = new DataTable();
da.Fill(dt);
DataRow dr;
dr = dt.NewRow();
comboBox4.DisplayMember = "process";
comboBox4.ValueMember = "process";
comboBox4.DataSource = dt;
}
 
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