Thursday, March 22, 2012

ConnectionString Property Not Set

Hi, After many nights without sleep I'm not seeing this? Can anyone help why I'm getting a ConnectionString Property not set error? thanks!

Dim SconnAsString
Dim DBConAsNew Data.SqlClient.SqlConnection
Sconn = ConfigurationManager.AppSettings("LocalSqlServer")
DBCon =New SqlClient.SqlConnection(Sconn)
Dim cmdCommandAsNew Data.SqlClient.SqlCommand'Dont forget to instantiate a connection object

cmdCommand.Connection = DBCon
DBCon.Open()

You created a command and you assigned connection to it, but maybe you should also assign command itself?

Try to see if your connection string is retrieved from configuration file, maybe your connection name or connection string in it is wrong?

Dim SconnAsString
Dim DBConAsNew Data.SqlClient.SqlConnection
Sconn = ConfigurationManager.AppSettings("LocalSqlServer")
DBCon =New SqlClient.SqlConnection(Sconn)
Dim cmdCommandAsNew Data.SqlClient.SqlCommand("select * from yourTable")

'Dont forget to instantiate a connection object

cmdCommand.Connection = DBCon
DBCon.Open()

|||

That didn't help. Perhaps a tracecode might give more information of what's going on. The error happens at line DBCon.Open()

Any help greatly appreciated.

System.InvalidOperationException was unhandled by user code
Message="The ConnectionString property has not been initialized."
Source="System.Data"
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 Admin_DistributorDetail.Button1_Click(....)
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

|||could you provide your connection string?|||

This is what I have now. Not sure if I needed the remove name statement after I've manually configured asp.net db to get another db name instead of the default.

<

connectionStrings>

<

removename="LocalSqlServer" />
<addname="LocalSqlServer"connectionString="Data Source=XXXXXXX;Initial Catalog=YYYYYYY;Persist Security Info=True;User ID=ZZZZZZ;Password=TTTTT"providerName="System.Data.SqlClient" />

</

connectionStrings>|||

BTW the connection string appears to be working fine for the rest of the site through the aspx pages and the VS sqladapters is just in the code behind I getting this problem.

|||

try to modify your code to be like this below and check if your SConn contain valid connection string

Dim SconnAsString
Sconn = ConfigurationManager.AppSettings("LocalSqlServer")
Dim DBConAsNew SqlClient.SqlConnection(Sconn)
DBCon =Dim cmdCommandAsNew Data.SqlClient.SqlCommand("select * from yourTable")

DBCon.open

Thanks

|||

Thanks but I haven't been able to run this code yet because an error occurs at the "Dim cmdCommand" with curlies under the "Dim" stating that it expects an expression. The cmdCommand in later code then has curlies underneath stating the cmdCommand is not declared.

Hope there is a small adjustment to this that can get it going?

|||

sorry something left in my code try it without any command and check if it works

Dim SconnAsString
Sconn = ConfigurationManager.AppSettings("LocalSqlServer")
Dim DBConAsNew SqlClient.SqlConnection(Sconn)

DBCon.open

dbcon.close

|||

This certainly looks tidy and concise, a step in the right direction, however unfortunate, I'm still getting the "The ConnectionString Property has not been initialized" error on DBCon.open

hmmm any ideas left?

|||

have you checked what is returned as your connection string?

Thanks

|||

I'm sorry I'm somewhat of a newby when it comes to debugging. How would I do this? My watch list does not show a connection string but only a red exlamation mark stating.

--------
Name Value
Div[1] End of expression expected
--------

If you mean something other than the watch list please let me know and how to do it.

thnks

|||

Sorry I've just found the debugging tab I'm supposed to look for. Locals right?

The connectionstring indeed returns "" thus empty.

Why in the world would it do that?

|||

Could it have something to do with my webconfig <appsettings> part? its has nothing in it. is now set to <appsettings></appsettings>

|||

your

Sconn = ConfigurationManager.AppSettings("LocalSqlServer")

probably points to wrong place try this:

Sconn = ConfigurationManager.ConnectionStrings("LocalSqlServer")

sqlsql

No comments:

Post a Comment