Getting "Connection property has not been initialized" in DAAB

Topics: Data Access Application Block
Nov 13, 2009 at 10:50 PM

I have EntLib 3.1 May 2007 in asp.net 2.0 scenario.  I am developing on windows xp sp3 with .net 2.0 sp1.

I am getting InvalidOperationException with message text "Connection property has not been intialized" when doing the following

Database db = DatabaseFactory.CreateDatabase();

DbCommand dbCommand = db.GetSqlStringCommand("select * from products");

Using (IDataReader reader = dbCommand.ExecuteReader())   <--- this is where it blows up

{  ... }

I've attempted another variation to ensure that it's not my entlib config by replacing the first line with this

SqlDatabase db = new SqlDatabase(connectionString);

I get the same error.  I've verified that connectionString is correct.

Nov 14, 2009 at 6:39 AM

Rroman81

From: rroman81 [mailto:notifications@codeplex.com]
Sent: Friday, November 13, 2009 6:50 PM
To: mapeiris@hotmail.com
Subject: Getting "Connection property has not been initialized" in DAAB [entlib:75091]

From: rroman81

Set a break point  on the db.command.

Before that get executed check (watch) the properties of db connection string . You should see your connection string initialized. If not Check the configuration…

Anthony

I have EntLib 3.1 May 2007 in asp.net 2.0 scenario. I am developing on windows xp sp3 with .net 2.0 sp1.

I am getting InvalidOperationException with message text "Connection property has not been intialized" when doing the following

Database db = DatabaseFactory.CreateDatabase();

DbCommand dbCommand = db.GetSqlStringCommand("select * from products");

Using (IDataReader reader = dbCommand.ExecuteReader()) <--- this is where it blows up

{ ... }

I've attempted another variation to ensure that it's not my entlib config by replacing the first line with this

SqlDatabase db = new SqlDatabase(connectionString);

I get the same error. I've verified that connectionString is correct.

Read the full discussion online.

To add a post to this discussion, reply to this email (entlib@discussions.codeplex.com)

To start a new discussion for this project, email entlib@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Nov 15, 2009 at 4:40 PM

Can you check if the Default Database on the property is set to the desired ConnectionString. Click on the Database Application Block and you will find the DefaultDatabase Property on the properties window.

Thanks and Regards

Abishek.R.Srikaanth

Nov 15, 2009 at 5:33 PM

Hi,

Can you try using the Database.ExecuteReader(command).

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

Nov 16, 2009 at 5:10 AM

Hi,

Please try my suggestion above, I verfied that it works. I also repro your error, so if you want to continue with your approach that you execute your reader against the command, you can try this code:

This manually assigns and open the connection that the command will use.

            Database db = DatabaseFactory.CreateDatabase();

            DbCommand cmd = db.GetStoredProcCommand("SelectAllPerson");
            DbConnection connection = db.CreateConnection();
            connection.Open();
            cmd.Connection = connection;
            IList<Person> persons = new List<Person>();

            using (IDataReader reader = db.ExecuteReader(cmd))
            {
                while (reader.Read())
                {
                    string name = reader["Name"].ToString();
                    int age = Convert.ToInt32(reader["Age"]);
                    persons.Add(new Person(name, age));
                }
            }

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Nov 16, 2009 at 6:36 AM

Abishek, yes the default database property is set.  That was not the issue.

Valiant, yes. Your solution works perfectly if I execute against Database instead of the DbCommand object .  I'll make a note that  Database.GetSqlStringCommand does not create a Command that has the connection property set to Database's connection property.  In fact in debugger, that property was null after GetSqlStringCommand call.  It felt a bit counter intuitive, though.