Solved

"ConnectionString property has not been initialized"

Posted on 2009-05-19
7
369 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

776 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