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 3398368
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:38:27+00:00 2026-05-18T04:38:27+00:00

I am using the following code and it works fine. protected void Button2_Click(object sender,

  • 0

I am using the following code and it works fine.

protected void Button2_Click(object sender, EventArgs e)
    {
        String a = DropDownList1.SelectedItem.Value;
        String b = DropDownList3.SelectedItem.Value.PadLeft(3, '0');      
        String c = TextBox2.Text.PadLeft(5,'0').ToString();
        String d = TextBox3.Text.ToString();
        String digit = a+ b  + c + d;
        try
        {
         myConn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=testcase;User=root;Password=root;Option=3;");
         myConn.Open();
            //**
            string sql = "select * from testcase.main where reg_no =?";            
            //**
            OdbcCommand cmd = new OdbcCommand(sql, myConn);            
            //**
            cmd.Parameters.AddWithValue("?", digit);
            MyReader = cmd.ExecuteReader();
            //**
            while (MyReader.Read())
            {
                String f = MyReader["pet_name"].ToString();
                String g = MyReader["res_name"].ToString();

                Label9.Visible = true;
                Label9.Text = f;

                Label10.Visible = true;
                Label10.Text = "VS";

                //Label11.Visible = true;
                Label11.Text = g;

            }

            MyReader.Close();
        }
        catch (Exception e1)
        {
            Response.Write(e1.ToString());
        }
        finally
        {
            if (MyReader != null && !MyReader.IsClosed)
            {
                MyReader.Close();   
            }

            if (myConn != null && myConn.State == ConnectionState.Open)
            {
                myConn.Close();
            }
        }

I want to add another sql query which is using two tables to fetch the data and than i want to display it in a dropdownlist after fetching data from database.

How should i proceed??
Should i create a totally new connection?
I tried many different ways like, creating new connection and new reader and entire try and catch block i coded again but on running the website i t was taking very long to load the contents.

What i did i modified only this portion(below code) and used the entire try catch again but it dint work.

while (MyReader1.Read())
            {
                String f = MyReader1["ret"].ToString();

             DropDownList1.Items.Add(f);

            }

Please help.

  • 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-05-18T04:38:28+00:00Added an answer on May 18, 2026 at 4:38 am

    Yes you should create a new connection and a new reader. It’s good practice to open and close the connection as soon as possible. If the content was taking awhile to load up, it’s most likely not the connection creation. Look at the indexing of the sql tables or how you’re loading the content.

    From the looks of it, if your query returns multiple results (which I’m guessing it’s not?), you will be overwriting same label over and over agin.

    You should wrap your OdbcConnection object & MyReader object in using statements. That way you don’t need the .close statements & Finally block. It’s a bit cleaner.

          private void DoSomething()
        {
            String a = DropDownList1.SelectedItem.Value;
            String b = DropDownList3.SelectedItem.Value.PadLeft(3, '0');
            String c = TextBox2.Text.PadLeft(5, '0').ToString();
            String d = TextBox3.Text.ToString();
            String digit = a + b + c + d;
            String sql = "select * from testcase.main where reg_no =?";
    
            try
            {
                using (OdbcConnection myConn = new OdbcConnection("Driver={MySQL ODBC 3.51   Driver};Server=localhost;Database=testcase;User=root;Password=root;Option=3;"))
     using(OdbcCommand cmd = new OdbcCommand(sql, myConn))
                    {
    
    
       myConn.Open();
                        //**
                        cmd.Parameters.AddWithValue("?", digit);
                        using (odbcReader MyReader = cmd.ExecuteReader())
                        {
                            //**
                            while (MyReader.Read())
                            {
                                String f = MyReader["pet_name"].ToString();
                                String g = MyReader["res_name"].ToString();
    
                                Label9.Visible = true;
                                Label9.Text = f;
    
                                Label10.Visible = true;
                                Label10.Text = "VS";
    
                                //Label11.Visible = true;
                                Label11.Text = g;
    
                            }
                        }
                    }
                }
            }
            catch (Exception e1)
            {
                Response.Write(e1.ToString());
            }
        }
    
    }
    

    Edit – Fixed the nested statement

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made something like the following code: protected void Page_Load(object sender, EventArgs e)
I have the following code: protected void Page_Load(object sender, EventArgs e) { var openId
Using the following code I get a nice formatted string: Request.QueryString.ToString Gives me something
I have the following code: class Program { static void Main(string[] args) { string
I'm using the following code to do a checksum of a file which works
following is my code for loading one video using FileReference class and it works
I'm using the following code to query a database from my jsp, but I'd
I've been using the following code to open Office Documents, PDF, etc. on my
I am using following PHP code to connect to MS Access database: $odb_conn =
I am currently using the following code to create a web request: Dim myRequest

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.