Click here to Skip to main content
16,013,082 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have designed a Windows form application with two comboboxes. I want combobox1 to take its contents from a column named "Type" in table "BIn" and combobox2 to take its contents from a column named "PN" in table "BIn". I already solved the case of a single input.but this is double input.I can't seem to be able to successfully extend the single input solution.

What I have tried:

public B()
{
InitializeComponent();

SqlConnection con = new SqlConnection("Data Source=PV10\\LOCALSERVER;Initial Catalog=SmallSoftwareDB;Integrated Security=True;Pooling=False");
con.Open();
string sPn = "select PN from BIn";
string sTp = "select Type from BIn";
SqlCommand CsPn = new SqlCommand(sPn, con);
SqlCommand CsTp = new SqlCommand(sTp, con);

SqlDataReader mDr;
SqlDataReader CmDr;
mDr = CsPn.ExecuteReader();

while (mDr.Read())
{
comboBox2.Items.Add(mDr["PN"].ToString());
}

using (SqlDataAdapter da = new SqlDataAdapter(CsTp))
{
DataTable dt = new DataTable();
da.Fill(dt);
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "Type";
}
}
Posted
Updated 20-Sep-16 1:34am

1 solution

try this

C#
string query = "select PN,Type from BIn";
           SqlCommand cmd = new SqlCommand(query, con);
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           DataTable dt = new DataTable();
           da.Fill(dt);
           comboBox1.DisplayMember = "Type";
           comboBox1.DataSource = dt;
           comboBox2.DisplayMember = "PN";
           comboBox2.DataSource = dt;
 
Share this answer
 
Comments
Nganku Junior 20-Sep-16 8:27am    
Thanks Karthik your code works like a charm. Five to you 🙋
Karthik_Mahalingam 20-Sep-16 8:58am    
welcome Nganku
but you havent given 5★ ;)

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