Click here to Skip to main content
15,890,512 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#
private void fillProductId()
        {
            SqlConnection con;
            SqlCommand com;
            String message;
            con=new SqlConnection(@"Data Source=AKMINDER-PC\SQLEXPRESS;Initial Catalog=elin appliances(Comaker of PHILIPS);Integrated Security=true");
            message
            = "Select Product_ID, Product 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.DataTextField = "Product";
                combo1.DataValueField = "Product ID";
                combo1.DataBind();               combo1.Items.Insert(0, "--Select--");
            }
            else
            {
                lblMsg.Text = "No product found";
            }
        }


error
C#
combo1.DataTextField = "Product";
                combo1.DataValueField = "Product ID";
                combo1.DataBind();


does not contain a definition for DataTextField ,DataValueField,DataBind and no extension method accepting a first argument of type (are you missing a using directive or an assembly reference?)
Posted
Comments
Sarvesh Kumar Gupta 24-Feb-12 1:37am    
You ar using windows or web
Sarvesh Kumar Gupta 24-Feb-12 1:38am    
in desktop application, combo not have DataTextField and DataValueField

try this:-

ComboBox1.DataSource = dsData.Tables("table1");
ComboBox1.DisplayMember = "Display Field name";
ComboBox1.ValueMember = "Value Field Name";


In your case it will be:-
combo1.DataSource = DS.Tables[0];
combo1.DisplayMember = "Product";
combo1.ValueMember = "Product_ID";


This link will be helpful to you:- http://www.akadia.com/services/dotnet_databinding.html[^]

Please don't forget to mark this as your answer if it helps you out.

Thanks
 
Share this answer
 
v2
Comments
Sarvesh Kumar Gupta 24-Feb-12 1:40am    
There is no need of Combo1.DataBind(); in desktop applications.
Varun Sareen 24-Feb-12 1:55am    
updated sarvesh jee :) thanks
akminder 24-Feb-12 2:51am    
<pre lang="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);


}</pre>
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?
Hi,

in desktop application, combo not have DataTextField and DataValueField.

Try using below code:

C#
combobox1.DataSource  = ds;
combobox1.DisplayMember = "EmpName";
combobox1.ValueMember = "EmpId";
 
Share this answer
 
Comments
akminder 24-Feb-12 2:51am    
<pre lang="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);


}</pre>
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?
try this:-

combo1.DataSource = DS;
combo1.DataTextField= "Product";
combo1.DataValueField= "Product_ID";
combo1.DataBind();

Please don't forget to mark this as your answer if it helps you out.

Thanks
 
Share this answer
 
Comments
akminder 24-Feb-12 2:51am    
<pre lang="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);


}</pre>
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?

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