Solved

"ConnectionString property has not been initialized"

Posted on 2009-05-19
7
367 Views
Last Modified: 2012-05-07
I've the following code.

I get the above error on my web page. If I run it local, in Visual Studio, I have no problems. When I publish the page to the server and run it, I get the error.

I know I'm missing something basic but I can't see what!

Thanks for your help on this...

jppinto
On Web.config:
 

<connectionStrings>

    <add name="ConnectionString" connectionString="Data Source=mssqlxxx.win-servers.com;Persist Security Info=True;User ID=xxxxxx;Password=yyyyyyy;" providerName="System.Data.SqlClient"/>

  </connectionStrings>
 

On ASP page:
 

Imports System.Configuration.ConfigurationManager

Public myConnectionString As String = ConnectionStrings(1).ConnectionString
 

Using connection As New SqlConnection(myConnectionString)

     Dim command As New SqlCommand("CheckUtilizador", connection)

     command.CommandType = CommandType.StoredProcedure

     command.Parameters.Add("@utilizador", Data.SqlDbType.VarChar).Value = txtUtilizador.Text

     connection.Open()

     Result = CType(command.ExecuteScalar(), Integer)

End Using

Open in new window

0
Comment
Question by:jppinto
  • 4
  • 2
7 Comments
 
LVL 5

Expert Comment

by:satheeshm
ID: 24421109
Hey ,

Try this,

ConnectionStrings.ConnectionStrings("ConnectionString")
0
 
LVL 33

Author Comment

by:jppinto
ID: 24421136
If I try like this:

Public myConnectionString As String = ConnectionStrings.ConnectionStrings("ConnectionString")

I get an error:

'ConnectionStrings' is not a member of 'System.Configuration.ConnectionStringSettingsCollection'.
0
 
LVL 39

Expert Comment

by:abel
ID: 24421214
> When I publish the page to the server and run it, I get the error.

when you publish, does the web.config get published too? Can you check?
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 5

Expert Comment

by:satheeshm
ID: 24421231
Hi,

Check this, I tested this and working in my project. hope you have added reference to system.configuration

Web.config
--------------
  <connectionStrings>
    <add name="constr" connectionString="Data Source=NDF-CORP-MS01;Initial Catalog=main_02_09_2009;Integrated Security=SSPI;"/>
  </connectionStrings>

Code behind
-----------------
Imports System.Configuration

Private connString As String = ConfigurationManager.ConnectionStrings("constr").ToString
0
 
LVL 39

Expert Comment

by:abel
ID: 24421279
Hmm, something else maybe up here. Looking at your code again, it seems wrong and it looks as if you are trying to access the second connection string, while you are only showing the first. Maybe locally you have two connection strings and on the production server you have only one? Try to access them by name, like satheeshm suggests:

 
 



' wrong: '

Public myConnectionString As String = ConnectionStrings(1).ConnectionString
 

' better: '

Public myConnectionString As String = ConfigurationManager.ConnectionStrings(0)
 

' good: '

Public myConnectionString As String = ConfigurationManager.ConnectionStrings("ConnectionString")

Open in new window

0
 
LVL 39

Accepted Solution

by:
abel earned 500 total points
ID: 24421295
Oops, mistake, wrong cut/paste of the lines. I meant this:

' wrong: '

Public myConnectionString As String = ConnectionStrings(1).ConnectionString

 

' better: '

Public myConnectionString As String = ConfigurationManager.ConnectionStrings(0).ConnectionString

 

' good: '

Public myConnectionString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString

Open in new window

0
 
LVL 39

Expert Comment

by:abel
ID: 24421567
You could have shared the points with satheesm, after all, I used his idea to present you the solution...

glad it works now, though.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now