Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

"ConnectionString property has not been initialized"

Posted on 2009-05-19
7
Medium Priority
?
376 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

610 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