Jake H.
Jake H.

Reputation: 603

using MySql data in function/method

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

Upvotes: 1

Views: 294

Answers (2)

mellamokb
mellamokb

Reputation: 56769

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());
}

Upvotes: 2

Nasreddine
Nasreddine

Reputation: 37838

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); 
                }
            } 
        }
    }
}

Upvotes: 2

Related Questions