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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:28:38+00:00 2026-06-13T04:28:38+00:00

I am using a method which get me a list of items from database.

  • 0

I am using a method which get me a list of items from database. I already used the connection to insert data to database and works fine, but when displaying data in gridview an exception pops up. Below you can find the method used, the code for binding data to gridview and the asp for the gridview. Hope you can help. Thanks

Method to retreive data

public List<Bet> getBets()
        {
            MySqlCommand cmd = Connection.CreateCommand();
            cmd = new MySqlCommand("SELECT * FROM bets ORDER BY date");

            try
            {
                if (this.Connection.State == ConnectionState.Closed)
                    this.Connection.Open();

                MySqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); --> getting exception here
                List<Bet> bets = new List<Bet>();

                while (dr.Read())
                {
                    Bet myBet = new Bet();
                    myBet = FillBetfromRow(dr);
                    bets.Add(myBet);
                }
                return bets;
            }
            catch (MySqlException ex)
            {
                throw ex;
            }
            finally
            {
                if (Connection.State == ConnectionState.Open)
                    Connection.Close();
            }
        }

Binding data to gridview

gvBets.DataSource = new BetManagement().getBets();
            gvBets.DataBind();
  • 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-13T04:28:39+00:00Added an answer on June 13, 2026 at 4:28 am

    Try changing with Using Keyword. There may be an issue with the data reader close

     try
            {
                if (this.Connection.State == ConnectionState.Closed)
                    this.Connection.Open();
                List<Bet> bets = new List<Bet>();
                using(MySqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)){
    
    
                while (dr.Read())
                {
                    Bet myBet = new Bet();
                    myBet = FillBetfromRow(dr);
                    bets.Add(myBet);
                }
           }
                return bets;
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page based web method which works fine, using [WebMethod], i.e [WebMethod]
I am trying to compare a value which was grabbed using the get method
I'm using a database, and there's a list fragment which use a cursor adapter
I have got method which is using Assembly.LoadFrom(...) statement and returns the supported cultures
I have following method which I am using to load ActiveX control dynamically, Dim
I'm using a Segment to Segment closest approach method which will output the closest
I'm developing an ASP.NET forms webapplication using C#. I have a method which creates
Possible Duplicate: Casting vs using the ‘as’ keyword in the CLR Which method is
using a dataset,each row has a method called hasVersion() which implies to me it
I'm programming a simulation using repast simphony groovy API. There's a method count(myTurtles) which

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.