views:

18

answers:

2

Hi,

There are several queries to be performed which return the DataTable object. In order to speed up the development I created a private method which should return the dataset by taking the query string as an argument.

the method is the following:

private DataTable getDataTable(string query)
    {
        DataTable dt = new DataTable();
        SqlDataAdapter DA = new SqlDataAdapter(query, conn);
        try
        {
            iStatusIndicator.SetBusy(true);
            iStatusIndicator.SetStatus("executing query" + query);
            DA.Fill(dt);
        }
        catch (Exception ex)
        {
            ...
        }
        iStatusIndicator.SetBusy(false);
        iStatusIndicator.SetStatus("");
        return dt;
    }

the procedure doesn't throw an exception but the DataTable dt is always null. I tried to run a query string directly in sql command prompt and it returns the data as expected so I don't know what could be the problem.

I would be very thankful if anyone of you explained the cause, suggested a fix or a better method for returning DataTables by receiving query strings.

Thank you

A: 
private static DataSet SelectRows(DataSet dataset,
    string connectionString,string queryString) 
{
    using (SqlConnection connection = 
        new SqlConnection(connectionString))
    {
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = new SqlCommand(
            queryString, connection);
        adapter.Fill(dataset);
        return dataset;
    }
}
Romain Hippeau
A: 

What's in your catch block? By any chance is there something that does a return or exit sub? That seems to be the only way that I can see that this function would return NOthing (Ie. Your function is not ever reaching the "return dt;" line

MikeG
The method executes without exception and it returns dt which is null.
niko
Hmm that doesn't seem possible! Have you stepped through this code with the debugger? Also can you post the code that is in the catrch block... i'm curious!
MikeG