Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8526833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:23:08+00:00 2026-06-11T08:23:08+00:00

I’m trying to build a class that will be responsible for all operations on

  • 0

I’m trying to build a class that will be responsible for all operations on database.

Using singleton pattern from here: http://codebender.denniland.com/a-singleton-base-class-to-implement-the-singleton-pattern-in-c/

I’ve build a class like so:

class DB : SingletonBase<DB>
    {

        public static readonly string SqlConnectionString  = @"Data Source=MYDB;Initial Catalog=PRODUCTS;Integrated Security=True;Asynchronous Processing=true;";

        private DB()
        {
        }

        public void loadData()
        {
            SqlConnection conn = new SqlConnection(SqlConnectionString);
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "STATISTICS_1";

            cmd.Connection = conn;
            conn.Open();
            IAsyncResult result = cmd.BeginExecuteReader(new AsyncCallback(HandleCallback), cmd, CommandBehavior.CloseConnection);
        }

        private void HandleCallback(IAsyncResult result)
        {
            SqlDataReader dr;
            SqlCommand _this = (SqlCommand)result.AsyncState;

            if (result.IsCompleted)
            {
                dr = _this.EndExecuteReader(result);
            }
            else
                dr = null;

            DataTable dt = new DataTable();
            dt.Load(dr);
            dr.Close();
            MessageBox.Show("loaded");
        }
    }

In my main class I’m using this like so:

    private void loadStatistics(object sender, EventArgs e)
    {
        showStatus("loading data");
        DB.Instance.loadData();
    }

But this will give me only my message box.

What I would like to do is to declare function in my main class that will be called after SQL query return something.

I think that the best way would by using events, but I don’t know how to do that proper way.

I would like to do something like this in my main class:

    private void loadCompleted(string msg)
    {
        MessageBox.Show(msg);
    }

    private void loadStatistics(object sender, EventArgs e)
    {
        showStatus("loading data");
        DB.Instance.loadData(loadCompleted);
    }

So that I can specify function that will be called after SQL call is finished.

I don’t know if this is the best way of doing this, so any comments, suggestions and solutions are welcome.

What I would like to achieve is to have one class responsible for calling SQL asynchronously and passing data to other functions that will process it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T08:23:10+00:00Added an answer on June 11, 2026 at 8:23 am
    public delegate void NotifyCallback(string message);    
    
    public class ClassWithCommandAndCallback 
    {
      public SqlCommand Sql;
      public NotifyCallback Callback;
    }
    
    public void loadData(NotifyCallback callback)
    {
      ClassWithCommandAndCallback ar = new ClassWithCommandAndCallback();
      ar.Sql = cmd;
      ar.Callback = callback;
      IAsyncResult result = cmd.BeginExecuteReader(new AsyncCallback(HandleCallback), ar, CommandBehavior.CloseConnection);
    }
    
    private void HandleCallback(IAsyncResult result)
    {
      ClassWithCommandAndCallback ar = (ClassWithCommandAndCallback)result.AsyncState;
    
      ar.Callback("loaded (SQL was: "+ar.Sql+")");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I have a view passing on information from a database: def serve_article(request, id): served_article

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.