By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,617 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

How to set datasource for two comboboxes on the same form, from two columns of a tabl

P: 2
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.

Here's what I've 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";
}
}
Sep 20 '16 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.