Click here to Skip to main content
15,917,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i us this code and it work good with me bout what im asking for is this the good way to code it ( I use it to get data from table to link it with another combobox to make them work together).

What I have tried:

the first combobox

C#
public Needform()
    {
        string mainconn = ConfigurationManager.ConnectionStrings["MY"].ConnectionString;
        SqlConnection sqlconn = new SqlConnection(mainconn);
        string sqlquery = "select * from [dbo].[Governorate]";
        SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn);
        sqlconn.Open();
        SqlDataAdapter sda = new SqlDataAdapter(sqlcomm);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        comboBoxGovernorate.ValueMember = "id_Governorate";
        comboBoxGovernorate.DisplayMember = "name";
        comboBoxGovernorate.DataSource = dt;
        comboBoxCity.Enabled = false;
  }



the another one

C#
private void ComboBoxGovernorate_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (comboBoxGovernorate.SelectedValue.ToString() != null)
        {
            string mainconn = ConfigurationManager.ConnectionStrings["MY"].ConnectionString;
            SqlConnection sqlconn = new SqlConnection(mainconn);
            string sqlquery = "select * from [dbo].[City]where id_Governorate=@id_Governorate";
            SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn);
            sqlconn.Open();
            sqlcomm.Parameters.AddWithValue("@id_Governorate", comboBoxGovernorate.SelectedValue.ToString());
            SqlDataAdapter sda = new SqlDataAdapter(sqlcomm);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            comboBoxCity.ValueMember = "id";
            comboBoxCity.DisplayMember = "city";
            comboBoxCity.DataSource = dt;
            comboBoxCity.Enabled = true;
        }
    }


thx and i hope all learn from that
Posted
Updated 9-Feb-19 20:15pm

1 solution

It's ok, it works. But ... connections and commands are scarce resources and should be Disposed when you have finished with them. The easiest way is to use a using block:
public void Needform()
    {
    string mainconn = ConfigurationManager.ConnectionStrings["MY"].ConnectionString;
    using (SqlConnection sqlconn = new SqlConnection(mainconn))
        {
        sqlconn.Open();
        string sqlquery = "SELECT * FROM [dbo].[Governorate]";
        using (SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn))
            {
            using (SqlDataAdapter sda = new SqlDataAdapter(sqlcomm))
                {
                DataTable dt = new DataTable();
                sda.Fill(dt);
                comboBoxGovernorate.ValueMember = "id_Governorate";
                comboBoxGovernorate.DisplayMember = "name";
                comboBoxGovernorate.DataSource = dt;
                comboBoxCity.Enabled = false;
                }
            }
        }
    }

private void ComboBoxGovernorate_SelectedIndexChanged(object sender, EventArgs e)
    {
    if (comboBoxGovernorate.SelectedValue.ToString() != null)
        {
        string mainconn = ConfigurationManager.ConnectionStrings["MY"].ConnectionString;
        using (SqlConnection sqlconn = new SqlConnection(mainconn))
            {
            sqlconn.Open();
            string sqlquery = "SELECT * FROM [dbo].[City] WHERE id_Governorate=@id_Governorate";
            using (SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn))
                {
                sqlcomm.Parameters.AddWithValue("@id_Governorate", comboBoxGovernorate.SelectedValue.ToString());
                using (SqlDataAdapter sda = new SqlDataAdapter(sqlcomm))
                    {
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    comboBoxCity.ValueMember = "id";
                    comboBoxCity.DisplayMember = "city";
                    comboBoxCity.DataSource = dt;
                    comboBoxCity.Enabled = true;
                    }
                }
            }
        }
    }
There are two other changes I'd recommend:
1) Just a "style" thing, but use UPPERCASE for SQL keywords - it makes it easier to "find" them in a command.
2) Don't do "SELECT * FROM" at all - always name the columns you need to fetch. That way, you don't waste bandwidth fetching data you aren't going to use (and if you add images for example that can be significant), and it helps "protect" you from DB layout changes since you control the order in which they are returned. With tables for example, that can make a big difference to your app when you update things later.
 
Share this answer
 
Comments
el_tot93 10-Feb-19 4:17am    
thx you bro for your help and thx you again for your time
OriginalGriff 10-Feb-19 5:10am    
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