CLASS(DLL) error message callback - visual c#

hi, i'm making a universal SQL dll for my current and future projects.

sofar i'm good, but i need a methode to pass error strings to the main class.
instead of a MessageBox.

i could probaly link the main class in the sql class.
but that doesn't make it universal.

 event based thing or something.

when i execute something from the MySQL class and a error occurs.
i would like to send a 'async' message to the main class Log  'richttextbox'.

i have been reading  my visual c#  3.0 5th book and look some examples.
but it isn't very clear.






using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;
 
 
namespace MySQL_Utility
{
    public class MySQL
    {
        //store local vars
        private string _driver; // store driver
        private string _host; // store host 
        private string _port; //  store port.
        private string _database; // database
        private string _username; // login name
        private string _password; // password
 
        private System.Data.Odbc.OdbcConnection OdbcCon;
        private System.Data.Odbc.OdbcCommand OdbcCom;
        private System.Data.Odbc.OdbcDataReader OdbcDR;
        private string ConStr;
 
 
        public MySQL(string driver, string host, string port, string username, string password, string database)
        {
            //seting up vars
            _driver     = driver;
            _host       = host;
            _port       = port;
            _database   = database;
            _username   = username;
            _password   = password;
        }
 
        private void Connect()
        {
            if (_driver != "" && 
            _host != "" && 
            _port != "" && 
            _database != "" && 
            _username != "" && 
            _password != "")
            {
                ConStr = "DRIVER={" + _driver
                        + "};SERVER=" + _host
                        + ";PORT=" + _port
                        + ";DATABASE=" + _database
                        + ";UID=" + _username
                        + ";PWD=" + _password
                        + ";OPTION=3";
                OdbcCon = new System.Data.Odbc.OdbcConnection(ConStr);
                try
                {
                    ShowInfo("Openning connection...");
                    if (OdbcCon.State == ConnectionState.Closed)
                    {
                        OdbcCon.Open();
                    }
                    ShowInfo("Connection opened");
                }
                catch (System.Data.Odbc.OdbcException Ex)
                {
                    MessageBox.Show("Could not access the database.\r\nPlease make sure you completed the fields with the correct information and try again.\r\n\r\nMore details:\r\n" + Ex.Message, "Database connection error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ShowInfo("Could not access the database.\r\nPlease make sure you completed the fields with the correct information and try again.\r\n\r\nMore details:\r\n" + Ex.Message);
                }
            }
            else
            {
                MessageBox.Show("Missing, please check the connection details", "Form field error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                ShowInfo("Missing, please check the connection details");
            }
        }
 
        private void Disconnect()
        {
            if (OdbcCon.State == ConnectionState.Open)
            {
                OdbcCon.Close();
            }
        }
 
        public void Query_database(string mode, string query)
        {
            if ((mode.Contains("SELECT")) || (mode.Contains("SHOW")))
            {
                try
                {
                    OdbcCom = new System.Data.Odbc.OdbcCommand(query, OdbcCon);
                    OdbcDR = OdbcCom.ExecuteReader();
                }
                catch (System.Data.Odbc.OdbcException Ex)
                {
                    MessageBox.Show("" + Ex.Message, "query error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                try
                {
                    OdbcCom = new System.Data.Odbc.OdbcCommand(query, OdbcCon);
                    OdbcCom.ExecuteNonQuery();
                }
                catch (System.Data.Odbc.OdbcException Ex)
                {
                    MessageBox.Show("" + Ex.Message, "query error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
 
        public void ShowInfo(string info)
        {
 
 
        }
    }
}

Open in new window

Natural_DemonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anurag ThakurTechnical ManagerCommented:
try something like this
use the ref object in the method call and if an exception comes fill that object and the error message should be available to you in the main calling class
        public void Query_database(string mode, string query, ref string dbMessage)
        {
            if ((mode.Contains("SELECT")) || (mode.Contains("SHOW")))
            {
                try
                {
                    OdbcCom = new System.Data.Odbc.OdbcCommand(query, OdbcCon);
                    OdbcDR = OdbcCom.ExecuteReader();
                }
                catch (System.Data.Odbc.OdbcException Ex)
                {
                    dbMessage = Ex.Message;
                }
            }
            else
            {
                try
                {
                    OdbcCom = new System.Data.Odbc.OdbcCommand(query, OdbcCon);
                    OdbcCom.ExecuteNonQuery();
                }
                catch (System.Data.Odbc.OdbcException Ex)
                {
                    dbMessage = Ex.Message;
                }
            }
        }

Open in new window

0
Natural_DemonAuthor Commented:
more input please, i'm not expert on references and objects.
thnk for your reply
0
Anurag ThakurTechnical ManagerCommented:
before calling your method from mySql Class object you will have to do the following steps

string dbMessage = string.Empty;
mySql.Query_database(modeString, queryString, ref dbMessage);

after the call is over you  have to just check if the string dbMessage is empty of not
if not then there was an exception you might want to handle that else just continue with the logic
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.