Solved

"ConnectionString property has not been initialized"

Posted on 2009-05-19
7
372 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# MVC Insert Multiple Row into DB 2 94
disable data migrations in visual studio 2017 4 67
Set the FileVersion of a website dll 1 57
Data Analysis 7 58
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…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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 a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 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