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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:36:19+00:00 2026-05-26T19:36:19+00:00

I am attempting to read a MySQL database from my C# project using the

  • 0

I am attempting to read a MySQL database from my C# project using the MySQL drivers for .net off the MySQL site.

Though I did a bit of research on this (including this), I am still flummoxed why this is happening. I later ran a spike and I still get the same error. (Prior to running this I populated the database with some default values.) Here’s the spike code in toto.

class Program {
    static void Main (string[] args) {
        Console.WriteLine (GetUserAge ("john")); // o/p's -1
    }

    static int GetUserAge (string username) {
        string sql = "select age from users where name=@username";

        int val = -1;
        try {
            using (MySqlConnection cnn = GetConnectionForReading ()) {
                cnn.Open ();
                MySqlCommand myCommand = new MySqlCommand (sql, cnn);
                myCommand.Parameters.AddWithValue ("@username", username);

                using (MySqlDataReader reader = myCommand.ExecuteReader ()) {
                    DataTable dt = new DataTable ();
                    dt.Load (reader);

                    if (reader.Read ()) {
                        val = reader.GetInt32 (0);
                    }
                }
            }
        } catch (Exception ex) {
            Console.WriteLine (ex.Message);
        } finally {
        }

        return val;
    }

    private static MySqlConnection GetConnectionForReading () {
        string conStr = "Data Source=localhost;Database=MyTestDB;User ID=testuser;Password=password";
        return new MySqlConnection (conStr);
    }
}

The code above gives me the exception: “Invalid attempt to Read when reader is closed.”

Later I modified the if-condition like so:

if (reader.HasRows && reader.Read ()) {
    val = reader.GetInt32 (0);
}

And now the o/p is -1. (The data’s in there in the table.) If for some reason the result set had zero rows, the reader should not have got into the if-block in the first place. I mean, the whole point of the Read() method is to check if there are any rows in the result set in the first place.

At my wit’s end here… just cannot figure out where I’m going wrong.

Thank you for your 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-26T19:36:19+00:00Added an answer on May 26, 2026 at 7:36 pm

    I think using DataTable.Load will “consume” the reader and, at the very least, position it at the end. It may even account for the closed connection (but I’m just guessing here). What if you remove that line? I don’t think it makes any sense here.

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

Sidebar

Related Questions

I am attempting to read out a value from the database in pence. For
I am using PHP to read a text field (DESCRIPTION) from MYSQL and prepare
I'm attempting to use TinyXML to read and save from memory, instead of only
I'm currently attempting to read information about an object from the web.config file and
I'm attempting to read a value from a registry entry with Powershell. This is
I am currently attempting to read in Hex values from a text file. There
I am attempting to read the results from a SQL query into a List(Of)
I am attempting to read a json message using the json.framework. The message is
I'm attempting to read a PDF from a UNC path, i.e. \10.32.16.24\repositories\repository0001\VOL00001\ktappb01_024655001_0.PDF My virtual
I have data stored in a SQL database that I'm attempting to read into

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.