Page 1 of 1

SQL "Helper" issue

PostPosted: Wed Jun 03, 2015 10:34 am
by neil.interactit
I have an issue I can't quite nail, with a Help I am developing to ease direct database access ...

USAGE:
Code: Select all
var taxCodes = PluginHelper.SqlDataTable("SELECT TaxID, Description FROM TX_Main ORDER BY Description");
foreach (DataRow row in taxCodes.Rows) BASCodeUltraComboEditor.Items.Add(row["TaxID"].ToString(), row["Description"].ToString());


HELPER:
Code: Select all
public static class PluginHelper
{
    public static DataTable SqlDataTable(string sql, SqlParameter[] paramList = null)
    {
        DataTable table = new DataTable();
        //try
        //{
        JiwaFinancials.Jiwa.JiwaODBC.database databaseObject = JiwaFinancials.Jiwa.JiwaApplication.Manager.Instance.Database;
        using (SqlCommand cmd = new SqlCommand(sql, databaseObject.SQLConnection, databaseObject.SQLTransaction))
        {
            if (paramList != null) cmd.Parameters.AddRange(paramList);
            cmd.Connection.Open();
            table.Load(cmd.ExecuteReader());
            cmd.Connection.Close();
        }
        //}
        //catch { }
        return table;
    }
}

(handling commented to see error)

ERROR:
The connection was not closed. The connection's current state is open. Module: TryOpenConnection

Can you point me in the right direction?

Cheers,
Neil

Re: SQL "Helper" issue

PostPosted: Wed Jun 03, 2015 10:47 am
by Mike.Sheen
Hi Neil,

You're closing the Jiwa connection:

Code: Select all
cmd.Connection.Close();


Don't do that.

Mike

Re: SQL "Helper" issue

PostPosted: Wed Jun 03, 2015 10:55 am
by neil.interactit
Thanks Mike,

I actually had just added that line in response to the "not closed" error. I have removed it again, but still get the same error.

Cheers,
Neil

Re: SQL "Helper" issue  Topic is solved

PostPosted: Wed Jun 03, 2015 10:58 am
by Scott.Pearce
You shouldn't be opening the connection either.

Re: SQL "Helper" issue

PostPosted: Wed Jun 03, 2015 11:26 am
by neil.interactit
That's got it ... do you need my postal address to forward on a dunce hat?

Re: SQL "Helper" issue

PostPosted: Wed Jun 03, 2015 11:31 am
by Scott.Pearce
lol