[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

CLASS(DLL) error message callback - visual c#

Posted on 2008-11-15
3
Medium Priority
?
365 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:Natural_Demon
  • 2
3 Comments
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22969948
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
 

Author Comment

by:Natural_Demon
ID: 22971242
more input please, i'm not expert on references and objects.
thnk for your reply
0
 
LVL 26

Accepted Solution

by:
Anurag Thakur earned 1500 total points
ID: 22973163
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Loops Section Overview
Suggested Courses
Course of the Month19 days, 9 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question