views:

445

answers:

2

I have a set of stored procedures that I am using to populate an ASP.Net CheckBoxList. When running this procedure from code as { CALL ProcedureName(params); } with the type set as stored procedure, I only seem to be getting a partial result back (i.e. many columns from the actual result are missing.)

If I copy the CommandText from the query (having used a breakpoint to get the exact text sent) and run it directly in Navicat (or any other MySQL GUI), I get all of the expected columns.

Here is the code that is not working:

using (OdbcCommand command = OdbcConnection.CreateCommand())
{
    command.CommandType = CommandType.StoredProcedure;
    command.CommandText = "{ " + string.Format(StoredProcedureCall, foundationId, fpids, "", "", "NULL", "2001/01/02", "2001/01/01", "*") +
                          " }";
    using (OdbcDataReader reader = command.ExecuteReader())
    {

        for (int i = 0; i < reader.FieldCount; i++)
        {
            columns.Add(reader.GetName(i));
        }
    }
}

If I change the code to the following however it starts to work (only adding the other using):

using (OdbcConnection)
using (OdbcCommand command = OdbcConnection.CreateCommand())
{
    command.CommandType = CommandType.StoredProcedure;
    command.CommandText = "{ " + string.Format(StoredProcedureCall, foundationId, fpids, "", "", "NULL", "2001/01/02", "2001/01/01", "*") +
                          " }";
    using (OdbcDataReader reader = command.ExecuteReader())
    {

        for (int i = 0; i < reader.FieldCount; i++)
        {
            columns.Add(reader.GetName(i));
        }
    }
}

What is going on here?

For reference here is the OdbcConnection property:

public static OdbcConnection OdbcConnection
{
    get
    {
        // If we have no connection or our connection isn't open
        if (null == odbcConnection || ConnectionState.Open != odbcConnection.State)
        {
            odbcConnection = new OdbcConnection(BaseAccess.DBConnectionString);
            odbcConnection.Open();
        }

        return odbcConnection;
    }
}
A: 

make sure your columns all have unique names.

longneck
Non uniquely named columns would throw a MySQL Exception. There is no such exception. Not to mention that the procedure forces unique column names as part of the code because they are all numbered (q1: blah, q2: blah, etc.)
mikeschuld
+3  A: 

This could be a bug in the Odbc MySql driver. Try using the ADO.NET driver. Also I would advice you against handling the connections manually inside static properties. Leave this task to ADO.NET which will effectively handle a connection pool:

using (var conn = new MySqlConnection(DBConnectionString))
using (var cmd = conn.CreateCommand())
{
    conn.Open();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "sp_name";

    cmd.Parameters.Add(new SqlParameter("@foundationId", foundationId));
    cmd.Parameters.Add(new SqlParameter("@fpids", fpids));
    ...

    using (var reader = cmd.ExecuteReader())
    {
        for (int i = 0; i < reader.FieldCount; i++)
        {
            columns.Add(reader.GetName(i));
        }
    }
}
Darin Dimitrov