Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello Friends,

I have the next problem.
I have a project (DBConnecting.DataObjects.DataObjects) that is the communication between my windows form app and my Sql Server 2008 Database.

In my 2nd project: a Windows form (that references DBConnecting.DataObjects.DataObjects) that calls the Async table method to load a datatable object asynchronous.

C#
string query = "select * from [dbo].[FactInternetSales]";
            DBConnecting.DataObjects.DataObjects.AsyncDataTableStart(query, null, CommandType.Text, null);

In the DBConnecting project the AsyncDataTableStart gets called and this method calls the method GetAsyncDataTableEnd

C#
public static IAsyncResult AsyncDataTableStart(String Query, List<sqlparameter> sqlParameters, CommandType sqlCommandType, String ConnectionString)
        {
            SqlCommand command = Open(Query, sqlParameters, sqlCommandType, ConnectionString);
            return command.BeginExecuteReader(GetAsyncDataTableEnd, command, CommandBehavior.CloseConnection);
        }

        public static void GetAsyncDataTableEnd(IAsyncResult myResult)
        {
            SqlCommand command = null;
            SqlDataReader dr = null;
            
            DateTime nu = DateTime.Now;
            DataTable myTable = new DataTable();
            try
            {
                command = (SqlCommand)myResult.AsyncState;
                dr = command.EndExecuteReader(myResult);
                myTable.Load(dr);
                
            }
            catch
            {
            }
            finally
            {
                if (dr != null)
                    dr.Close();
                if (command != null && command.Connection.State != ConnectionState.Closed)
                    command.Connection.Close();
            }
        }
</sqlparameter>

So I have a DataTable object loaded with data, but How do I return this table to the windowsform?
In the DBConnecting.DataObjects.DataObjects class I would like to have a delegate that when the windows form implements this delegate the datatable is returned so it can be set as DataSource for a DataGridView.

Please help me with some examples
Posted
Updated 19-Aug-11 4:44am
v2
Comments
Philippe Mori 19-Aug-11 16:12pm    
Having 2 projects does not matters much... as long as type like a delegate that are used by both are declared in the first project.

1 solution

This[^] article contains information on how to pass information back using IAsyncResult.

Create the DataTableEventArgs in your source project
C#
public class DataTableEventArgs : System.EventArgs {
    DataTable dataTable;
    public DataTable DataTableInfo {
        get {
            return dataTable;
        }
    }

    public DataTableEventArgs(DataTable dataTable) {
        this.dataTable= dataTable;
    }
}

Declare the delegate in your source project
C#
public delegate void DataTableEventHandler(object sender, DataTableEventArgs e);


Declare the event in the class that returns the DataTable
C#
public event DataTableEventHandler ReturnDataTable;


Raise the event where you want to send the DataTable
C#
if (ReturnDataTable != null) {
    DataTable dataTable = BuildDataTable();
    ReturnDataTable(this, dataTable);
}


Now, in your receiver class that must receive the DataTable
C#
private void Init() {
    //Bind your method to the event
    MyDataSource src = new MyDataSource();
    src.ReturnDataTable += new DataTableEventHandler(HandleDataTable);
}

private void HandleDataTable(object sender, DataTableEventArgs e) {
    DataTable dataTable = e.DataTableInfo;
    //Use dataTable here
}
 
Share this answer
 
v2
Comments
Herman<T>.Instance 19-Aug-11 11:00am    
my current declaration of the delegate looks like:
public delegate DataTable ReturnTable(DataTable theTable);

how should I do this (sorry no experience with delegates)
[no name] 19-Aug-11 15:24pm    
See my updated answer.
Herman<T>.Instance 19-Aug-11 16:05pm    
I am gonna try this on monday and if it works I'll take a deep bow for you
Herman<T>.Instance 20-Aug-11 7:58am    
it fails on ReturnDataTable(this, dataTable);
Error 2 Argument 2: cannot convert from 'System.Data.DataTable' to 'DBConnecting.EventHandlers.DataTableEventArgs'

my source:

public void GetAsyncDataTableEnd(IAsyncResult myResult)
{
SqlCommand command = null;
SqlDataReader dr = null;

DateTime nu = DateTime.Now;
DataTable myTable = new DataTable();
try
{
command = (SqlCommand)myResult.AsyncState;
dr = command.EndExecuteReader(myResult);
if (ReturnDataTable != null)
{
myTable.Load(dr);
ReturnDataTable(this, myTable);
}
}
catch
{
}
finally
{
if (dr != null)
dr.Close();
Close(command);
}
}

(need full project to see what I have done?)
[no name] 20-Aug-11 14:14pm    
Do this:
DataTableEventArgs dtArgs = new DataTableEventArgs(myTable);
ReturnDataTable(this, dtArgs);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900