1

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

3
  • What's wrong with what you have now? Looks fine to me. Does it not work correctly? Are you looking for easier-to-read code? Commented Nov 17, 2011 at 20:21
  • I am getting an object reference is required error. I was also getting some kind of unicode error when ToString() was at the end of SendFax(""); Commented Nov 17, 2011 at 20:24
  • @user1046434 for first error you are missing "}" , would you pls check it... Commented Nov 17, 2011 at 20:48

2 Answers 2

2

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());
}
Sign up to request clarification or add additional context in comments.

3 Comments

Ok I see what you are saying.
You are wrong in the 4th point, @user1046434 should reference System.Windows.Forms.dll and not System.web.forms.dll, and add the following using directive System.Windows.Forms; instead of System.Web.Forms; (It's even referenced in the link you added). You should edit your answer.
@Nacereddine: Yoiks.. my mind read one thing and wrote another! Thanks for the catch :)
2

I didn't understand what you're looking for in the 1st part of your question (as it looks fine), as for the second part you can use :

if(reader.HasRows){
    //code here
}

Edit :

Error 1: You must add a closing curly (}) brace to the main function before declaring the SendFax() function

Error 2: May have been cause by the first one. (since this function SendFax() was declared inside another function main()).

Error 3: Solution is available here

Error 4: Add this using directive using System.Windows.Forms;

The code should now looks someting like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
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";

            try
            {
                conn.Open();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            MySqlDataReader reader = command.ExecuteReader();

            if(reader.HasRows){
                while (reader.Read())
                {
                    SendFax(reader["title"].ToString(),
                            reader["filepath"].ToString(),
                            reader["name"].ToString(),
                            reader["name"].ToString());
                }
                reader.Close();
            }
        }       
        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); 
                }
            } 
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.