Greetings,
I have a comboBox and I want to fill it with Data from 2 tables (Sql Server)... And with ComboBox selectedindexchanged event to populate a few labels with the data from ComboBox items...So how to distinguish the tables ... I mean I have table Customers and table Clients.If I selectindex data from Clients to populate the labels with data from clients and if the index is from Customers data ... to populate the labels with data from Customers.
Satyapriya NayakPosted Dec 21, 2013, 10:35 AM
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Combobox
{
public partial class Form1 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com ,com1;
string str;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("Choose CourseID");
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from Courses";
com = new OleDbCommand(str, con);
OleDbDataReader reader = com.ExecuteReader();
while (reader.Read())
{
comboBox1.Items.Add(reader["CourseID"]);
}
reader.Close();
con.Close();
con.Open();
str = "select * from Students";
com1 = new OleDbCommand(str, con);
OleDbDataReader reader1 = com1.ExecuteReader();
while (reader1.Read())
{
comboBox1.Items.Add(reader1["StudentID"]);
}
reader1.Close();
con.Close();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from Courses where CourseID='" + comboBox1.Text.Trim() + "'";
com = new OleDbCommand(str, con);
OleDbDataReader reader = com.ExecuteReader();
while (reader.Read())
{
label2.Text = reader["CourseTitle"].ToString();
}
reader.Close();
con.Close();
con.Open();
str = "select * from Students where StudentID='" + comboBox1.Text.Trim() + "'";
com1 = new OleDbCommand(str, con);
OleDbDataReader reader1 = com1.ExecuteReader();
while (reader1.Read())
{
label2.Text = reader1["FirstName"].ToString();
}
reader.Close();
con.Close();
}
}
}