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