web config

  • Thread starter Joe via DotNetMonster.com
  • Start date
J

Joe via DotNetMonster.com

Hi,

I'm trying to setup the Web Config file so that the connection string is
read from it but I'm not sure what I'm doing wrong. The error I get is:
The ConnectionString property has not been initialized.
I also tried to create a virtual path but that didn't work.
My application is at c:\inetpub\wwwroot\web This is where the web.config
file is and it contains the following:

<appSettings>
<add key="connectionstring" value="server=(local)
;trusted_connection=true;database=xxx"/>
</appSettings>


The aspx file contains:

<%@ Page Language="VB" %>
<script runat="server">
Function GetData() As System.Data.IDataReader
Dim connectionString As String
connectionString = ConfigurationSettings.AppSettings
("ConnectionString")

Dim dbConnection As System.Data.IDbConnection = New
System.Data.SqlClient.SqlConnection(connectionString)

Dim queryString As String = "SELECT [tblLink].
, [tbllink]
..[linkURL"& _
"] FROM [tblink]"
Dim dbCommand As System.Data.IDbCommand = New
System.Data.SqlClient.SqlCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

dbConnection.Open
Dim dataReader As System.Data.IDataReader =
dbCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)

Return dataReader
End Function

Sub Page_Load(sender As Object, e As EventArgs)
Page.Databind()
End Sub

</script>


Thanks
 
P

Prodip Saha

"connectionstring" and "ConnectionString" are not the same. Change the line-
connectionString=ConfigurationSettings.AppSettings("ConnectionString")
to
connectionString=ConfigurationSettings.AppSettings("connectionstring")


Joe via DotNetMonster.com said:
Hi,

I'm trying to setup the Web Config file so that the connection string is
read from it but I'm not sure what I'm doing wrong. The error I get is:
The ConnectionString property has not been initialized.
I also tried to create a virtual path but that didn't work.
My application is at c:\inetpub\wwwroot\web This is where the web.config
file is and it contains the following:

<appSettings>
<add key="connectionstring" value="server=(local)
;trusted_connection=true;database=xxx"/>
</appSettings>


The aspx file contains:

<%@ Page Language="VB" %>
<script runat="server">
Function GetData() As System.Data.IDataReader
Dim connectionString As String
connectionString = ConfigurationSettings.AppSettings
("ConnectionString")

Dim dbConnection As System.Data.IDbConnection = New
System.Data.SqlClient.SqlConnection(connectionString)

Dim queryString As String = "SELECT [tblLink].
, [tbllink]
.[linkURL"& _
"] FROM [tblink]"
Dim dbCommand As System.Data.IDbCommand = New
System.Data.SqlClient.SqlCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

dbConnection.Open
Dim dataReader As System.Data.IDataReader =
dbCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)

Return dataReader
End Function

Sub Page_Load(sender As Object, e As EventArgs)
Page.Databind()
End Sub

</script>


Thanks
 
J

Joe via DotNetMonster.com

Thanks. I changed it but I still get the error message that:

The ConnectionString property has not been initialized.
 
P

Patrick Olurotimi Ige

JOe how can u get ConnectionString property error
when u said u have changed it!
 
J

Joe via DotNetMonster.com

I fixed the names so that I tried to make sure it was case sensitive. I've
been going through the code but still can't seem to see what's wrong. I
renamed the connection string, so I have in the Web config:

<appSettings>
<add key="ConnectionString1" value="server=(local)
;trusted_connection=true;database=xx"/>
</appSettings>

aspx file:
Function GetLessons() As System.Data.IDataReader
Dim strConnection As String
strConnection = ConfigurationSettings.AppSettings
("ConnectionString1")
Dim dbConnection As System.Data.IDbConnection = New
System.Data.SqlClient.SqlConnection(strConnection)

Dim queryString As String = "SELECT [tblLesson].* FROM
[tblLesson]"
Dim dbCommand As System.Data.IDbCommand = New
System.Data.SqlClient.SqlCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

dbConnection.Open
Dim dataReader As System.Data.IDataReader =
dbCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)

Return dataReader
End Function


Sub Page_Load(sender As Object, e As EventArgs)
Page.Databind()
End Sub

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<asp:DataGrid id="DataGrid1" runat="server" DataSource="<%#
GetLessons() %>"></asp:DataGrid>
</form>
</body>
</html>

Thanks again
 
J

Juan T. Llibre

Joe,

why are you using Data Provider syntax
for a simple data connection ?

System.Data.IDataReader, System.Data.IDbConnection,
and System.Data.IDbConnection all are used when
implementing your own Data Provider.

Please review
http://msdn.microsoft.com/library/d...en-us/cpguide/html/cpconadonetconnections.asp
to see the info you need to implement a data connection
using any of the Data Providers built into .NET.




Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================
 
J

Joe via DotNetMonster.com

Hi,

I think what's happening is that it's not seeing the web.config file since
I tried removing it and got the same error message.

I have the application at: c:/inetpub/wwwroot/web
The web.config file is in this directory and so is the aspx file that I am
testing. I created a virtual directory to this.

Am I missing anything in making this my application root?

Thanks again
 
J

Joe via DotNetMonster.com

Thanks for all the help. I got it working. It was the case-sensitivity
issue. Also thanks for the advice on the data provider.
 
M

Matt Berther

Hello Juan,

I disagree with this statement. While these interfaces can be used when implementing
your own data provider, they provide a very nice abstraction layer.

Let's say that I want to interact with either an Oracle or an MSSQL database
in my application, depending on my client's requirements. The quickest/easiest
way to accomplish this is to return an IDbConnection or IDataReader out of
my class's methods. The calling code has no idea about the underlying datastore
and new databases can be added by simply adding a new class.

Very good design...

--
Matt Berther
http://www.mattberther.com
Joe,

why are you using Data Provider syntax
for a simple data connection ?
System.Data.IDataReader, System.Data.IDbConnection, and
System.Data.IDbConnection all are used when implementing your own Data
Provider.

Please review

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgui
de/html/cpconadonetconnections.asp

to see the info you need to implement a data connection

using any of the Data Providers built into .NET.

Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================
I fixed the names so that I tried to make sure it was case sensitive.
I've been going through the code but still can't seem to see what's
wrong. I renamed the connection string, so I have in the Web config:

<appSettings>
<add key="ConnectionString1" value="server=(local)
;trusted_connection=true;database=xx"/>
</appSettings>
aspx file:
Function GetLessons() As System.Data.IDataReader
Dim strConnection As String
strConnection = ConfigurationSettings.AppSettings
("ConnectionString1")
Dim dbConnection As System.Data.IDbConnection = New
System.Data.SqlClient.SqlConnection(strConnection)
Dim queryString As String = "SELECT [tblLesson].* FROM
[tblLesson]"
Dim dbCommand As System.Data.IDbCommand = New
System.Data.SqlClient.SqlCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection
dbConnection.Open
Dim dataReader As System.Data.IDataReader =
dbCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
Return dataReader
End Function
Sub Page_Load(sender As Object, e As EventArgs)
Page.Databind()
End Sub
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<asp:DataGrid id="DataGrid1" runat="server" DataSource="<%#
GetLessons() %>"></asp:DataGrid>
</form>
</body>
</html>
Thanks again
 
J

Juan T. Llibre

In general, yes, but the added level of complexity
may cause troubleshooting problems which don't
crop up with the simpler data connection methods.




Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

Matt Berther said:
Hello Juan,

I disagree with this statement. While these interfaces can be used when
implementing your own data provider, they provide a very nice abstraction
layer.

Let's say that I want to interact with either an Oracle or an MSSQL
database in my application, depending on my client's requirements. The
quickest/easiest way to accomplish this is to return an IDbConnection or
IDataReader out of my class's methods. The calling code has no idea about
the underlying datastore and new databases can be added by simply adding a
new class.

Very good design...

--
Matt Berther
http://www.mattberther.com
Joe,

why are you using Data Provider syntax
for a simple data connection ?
System.Data.IDataReader, System.Data.IDbConnection, and
System.Data.IDbConnection all are used when implementing your own Data
Provider.

Please review

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgui
de/html/cpconadonetconnections.asp

to see the info you need to implement a data connection
using any of the Data Providers built into .NET.

Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================
I fixed the names so that I tried to make sure it was case sensitive.
I've been going through the code but still can't seem to see what's
wrong. I renamed the connection string, so I have in the Web config:

<appSettings>
<add key="ConnectionString1" value="server=(local)
;trusted_connection=true;database=xx"/>
</appSettings>
aspx file:
Function GetLessons() As System.Data.IDataReader
Dim strConnection As String
strConnection = ConfigurationSettings.AppSettings
("ConnectionString1")
Dim dbConnection As System.Data.IDbConnection = New
System.Data.SqlClient.SqlConnection(strConnection)
Dim queryString As String = "SELECT [tblLesson].* FROM
[tblLesson]"
Dim dbCommand As System.Data.IDbCommand = New
System.Data.SqlClient.SqlCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection
dbConnection.Open
Dim dataReader As System.Data.IDataReader =
dbCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
Return dataReader
End Function
Sub Page_Load(sender As Object, e As EventArgs)
Page.Databind()
End Sub
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<asp:DataGrid id="DataGrid1" runat="server" DataSource="<%#
GetLessons() %>"></asp:DataGrid>
</form>
</body>
</html>
Thanks again
 
T

Tampa.NET Koder

Glad you got this solved. From first look, I thought you didn't import the
System.Configuration Namespace. I didn't see it anywere in your code. The
fully namespace with class name is
System.Configuration.ConfigurationSettings.AppSettings. But, look like this
wasn't it. Just wanted to give others a heads up on this. People sometimes
forget this when not using VS.NET to compile their code.


Joe via DotNetMonster.com said:
Hi,

I'm trying to setup the Web Config file so that the connection string is
read from it but I'm not sure what I'm doing wrong. The error I get is:
The ConnectionString property has not been initialized.
I also tried to create a virtual path but that didn't work.
My application is at c:\inetpub\wwwroot\web This is where the web.config
file is and it contains the following:

<appSettings>
<add key="connectionstring" value="server=(local)
;trusted_connection=true;database=xxx"/>
</appSettings>


The aspx file contains:

<%@ Page Language="VB" %>
<script runat="server">
Function GetData() As System.Data.IDataReader
Dim connectionString As String
connectionString = ConfigurationSettings.AppSettings
("ConnectionString")

Dim dbConnection As System.Data.IDbConnection = New
System.Data.SqlClient.SqlConnection(connectionString)

Dim queryString As String = "SELECT [tblLink].
,
[tbllink]
.[linkURL"& _
"] FROM [tblink]"
Dim dbCommand As System.Data.IDbCommand = New
System.Data.SqlClient.SqlCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

dbConnection.Open
Dim dataReader As System.Data.IDataReader =
dbCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)

Return dataReader
End Function

Sub Page_Load(sender As Object, e As EventArgs)
Page.Databind()
End Sub

</script>


Thanks
 

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

Top