Monday, March 26, 2012

The ConnectionString property has not been initialized

Hi, I am trying to run some code I got off from the Quickstart tutorial for connecting to an Access database. I have been attempting several different methods from several different tutorials, none of which have been successful.

Here is the code that I have used which gave me the error: "The ConnectionString property has not been initialized" The complete error message is posted below the code.


<%@dotnet.itags.org. Import Namespace="System.Data" %>
<%@dotnet.itags.org. Import Namespace="System.Data.SqlClient" %
<html>
<script language="VB" runat="server"
Sub Page_Load(Sender As Object, E As EventArgs)

Dim DS As DataSet
Dim MyConnection As SqlConnection
Dim MyCommand As SqlDataAdapter

MyConnection = New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("PubsString"))
MyCommand = New SqlDataAdapter("select * from Release_of_Information", MyConnection)

DS = new DataSet()
MyCommand.Fill(ds, "Release_of_Information")

MyDataGrid.DataSource=ds.Tables("Release_of_Information").DefaultView
MyDataGrid.DataBind()
End Sub

</script

Below is the complete error message.

I made a change to the web.cnfg file, as is stated below, and all that I got was even less functionality (okay, no functionality) from the webserver.

any ideas ?

The ConnectionString property has not been initialized.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The ConnectionString property has not been initialized.

Source Error:

The source code that generated this unhandled exception can only be shown when compiled in debug mode. To enable this, please follow one of the below steps, then request the URL:

1. Add a "Debug=true" directive at the top of the file that generated the error. Example:

<%@dotnet.itags.org. Page Language="C#" Debug="true" %
or:

2) Add the following section to the configuration file of your application:

<configuration>
<system.web>
<compilation debug="true"/>
</system.web>
</configurationTry adding a Trace.Write (or a response.write) to see what your application sees as the configuration string - like:
Trace.Write("Config = " & ConfigurationSettings.AppSettings("PubsString"))

see what's really happening there - - it looks like, at first glance, that it merely doesn't find a viable connection string there.
Check out this link:

http://www.connectionstrings.com/
With my code now looking like this:

<configuration>
<appSettings>
<add key="PubsString" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\C:\mikep\Career_Start\Career_Start.mdb;User Id=admin;Password=;" />
</appSettings>
<system.web>
<compilation debug="true"/>
</system.web>
</configuration
<%@dotnet.itags.org. Import Namespace="System.Data" %>
<%@dotnet.itags.org. Import Namespace="System.Data.SqlClient" %
<html>
<script language="VB" runat="server"
Sub Page_Load(Sender As Object, E As EventArgs)
Trace.Write("Config = " & ConfigurationSettings.AppSettings("PubsString"))
Dim DS As DataSet
Dim MyConnection As SqlConnection
Dim MyCommand As SqlDataAdapter

MyConnection = New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("PubsString"))
MyCommand = New SqlDataAdapter("select * from Release_of_Information", MyConnection)

DS = new DataSet()
MyCommand.Fill(ds, "Release_of_Information")

MyDataGrid.DataSource=ds.Tables("Release_of_Information").DefaultView
MyDataGrid.DataBind()
End Sub

</script
<body

I am still getting "The ConnectionString property has not been initialized" error message.

I will be glad if I can get beyond this...
your code in web.config looks fine to me just check that you are using the same username and password as you have specified.

Also try changing this the following line:


MyConnection = New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("PubsString"))

TO

string ConnectionString = (string) ConfigurationSettings.AppSettings["PubsString"];
SqlConnection myConnection = new SqlConnection(ConnectionString);

0 comments:

Post a Comment