views:

100

answers:

1

Can someone post a "working" example of the Enterprise Application Data block, to call a SP.

All the sample code I find starts with

 // Create DataBase Instance
   Database db = DatabaseFactory.CreateDatabase();

But my code throws an object reference not set to an instance....

+2  A: 

Do you have your application configuration setup correctly?

Here is a quick example on calling a stored procedure via Enterprise Library Data Access Block:

using System;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
namespace MyTest
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var database = DatabaseFactory.CreateDatabase();
            var command = database.GetStoredProcCommand("SomeSP");
            using (IDataReader reader = database.ExecuteReader(command))
            {
                while (reader.Read())
                {
                    Console.WriteLine("X: {0} Y: {1} Z: {2}", 
                                    reader["SomeField"], 
                                    reader["AnothterField"], 
                                    reader["Enabled"]);
                }
            }
            Console.ReadLine();
        }
    }
}

And the corresponding app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
      <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    </configSections>
  <connectionStrings>
    <add
    name="DB"
    providerName="System.Data.SqlClient"
    connectionString="server=(local)\SQLEXPRESS;database=YourDB;Integrated Security=true" />
  </connectionStrings>
  <dataConfiguration defaultDatabase="DB"/>
</configuration>

I ran into that issue when I didn't have the defaultDatabase set to a corresponding connection string.

Mark
Perfectly sound answer, thank you...
JL