views:

161

answers:

2

I know this questions has been asked before, but previous answers doesn't seem to be relevant for my problem.

I periodically get the "The ConnectionString property has not been initialized." error. This is what the connection string looks like in web.config

<connectionStrings>
        <add name="Dev" connectionString="Data Source=192.168.200.132,1445;Initial Catalog=Test;User Id=tester;Password=test123;Pooling=true" providerName="System.Data.SqlClient"/>
</connectionStrings>

I'm getting the connection string using

ConfigurationManager.ConnectionStrings["Dev"].ConnectionString

Here is the stacktrace

at System.Data.SqlClient.SqlConnection.PermissionDemand()
at System.Data.SqlClient.SqlConnectionFactory.PermissionDemand(DbConnection outerConnection)
at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
at System.Data.SqlClient.SqlConnection.Open()
at Lite.Data.Database`2.CheckConnection()
at Lite.Data.Database`2.ExecuteReader(String sql, QueryParam[] args)
at Lite.Data.LiteQuery.Results()

I have a simple query builder. Results() compiles the query and executes it using the database class.

Also, do you know why there's a "`2" after my database class name?

Thanks.

A: 

At first, the Data Source parameter looked odd to me... but it appears a comma can be used to separate the IP from the port number.

So, looking at the error, looks like an permissions issue.

Are you, by chance, running a .NET executable from a network share? Running .NET from a non-local drive requires some permissions work before it can do much more than a "Hello World" (search for "caspol.exe" for more details).

richardtallent
Richard, .NET is running on my local WinXP machine. I'm also getting same periodic problem on the production Win2k3 Std server.
Daniel
A: 

Your connection string is improperly formed.

Try:

<connectionStrings>
    <add name="Dev" connectionString="Data Source=192.168.200.132:1445;Initial Catalog=Test;User Id=tester;Password=test123;Pooling=true" providerName="System.Data.SqlClient"/>
</connectionStrings>

Check www.connectionstrings.com for the proper format of a connection string given the database you are using.

Also, 1445 is not the default port number for SQL Server. the default port is 1433. - I'm not sure if you were going for that or if you had actually changed the port number for that server.

Gabriel McAdams
Gabriel, that's the same thing I thought, but there's an example using a comma separating the IP address and port on that page under "Connect via an IP address".
richardtallent
Can you show us the code where you are instantiating the sql connection?
Gabriel McAdams
Maybe this error is actually telling us the truth. Put a break point where this exception occurs. Look at connection.ConnectionString and see if it is set.
Gabriel McAdams
Gabriel, the port number is correctly set on the SQL Server.Richard, here are excerpts from my db classes. http://174.143.205.53/temp/db_excerpts.cs
Daniel
The error occurred again. The connection string was set correctly, it returned the same string in the web.config. So strange... any ideas? Does my db class look ok?
Daniel
Your class looks fine (although I didn't see the use of COMMAND_TYPE). I'm assuming the error occurs in ExecuteNonQuery? Thats the code you left out.
Gabriel McAdams
Yes, from that method as well as other methods that requires database connectivity. Does it matter that I'm using a static instance of the database? Could that cause this type of problem?
Daniel
Very likely, actually. You should count on connection pooling and create an instance of the database for every db call. If you're using the enterprise library, then connection pooling is done automatically. I would use it if you're not already.
Gabriel McAdams
Hmm... I will remove the static instance and see how it goes. I believe I have connection pooling working. When I query `sp_who2` only one connection is created by the sql client. Thanks Gabriel, hopefully this will resolve my problem.
Daniel