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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:58:01+00:00 2026-05-26T20:58:01+00:00

What is the correct way to do this? MySqlDataReader reader = command.ExecuteReader(); while (reader.Read())

  • 0

What is the correct way to do this?

MySqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
    //Console.WriteLine(reader["name"].ToString());
    SendFax(reader["title"].ToString(),
            reader["filepath"].ToString(),
            reader["name"].ToString(),
            reader["name"].ToString());
}

Also, how do you check to see if it returns any rows in an if statement?

Like:

if($numrows>"0")
{ 
    //execute code
}
else
{
    //do nothing
}

Full Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using FAXCOMLib;

namespace MysqlConnection1
{
class Program
{
    static void Main(string[] args)
    {
        string connString = "Server=localhost;Port=3306;Database=test;Uid=myuser;password=mypassword;";
        MySqlConnection conn = new MySqlConnection(connString);
        MySqlCommand command = conn.CreateCommand();
        command.CommandText = "SELECT * FROM firstcsharp";
        //command.CommandText = "UPDATE blah blah";
        //conn.Open();
        //conn.ExecuteNonQuery();
        //conn.Close();

        try
        {

            conn.Open();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        MySqlDataReader reader = command.ExecuteReader();
        if(reader.HasRows){

        while (reader.Read())
        {
            //Console.WriteLine(reader["name"].ToString());
            SendFax(reader["title"].ToString(),reader["filepath"].ToString(),reader["name"].ToString(),reader["name"].ToString());
        }

        }
        //Console.ReadLine();

        public void SendFax(string DocumentName, string FileName, string RecipientName, string FaxNumber) 
    { 
        if (FaxNumber != "") 
        { 
            try
            {
                FAXCOMLib.FaxServer faxServer = new FAXCOMLib.FaxServerClass(); 
                faxServer.Connect(Environment.MachineName); 

                
                FAXCOMLib.FaxDoc faxDoc = (FAXCOMLib.FaxDoc)faxServer.CreateDocument(FileName);  
            
                faxDoc.RecipientName = RecipientName;
                faxDoc.FaxNumber = FaxNumber; 

                faxDoc.DisplayName = DocumentName;
                

                int Response = faxDoc.Send(); 
                

                faxServer.Disconnect();

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

    
    }


}
}

Errors:

Error 1 } expected c:\documents and settings\bruser\my documents\visual studio 2010\Projects\FirstMysqlConnection\MysqlConnection1\Program.cs 41 14 MysqlConnection1

Error 2 An object reference is required for the non-static field, method, or property ‘MysqlConnection1.Program.SendFax(string, string, string, string)’ c:\documents and settings\bruser\my documents\visual studio 2010\Projects\FirstMysqlConnection\MysqlConnection1\Program.cs 38 17 MysqlConnection1

Error 3 Interop type ‘FAXCOMLib.FaxServerClass’ cannot be embedded. Use the applicable interface instead. c:\documents and settings\bruser\my documents\visual studio 2010\Projects\FirstMysqlConnection\MysqlConnection1\Program.cs 50 52 MysqlConnection1

Error 4 The name ‘MessageBox’ does not exist in the current context c:\documents and settings\bruser\my documents\visual studio 2010\Projects\FirstMysqlConnection\MysqlConnection1\Program.cs 68 25 MysqlConnection1

  • 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-26T20:58:02+00:00Added an answer on May 26, 2026 at 8:58 pm

    Edit: Regarding your error messages:

    1. This error indicates your braces are mismatched. I believe you are missing one at the end of Main method (add another } at the end). One way to help with this is to keep matching braces on the same vertical line so they are easy to see – tab them over to match. Also, you can use Edit -> Advanced -> Format Document feature in Visual Studio to help see the mismatch.
    2. You are trying to call SendFax, which is non-static, from Main, which is static. This is easily fixed by adding static to make it public *static* void SendFax.
    3. Might have to go to documentation/support on this 3rd-party library to figure out how you are supposed to reference it correctly.
    4. To use the MessageBox class you need to add a reference to System.Windows.Forms.dll library and also to the namespace System.Windows.Forms at the top.

    The code you have now looks like it should work. One thing to note is that if SendFax is a long running operation, you will probably want to either run it asynchronously, or download all the data from the database at once and process it afterward so that the connection can be closed. Connections to databases should be opened for as short as possible.

    MySqlDataAdapter adap = new MySqlDataAdapter(commandText, connectionString);
    DataTable dt = new DataTable();
    adap.Fill(dt);
    
    foreach (DataRow dr in dt.Rows) {
        string title = dr["title"] as string;
        string filepath = dr["filepath"] as string;
        string name = dr["name"] as string;
    
        SendFax(title, filepath, name, name);
    }
    

    Regarding your second question, using the above method it’s as simple as checking

    if (dt.Rows.Count > 0) ...
    

    Otherwise, you can do something like this:

    if (reader.Read()) {
        // there is data - now use do...while instead of while
        // because first row was consumed by if statement
        do {
            // process data
        } while (reader.Read());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the correct way to do this? For example, how would I change
Is this the correct way to do it? <a id=load href=# class=btn load onclick=request(this);
Is this the correct way to obtain the most negative double in Java? double
This may not be the correct way to use controllers, but I did notice
I'm not sure if this is the correct way to synchronize my ArrayList .
Is a regular expression the correct way of going about this? I have a
This is a simple question: Is this a correct way to get an integer
I'm still not sure this is the correct way to go about this, maybe
Is this the correct (or even a valid way) to use emums in Objective-C?
I seriously cannot find the correct way to do this. I have this method

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.