Reputation: 1
how to populate a combobox using 3tier architecture?i have used this code;
private void BindDepName()
{
MySqlConnection con = new MySqlConnection("server=localhost;User Id=root;password=sa;database=employeedetails");
con.Open();
string MySql = "SELECT * FROM tbldept";
MySqlDataAdapter da = new MySqlDataAdapter(MySql, con);
DataSet ds = new DataSet();
da.Fill(ds);
DepName.DataSource = ds.Tables[0];
DepName.ValueMember = "Dept_id";
DepName.DisplayMember = "Dept_name";
string i = ds.Tables[0].Rows[0].ItemArray[1].ToString();
DepName.Text = "select dept";
con.Close();
}
Upvotes: 0
Views: 789
Reputation: 44595
in a 3tier world you will have one class in the BusinessLogic
class library which returns some kind of enumerable (IEnumerable, List, Collection, array...) of Entities
to the user interface; as good practice the UI should not work directly with DataTable and DataSet.
the actual type of objects you are returning to fill the UI controls (combo box in your case) depends on your design and preferences, if you use Entity Framework you can have entities defined and modeled in the EF designer/model.
the lower part of the architecture is the Dala Access Layer
, imagine it as a class library that is the only one allowed and capable to estabilish the connection to the database, so the mySql connection management and actual query execution should be isolated and not exposed upwards from the DAL.
UI calls BL which calls DAL.
DAL connects to the database, queries and returns entities to BL.
BL returns entities (kind of collection) to the UI for binding.
to give it a try and keep it simple, only for starting, you can skip the entity modeling and return datatable so you can test the logic. :)
Upvotes: 1