locked
EL3 Invalid Operation Exception with Data Access Block RRS feed

  • Question

  • Hello,

    I have a unit test, which does this and works using the new EL 3 block:

    [

    Test]

    public

    void TestUsingELNormally()

    {

    Database database = DatabaseFactory.CreateDatabase();

    DbCommand command = database.GetStoredProcCommand("mains_Test");

    database.AddInParameter(command,

    "TableName", DbType.String, "mains_Events");

    database.AddInParameter(command,

    "TableSchema", DbType.String, "dbo");

    DataSet results = database.ExecuteDataSet(command);

    Assert.IsNotNull(results);

    Assert.AreEqual(1, results.Tables.Count);

    }

    This works, however the following doesn't, and I get the error you see below.  I create an abstracted array of database parameter, which is not passed directly into the database but the properties are passed along to a method that calls AddInParameter, AddOutParameter, etc.  I don't know why, when broken up instead of all together, I get an error. 

    [

    Test]

    public

    void TestExecutingDataTable()

    {

    List<DatabaseParameter> parameters = new List<DatabaseParameter>();

    parameters.Add(

    new DatabaseParameter("TableName", DbType.String, "mains_Events"));

    parameters.Add(

    new DatabaseParameter("TableSchema", DbType.String, "dbo"));

    //Results in exception

    DataTable results = base.ExecuteDataTable("dbo.mains_Test", parameters.ToArray());

    Assert.IsNotNull(results);

    Assert.AreEqual(9, results.Rows.Count);

    }

    //Method begin called by method above

    protected

    DataTable ExecuteDataTable(string commandName, DatabaseParameter[] parameters)

    {

    Database database = DatabaseFactory.CreateDatabase();

    DbCommand command = database.GetStoredProcCommand(commandName);

    DatabaseParameterHelper.AssignParametersToDatabase(database, command, parameters);

    return database.ExecuteDataSet(commandName).Tables[0].Copy();

    }

    internal

    static void AssignParametersToDatabase(Database database, DbCommand command, DatabaseParameter[] parameters)

    {

    foreach (DatabaseParameter parameter in parameters)

    {

    if (parameter != null && !string.IsNullOrEmpty(parameter.Name))

    {

    if (parameter.Direction == ParameterDirection.Input)

    database.AddInParameter(command, parameter.Name, parameter.Type, parameter.Value);

    else if (parameter.Direction == ParameterDirection.Output)

    database.AddOutParameter(command, parameter.Name, parameter.Type, -1);

    else if (parameter.Direction == ParameterDirection.ReturnValue)

    database.AddParameter(command, parameter.Name, parameter.Type, parameter.Direction,

    null, DataRowVersion.Default, parameter.Value);

    else

    throw new NotImplementedException();

    }

    }

    }

    Nucleo.Data.BaseDataGatewayTest.TestExecutingDataTable : System.InvalidOperationException : The number of parameters does not match number of values for stored procedure.

       at Microsoft.Practices.EnterpriseLibrary.Data.Database.GetStoredProcCommand(String storedProcedureName, Object[] parameterValues)
       at Microsoft.Practices.EnterpriseLibrary.Data.Database.ExecuteDataSet(String storedProcedureName, Object[] parameterValues)
       at Nucleo.Data.BaseDataGateway.ExecuteDataTable(String commandName, DatabaseParameter[] parameters) in C:\Documents and Settings\Brian\My Documents\Visual Studio 2005\Projects\NucleoNet\Nucleo.Data\BaseDataGateway.cs:line 17
       at Nucleo.Data.BaseDataGatewayTest.TestExecutingDataTable() in C:\Documents and Settings\Brian\My Documents\Visual Studio 2005\Projects\NucleoNet\Nucleo.Tests\Data\BaseDataGatewayTest.cs:line 21

    I don't understand what the difference is.

    Thanks.

    Saturday, April 28, 2007 7:51 PM