Solved

"ConnectionString property has not been initialized"

Posted on 2009-05-19
7
370 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

829 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