Click here to Skip to main content
13,895,584 members
Rate this:
 
Please Sign up or sign in to vote.
See more: , +
i write the code to fill combobox with database value. my code is working correctly if this code written on presentation layer. But i want to written this code on data layer. i have 3 project in my solution (UI, BLL, DAL). i want this code in DAL and then call in UI with the help of BLL. how to do that. please help me. here is my code.

How to convert my code into 3 tier architecture.

What I have tried:

using (SQLiteConnection conn = new SQLiteConnection(EmployeeComboFills.ecbconn()))
            {
                string CommandText = "SELECT Name FROM User";
                using (SQLiteCommand cmd = new SQLiteCommand(CommandText, conn))
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    DataTable dt = new DataTable();
                    SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
                    da.Fill(dt);
                    foreach (DataRow dr in dt.Rows)
                    {
                        CELD_employeename.Items.Add(dr["Name"].ToString());
                    }
                }
            }
Posted
Updated 12-Jan-19 0:08am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Put that code in a function in the data layer but don't do the "foreach", just have that method return "dt". The business layer will just be a single line that will call the function from the data layer and return the result. The presentation layer will then get the datatable from the business layer and it fill do the "foreach" to add the items to CELD_employeename
   
Comments
Member 14115523 11-Jan-19 13:05pm
   
but how to write code on presentation layer. sorry for stupid question i'm newbie in c#.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Try this.

void List(){
SQLConnection.MsSql con = new SQLConnection.MsSql();
                System.Data.SqlClient.SqlConnection objConnd = new System.Data.SqlClient.SqlConnection();
                System.Data.SqlClient.SqlDataAdapter dtAdapterd = new System.Data.SqlClient.SqlDataAdapter();
                DataTable dtd = new DataTable();
                String strConnString = con.conn;
                objConnd = new System.Data.SqlClient.SqlConnection(strConnString);
                objConnd.Open();
                String strSQL;
                strSQL = "SELECT Name FROM User";
                dtAdapterd = new System.Data.SqlClient.SqlDataAdapter(strSQL, objConnd);
                dtAdapterd.Fill(dtd);
                dtAdapterd = null;
                objConnd.Close();
                objConnd = null;
                this.comboboxFilter.DataSource = dtd;
                this.comboboxFilter.ValueMember = "Name";
                this.comboboxFilter.DisplayMember = "Name";
                this.comboboxFilter.SelectedIndex = -1;
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web05 | 2.8.190306.1 | Last Updated 12 Jan 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100