• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 377
  • Last Modified:

"ConnectionString property has not been initialized"

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
jppinto
Asked:
jppinto
  • 4
  • 2
1 Solution
 
satheeshmCommented:
Hey ,

Try this,

ConnectionStrings.ConnectionStrings("ConnectionString")
0
 
jppintoAuthor Commented:
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
 
abelCommented:
> 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
satheeshmCommented:
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
 
abelCommented:
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
 
abelCommented:
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
 
abelCommented:
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now