Link to home
Start Free TrialLog in
Avatar of Natural_Demon
Natural_DemonFlag for Spain

asked on

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

Avatar of Anurag Thakur
Anurag Thakur
Flag of India image

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

Avatar of Natural_Demon

ASKER

more input please, i'm not expert on references and objects.
thnk for your reply
ASKER CERTIFIED SOLUTION
Avatar of Anurag Thakur
Anurag Thakur
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial