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

  • SEARCH
  • Home
  • 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 8540815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:41:17+00:00 2026-06-11T11:41:17+00:00

I have a class that is responsible for all my Database actions, in general

  • 0

I have a class that is responsible for all my Database actions, in general it calls stored procedures.

I’ve created 2 delegates in my class, one responsible for positive response (server returned OK for example) and second for all error handling.

public delegate void Part1_Callback(string message);
public delegate void Part2_Callback(DataTable dt);
public delegate void Part3_Callback(DataTable dt, int x, int y);
public delegate void ErrorHandler(string message);

I call all methods asynch like shown in my previous question: C# asynch SQL inside singleton and delegates

I have a problem when I need my delegate to return a different type of data.

For example my first method returns a String, second a DataTable, and third a DataTable and 2 ints.

Right now for every method I must create class that holds parameters:

public class Part1_CommandAndCallback
{
    public SqlCommand Sql;
    public Part1_Callback Callback;
    public ErrorHandler Error;
}

public class Part2_CommandAndCallback
{
    public SqlCommand Sql;
    public Part2_Callback Callback;
    public ErrorHandler Error;
}

public class Part3_CommandAndCallback
{
    public SqlCommand Sql;
    public Part3_Callback Callback;
    public ErrorHandler Error;
}

Is it possible to create one generic delegate so that I will be able to have one delegate for responses and one class for parameters?

This way I’ll be able to more easily control my code.

I found article on codeproject: http://www.codeproject.com/Articles/192027/Delegates-101-Part-III-Generic-Delegates but I don’t know how to use this in my case :/

Should I declare my delegate like this:

delegate void MyDelegate (params object[] params);

or:

public delegate void MyDelegate2<T>(T param1);

but this way I’ll be able to pass only one parameter, I won’t be able to use same delegate for 3 parameters.

Which solution is better?
I would like to have one generic delegate that will be able to take one-to-three parameters with different types.

Can this be done?

EDIT:

I’ll try to show my scenario:

In my main form class I’m calling my DB class like so:

    private void form1_Enter(object sender, EventArgs e)
    {
        showStatus("Loading statistics...");
        DB.Instance.Part1(part1_ok,ErrorHandler);
        DB.Instance.Part2(part2_ok, ErrorHandler);
    }
    private void ErrorHandler(string msg)
    {
        hideStatus();
        //viewStack1.InvokeIfRequired(c => { c.moveToFirst(); });
        MessageBox.Show(msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

    private void part1_ok(string msg)
    {
        MessageBox.Show(msg);

    }

    private void part2_ok(DataTable dt)
    {
        dataGridView1.InvokeIfRequired(c =>
        {
            c.DataSource = dt;
        });
    }

My DB class looks like so:

public delegate void Part1_Callback(string message);
public delegate void Part2_Callback(DataTable dt);
public delegate void Part3_Callback(DataTable dt, int x, int y);
public delegate void ErrorHandler(string message);

public class Part1_CommandAndCallback
{
    public SqlCommand Sql;
    public Part1_Callback Callback;
    public ErrorHandler Error;
}

public class Part2_CommandAndCallback
{
    public SqlCommand Sql;
    public Part2_Callback Callback;
    public ErrorHandler Error;
}

public class Part3_CommandAndCallback
{
    public SqlCommand Sql;
    public Part3_Callback Callback;
    public ErrorHandler Error;
}

class DB : SingletonBase<DB>
{

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

    private DB()
    {
    }

    public void Part1(Part1_Callback callback, ErrorHandler error)
    {
        SqlConnection conn = new SqlConnection(SqlConnectionString);
        SqlCommand cmd = conn.CreateCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "Part1";

        try
        {
            conn.Open();
        }
        catch (Exception ex)
        {
            error(ex.ToString());
            return;
        }

        Part1_CommandAndCallback ar = new Part1_CommandAndCallback() { Callback = callback, Error = error, Sql = cmd };
        IAsyncResult result = cmd.BeginExecuteReader(new AsyncCallback(Part1_Handler), ar, CommandBehavior.CloseConnection);
    }

    private void Part1_Handler(IAsyncResult result)
    {
        string stats = string.Empty;
        Part1_CommandAndCallback ar = (Part1_CommandAndCallback)result.AsyncState;
        SqlDataReader dr;

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

        while (dr.Read())
        {
            stats += dr[0].ToString() + Environment.NewLine;
        }
        dr.NextResult();

        while (dr.Read())//problem is here
        {
            stats += dr[0].ToString() + " - " + dr[1].ToString() +Environment.NewLine;
        }
        dr.Close();
        ar.Callback(stats);
    }

    public void Part2(Part2_Callback callback, ErrorHandler error)
    {
        SqlConnection conn = new SqlConnection(SqlConnectionString);
        SqlCommand cmd = conn.CreateCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "Part2";

        try
        {
            conn.Open();
        }
        catch (Exception ex)
        {
            error(ex.ToString());
            return;
        }

        Part2_CommandAndCallback ar = new Part2_CommandAndCallback() { Callback = callback, Error = error, Sql = cmd };
        IAsyncResult result = cmd.BeginExecuteReader(new AsyncCallback(Part2_Handler), ar, CommandBehavior.CloseConnection);
    }

    private void Part2_Handler(IAsyncResult result)
    {
        DataTable dt = new DataTable();
        Part2_CommandAndCallback ar = (Part2_CommandAndCallback)result.AsyncState;
        SqlDataReader dr;

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

        dt.Load(dr);
        dr.Close();
        ar.Callback(dt);
    }
}

My idea was to use my singleton DB controller in many forms at the same time. So in first form I’ll see some stats that will auto refresh, and if I’ll like then is second form I’ll be able to see some different stats that I’ll be able to refresh on button click.

  • 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-11T11:41:18+00:00Added an answer on June 11, 2026 at 11:41 am

    It looks like what you need is to create a generic CommandAndCallback class:

    public class CommandAndCallback<TCallback>
    {
        public SqlCommand Sql;
        public TCallback Callback;
        public ErrorHandler Error;
    }
    

    And, for example, where you previously used Part3_CommandAndCallback, you would now use CommandAndCallback<Part3_Callback> or even CommandAndCallback<Action<DataTable, int, int>>, if you didn’t want to create a delegate type for each part either.

    Also, public fields are generally discouraged, so you might consider changing them to autoproperties:

    public class CommandAndCallback<TCallback>
    {
        public SqlCommand Sql { get; set; }
        public TCallback Callback { get; set; }
        public ErrorHandler Error { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my application I have class that is responsible for all database actions. It
I have a class that is responsible for execute stored procedure, it was working
I'm implementing a class that is responsible for all my HTTP requests from the
I have a class implemented in C++ that's responsible for the arithmetic computation of
I'm trying to build a class that will be responsible for all operations on
I have class that extend FragmentActivity in it I add fragment to layout as
I have class that looks like this: class A { public: class variables_map vm
I have class that represents users. Users are divided into two groups with different
I have class grades that I need to check if a specific grade is
I have class A such that: class A { static int i; A(); f1();

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.