Having trouble connecting with EntLib 3.1 Data App Block

Topics: User Discussion
Jan 29, 2009 at 3:13 PM
Hi,

I'm pretty sure I am missing something simple.

I have four 2.0 projects. 3 web and 1 c#. The c# project is a data layer where the EntLib Data App lives.

The web apps will connect through the Data App.

The error I am getting is: Object reference not set to instance of an object and it points to the CreateDatabase line.

Below is the code for the c# project

 

public class DbGateway

 

 

{

 

public DataTable ExecuteSqlSP(string spname)

 

{

 

 

Database db = DatabaseFactory.CreateDatabase();

 

 

 

using (SqlCommand sqlCommand = new SqlCommand())

 

{

db.GetStoredProcCommand(spname);

 

 

using (DataSet myds = new DataSet())

 

{

db.ExecuteDataSet(sqlCommand);

 

 

return myds.Tables[0];

 

}

}

}

 

 

public DataTable ExecuteSqlSP(string spname, SqlParameter[] parms)

 

{

 

 

SqlDatabase db = DatabaseFactory.CreateDatabase() as SqlDatabase;

 

 

 

using (SqlCommand sqlCommand = new SqlCommand())

 

{

db.GetStoredProcCommand(spname, parms);

 

 

using (DataSet myds = new DataSet())

 

{

db.ExecuteDataSet(sqlCommand);

 

 

return myds.Tables[0];

 

}

}

}

}

 

Below is the source code from the calling web app method:

protected

 

void LoginButton_Click(object sender, EventArgs e)

 

{

 

SqlParameter[] parms = new SqlParameter[2];

 

parms[0] =

new SqlParameter("Username", SqlDbType.VarChar);

 

parms[0].Value = txtUserName.Text.ToString();

parms[1] =

new SqlParameter("Password", SqlDbType.VarChar);

 

parms[1].Value = txtPassword.Text.ToString();

 

DataTable dtAdministrator = Gateway.ExecuteSqlSP("GetAdministrator", parms);

 

 

if (dtAdministrator.Rows.Count > 0)

 

{

litStatus.Text =

"User has been verified";

 

}

 

else

 

{

litStatus.Text =

"User does not exist";

 

}

}


Jan 29, 2009 at 3:18 PM
It may help if I post the app.config source too!

<?

 

xml version="1.0" encoding="utf-8"?>

 

<

 

configuration>

 

<

 

configSections>

 

<

 

section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />

 

</

 

configSections>

 

<

 

dataConfiguration defaultDatabase="SqlServer" />

 

<

 

connectionStrings>

 

<

 

add name="SqlServer" connectionString="Database=studentparent;Server=8669AD0TEL01;Integrated Security=SSPI"

 

 

 

providerName="System.Data.SqlClient" />

 

</

 

connectionStrings>

 

</

 

configuration>

 

Coordinator
Mar 10, 2009 at 10:30 AM
Hi Sutorious,

A couple of things to consider:

1. The problem may be permissions.  You are using your default connection "sqlserver" which is set up for SSPI, are you sure the authenticated account using your application will have the correct permissions?  If it is a web app then you will be using an account like ASPNET or Network Service, depending on your version of IIS.

2. I notice that you are using specific ADO.NET objects (e.g. SqlCommand) rather then the generic ones (e.g. DbCommand), this means that you are not be getting the maximum benefit out of the DAAB which is designed so that your applications don't have to know about the underlying database. Use DbCommand instead and then use your Database object to execute this command for reading or writing to the database.

Cheers...   Steve