ConnectionString property has not been initialized

  • Thread starter Thread starter tshad
  • Start date Start date
T

tshad

I am trying to create an application in my VS 2002 application. I took some
of the code from my web page where it works fine.

I can connect fine from my Sql Query Analyser to both my local Sql Server
and off Sql Server machines.

But I am getting the following error from my "objConn.Open()".

Is this a problem from going from my web to a normal application?

The error is:

The ConnectionString property has not been initialized

My code is:

Imports System.Data
Imports System.Data.SqlClient

....

Dim dbReader As SqlDataReader
Dim ConnectionString As String =
System.Configuration.ConfigurationSettings.AppSettings("Persist Security
Info=False;Data Source=balair.interez.com;Initial Catalog=InterezData;User
ID=xxx;Password=xxxx;")
Dim objConn As New SqlConnection(ConnectionString)
Dim CommandText As String = "GetCandidateSearchDrops"
Dim objCmd As New SqlCommand(CommandText, objConn)
objCmd.CommandType = CommandType.StoredProcedure
With objCmd.Parameters
.Add("@CompanyID", SqlDbType.Int).Value = 153973
End With
objConn.Open()
dbReader = objCmd.ExecuteReader

Thanks,

Tom
 
I can connect fine from my Sql Query Analyser to both my local Sql Server
and off Sql Server machines.

But I am getting the following error from my "objConn.Open()".
Dim dbReader As SqlDataReader
Dim ConnectionString As String =
System.Configuration.ConfigurationSettings.AppSettings("Persist Security
Info=False;Data Source=balair.interez.com;Initial Catalog=InterezData;User
ID=xxx;Password=xxxx;")
Dim objConn As New SqlConnection(ConnectionString)


What your code is doing is trying to fetch a key from the .config file whose
name is the content of your connection string.

Here:

In App.Config:

<add key="connectionString" value="your actual connection string here" />


In your VB code:

Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient

Public Class Foo

Public Sub DoStuff()

Dim cnstr As String =
ConfigurationSettings.AppSettings("connectionString")
Dim cn As New SqlConnection(cnstr)
' etc.

End Class

You see, on the first "Dim" line, you are asking for the value of the item
whose key is named "connectionString". The value that is returned is
whatever is in the value attribute in the config file. Then you can go
about your business :)
 
Mike Labosh said:
What your code is doing is trying to fetch a key from the .config file whose
name is the content of your connection string.

Here:

In App.Config:

<add key="connectionString" value="your actual connection string here" />


In your VB code:

Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient

Public Class Foo

Public Sub DoStuff()

Dim cnstr As String =
ConfigurationSettings.AppSettings("connectionString")
Dim cn As New SqlConnection(cnstr)
' etc.

End Class

You see, on the first "Dim" line, you are asking for the value of the item
whose key is named "connectionString". The value that is returned is
whatever is in the value attribute in the config file. Then you can go
about your business :)

So what I need to do in my application code is just put the connection
string directly into the variable. I tried that and it worked.

Thanks,

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top