Click here to Skip to main content
16,011,949 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm fresher this is my first project.I have Written this method in windows form directly.Now i Want to implement 2-tier architecture.
I will post the code.Actual method in windows project.And What code i have written in class file both.
Now if i create method like that how to bind to the combobox.Please Help me how to do this.

What I have tried:

C#
this code i was written in windows project
----------------------------------------------

       private void FillBankName()
        {
            SqlConnection con = new SqlConnection(connectionString);
            con.Open();
            SqlCommand cmd = new SqlCommand("select bank_Id,bank_Name from 
   bankdetails", con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            Dictionary Dctbnk = new Dictionary();
            Dctbnk.Add("select", "select");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Dctbnk.Add(dt.Rows[i]["bank_Name"].ToString(), dt.Rows[i]["bank_Id"].ToString());
            }
            cmbBankname.ValueMember = "Value";
            cmbBankname.DisplayMember = "Key";
            cmbBankname.DataSource = new BindingSource(Dctbnk, null);
            con.Close();
--thisis i written inclass  file
--------------------------------------------
          public DataTable FillBankName()
        {
            SqlConnection con = new SqlConnection(connectionString);
            con.Open();
            SqlCommand cmd = new SqlCommand("select bank_Id,bank_Name from bankdetails", con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            Dictionary<string, string> Dctbnk = new Dictionary<string, string>();
            Dctbnk.Add("select", "select");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Dctbnk.Add(dt.Rows[i]["bank_Name"].ToString(), dt.Rows[i]["bank_Id"].ToString());
            }    
            con.Close();
            return dt;
}      
Posted
Updated 6-Jun-17 2:27am
v2

1 solution

Um. There's a lot of odd code there.
Look at your FillBankName method: why are you doing anything with the dictionary Dctbank, when you don't use it for anything afterwards, and throw it away when the method ends?

I'd suggest that you step back and think about what you want to use to pass data around: Dictionary or DataTable, and write functions which return the appropriate type.
I'd also suggest that you start using Dispose for SQL components: they are scarce resources and should be enclosed in a using block. So the DataTable version would be:
C#
public DataTable FillBankName()
    {
    DataTable dt = new DataTable();
    using (SqlConnection con = new SqlConnection(connectionString))
        {
        con.Open();
        using (SqlCommand cmd = new SqlCommand("SELECT bank_Id,bank_Name FROM bankdetails", con))
            {
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                sda.Fill(dt);
                }
            }
        }
    return dt;
    }

Multi-tier organisation is about separation of concerns: the DB tier knows about the DB only, and the presentation tier knows how to interface with the user and translate that into stuff the data tier can understand. Adding what is effectively presentation code into your data code (such as your "select", "select" row) isn't a good idea, as it forced the data layer to "know" what is happening. I'd stick with DataTable (or better, a collection of a defined class specific to the data you transfer) instead of going with the Dictionary as part of the interface.
 
Share this answer
 
Comments
Member 13153537 6-Jun-17 8:47am    
Thank you.
OriginalGriff 6-Jun-17 8:50am    
You're welcome!

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