Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9004701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:57:18+00:00 2026-06-16T00:57:18+00:00

I have a Combobox to fill the data for City, State and PinCode these

  • 0

I have a Combobox to fill the data for City, State and PinCode
these combobox is dopdown list and the user will pick it.

and it loads once the form opens.

Here is the CODE:

            /// CODE TO BRING A DATA FROM SQL INTO THE FORM DROP LIST

            /// To fill the sates from States Table

            cn = new SqlConnection(@"Data Source=Nick-PC\SQLEXPRESS;Initial Catalog=AutoDB;Integrated Security=True");
            cmd= new SqlCommand("select * from TblState",cn);
            cn.Open();
            SqlDataReader dr;

            try
            {
                dr = cmd.ExecuteReader();
                while (dr.Read())

                {
                    SelectState.Items.Add(dr["State"].ToString());
                }

            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            finally
            {
                cn.Close();
            }

            //To fill the Cities from City Table

            cn1 = new SqlConnection(@"Data Source=Nick-PC\SQLEXPRESS;Initial Catalog=AutoDB;Integrated Security=True");
            cmd1 = new SqlCommand("SELECT * FROM TblCity", cn);
            cn.Open();
            SqlDataReader ds;

            try
            {
                ds = cmd1.ExecuteReader();
                while (ds.Read())
                {
                    SelectCity.Items.Add(ds["City"].ToString());
                }

            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            finally
            {
                cn1.Close();
            }

            // To fill the Data in the Pincode from the City Table
            cn2 = new SqlConnection(@"Data Source=Nick-PC\SQLEXPRESS;Initial Catalog=AutoDB;Integrated Security=True");
            cmd2 = new SqlCommand("SELECT (Pincode) FROM TblCity ", cn2);
            cn2.Open();
            SqlDataReader dm;

            try
            {
                dm = cmd2.ExecuteReader();
                while (dm.Read())
                {
                    SelectPinCode.Items.Add(dm["Pincode"].ToString());
                }

            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            finally
            {
                cn2.Close();
            }

its kinda Big, i am doing the same steps for all the combo-box, but is there a way i can merge it in a simple way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:57:19+00:00Added an answer on June 16, 2026 at 12:57 am

    The stick-to-the-basics approach – refactor your code into a single method.

    static IEnumerable<string> Load(string tableName, string columnName)
    {
        List<string> result = new List<string>();
    
        try
        {
            using (var cn = new SqlConnection(@"Data Source=Nick-PC\SQLEXPRESS;Initial Catalog=AutoDB;Integrated Security=True"))
            {
                cn.Open();
                using (var cmd = new SqlCommand("SELECT * FROM " + tableName, cn))
                {
                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            result.Add(reader[columnName].ToString());
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    
        return result;
    }
    

    Crude assumptions made:

    1. Only querying 1 table, 1 column.
    2. Not checking for nulls on the IDataReader.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a combobox bound to a datatable that I fill with user's names
i have comboBox that i fill in data like this: SQL = select DISTINCT
I have to fill a combobox with month in English: January, February, etc. I
I have a combobox in a dialog box form. I need to fill this
I have a ComboBox and I want to bind a generic List to it.
I have a VB.NET 2010 application that loads data from SQL Server to a
I am trying to validate fields so user can't have entered data with empty
I have binded the accdb to combobox but it can not bind the data
I have an NSArrayController that I fill in the awakeFromNib method. The data has
In Delphi 2007 I have a DBGrid. I need to fill a ComboBox when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.