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

  • Home
  • SEARCH
  • 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 9031557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:45:40+00:00 2026-06-16T07:45:40+00:00

I am programmer in asp.net. I am using C#. I have written very lengthy

  • 0

I am programmer in asp.net. I am using C#. I have written very lengthy code for query execution in each time. How to re-factor and organize the following code?

MySqlConnection connection = new MySqlConnection(connstring);
string query = "Select fo_region_Name from fo_region where fo_region_DeleteStatus=0";
MySqlCommand command = new MySqlCommand(query, connection);
MySqlDataReader reader;
connection.Open();
reader = command.ExecuteReader();
while (reader.Read())
{
    ddl_Country.Items.Add(UppercaseFirst(reader[0].ToString()));
}
connection.Close();

query = "Select Fo_Nationality_Name from fo_Nationality a, Fo_region b where a.Fo_Nationality_Type=1 and "
        + "LEFT(a.Fo_Nationality_Code,2)=LEFT(b.fo_region_Name,2)  and  a.Fo_Nationality_DeleteStatus=0 and "
        + "b.fo_region_DeleteStatus=0 Union Select Fo_Nationality_Name from fo_nationality where Fo_Nationality_DeleteStatus=0";
command = new MySqlCommand(query, connection);
connection.Open();
reader = command.ExecuteReader();
while (reader.Read())
{
    ddl_Nationality.Items.Add(UppercaseFirst(reader[0].ToString()));
}
connection.Close();

query = "select mcs_CreditCard_CardName from mcs_creditcard where mcs_CreditCard_DeleteStatus=0";
command = new MySqlCommand(query, connection);
connection.Open();
reader = command.ExecuteReader();
while (reader.Read())
{
    ddl_CreditCard.Items.Add(UppercaseFirst(reader[0].ToString()));
}
connection.Close();
  • 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-16T07:45:42+00:00Added an answer on June 16, 2026 at 7:45 am

    Some thoughts:

    • Use multiline strings to format your SQL statements.
    • There is no need to close and reopen the connection betwween each command execution.
    • There is also no need to create new connection and command objects (in this case)
      • If you have parameters on the command objects, it is easier to create new command objects, rather than clearing out the old parameters
    • Use var statements to have the C# compiler automatically determine the variable type for you.
    • Wrap objects that need to be disposed, in a using block.

    using (var connection = new MySqlConnection(connstring)) {
        connection.Open();
    
        using (var command = new MySqlCommand()) {
            MySqlDataReader reader;
    
            command.CommandText = @"
                SELECT fo_region_Name
                FROM fo_region
                WHERE fo_region_DeleteStatus=0
            ";
            using (reader = command.ExecuteReader()) {
                while (reader.Read()) {
                    ddl_Country.Items.Add(UppercaseFirst(reader[0].ToString()));
                }
            }
    
            command.CommandText = @"
                SELECT Fo_Nationality_Name
                FROM fo_Nationality a,
                    Fo_region b
                WHERE a.Fo_Nationality_Type = 1
                    AND LEFT(a.Fo_Nationality_Code,2) = LEFT(b.fo_region_Name,2)
                    AND b.fo_region_DeleteStatus=0
    
                UNION SELECT Fo_Nationality_Name 
                FROM fo_nationality
                WHERE Fo_Nationality_DeleteStatus=0
            ";
            using (reader = command.ExecuteReader()) {
                while (reader.Read()) {
                    ddl_Nationality.Items.Add(UppercaseFirst(reader[0].ToString()));
                }
            }
    
            command.CommandText = @"
                SELECT mcs_CreditCard_CardName
                FROM mcs_creditcard
                WHERE mcs_CreditCard_DeleteStatus = 0
            ";
            using (reader = command.ExecuteReader()) {
                while (reader.Read()) {
                    ddl_Nationality.Items.Add(UppercaseFirst(reader[0].ToString()));
                }
            }
        }
    }
    

    With LINQ (add a using System.Data.Common statement):

            using (reader = command.ExecuteReader()) {
                /*while (reader.Read()) {
                    ddl_Country.Items.Add(UppercaseFirst(reader[0].ToString()));
                }*/
                ddl_Country.Items.AddRange((
                    from DbDataRecord row in reader
                    select new ListItem(
                        UppercaseFirst(reader.GetString(0))
                    )
                ).ToArray());
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am an ASP.NET WebForms programmer and I'm very new to ASP.NET MVC3. I've
So I'm using ASP.NET with C#. And I have an item that is queried
when the average ASP.net C# programmer (with very poor JavaScript knowledge other than scripting
I am programmer of ASP .NET using C#, recently working on .Net Framework 3.5
I'm using ASP.NET MVC 3 to built my site (former PHP programmer), using Entity
I have created an asp.net MVC application in VS 2010, and am using TFS.
I'm developing a mobile application using jQuery. On Microsoft Azure, I have an Asp.Net
Good evening/morning/after/noon. I have an ASP.net 3.5 website and I am using vb.net in
I am new to C# and ASP.net. I am a ColdFusion Programmer but I
I know ASP.NET C# very well. I make a lot of stuff like surveys

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.