Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 16th, 2004, 07:56 PM
Authorized User
 
Join Date: Jul 2003
Location: , , .
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
Default Putting connection in web.config issue

I have this connection in my aspx file:
Dim strConnection As String = "server=MARINERHOME; database=Northwind; " & _
"integrated security=true"

It works great.

----------------------------------------------------------
I'm trying to move it to the web.config file like so:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<customErrors mode="Off" />
<compilation debug="true"/>
</system.web>
<appSettings>
<add key="NWind" value="server=marinerhome; database=Northwind; integrated security=true;" />
</appSettings>
</configuration>

and call it from the aspx file like so:
<script language="VB" runat="server">
Sub Page_Load(Source As Object, E As EventArgs)
Dim strConnection As String = ConfigurationSettings.AppSettings("NWind")
Dim objConnection As New SqlConnection(strConnection)

Dim strSQL As String = "SELECT FirstName, LastName, Country " & _
"FROM Employees;"
Dim objCommand As New SqlCommand(strSQL, objConnection)

objConnection.Open()

Response.Write("ServerVersion: " & objConnection.ServerVersion & _
vbCRLF & "Datasource: " & objConnection.DataSource & _
vbCRLF & "Database: " & objConnection.Database)

dgNameList.DataSource = objCommand.ExecuteReader()
dgNameList.DataBind()
objConnection.Close()
End Sub
</script>

This is the result I get when trying to call the aspx file from a browser.
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:


Line 30: Dim objCommand As New SqlCommand(strSQL, objConnection)
Line 31:
Line 32: objConnection.Open()
Line 33:
Line 34: Response.Write("ServerVersion: " & objConnection.ServerVersion & _

I've tried the common mistakes with no luck. Can someone please help.

Thanks!
Rich

 
Old October 18th, 2004, 04:48 AM
Friend of Wrox
 
Join Date: Jun 2004
Location: Chennai, Tamil Nadu, India.
Posts: 449
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to r_ganesh76
Default

I tried running your code. It is working well. This is the code which I used.

.aspx
------

<%@ Import Namespace="System.Data.SqlClient" %>
<script language="VB" runat="server">
Sub Page_Load(Source As Object, E As EventArgs)
    Dim strConnection As String = ConfigurationSettings.AppSettings("NWind")
    Dim objConnection As New SqlConnection(strConnection)

    Dim strSQL As String = "SELECT FirstName, LastName, Country " & _
    "FROM Employees;"
    Dim objCommand As New SqlCommand(strSQL, objConnection)

    objConnection.Open()

    Response.Write("ServerVersion: " & objConnection.ServerVersion & _
    vbCRLF & "Datasource: " & objConnection.DataSource & _
    vbCRLF & "Database: " & objConnection.Database)

    dgNameList.DataSource = objCommand.ExecuteReader()
    dgNameList.DataBind()
    objConnection.Close()
End Sub
</script>
<HTML>
    <HEAD>
        <title>Testing</title>
    </HEAD>
    <body>
        <form id="Form1" method="post" runat="server">
            <asp:DataGrid id="dgNameList" runat="server"></asp:DataGrid>
        </form>
    </body>
</HTML>


my web.config is this.


web.config
----------


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.web>
        <customErrors mode="Off" />
        <compilation debug="true" />
    </system.web>
    <appSettings>
        <add key="NWind" value="server=servername; database=Northwind; uid=uid;pwd=pwd;" />
    </appSettings>
</configuration>

Please try it at your side after changing the connection string in the web.config.



Regards
Ganesh
 
Old October 18th, 2004, 07:31 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Harrisburg, PA, USA.
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hey,

You could also try ConfigurationSettings.AppSettings.Get("NWind")

Brian
 
Old October 18th, 2004, 12:02 PM
Authorized User
 
Join Date: Jul 2003
Location: , , .
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'll try that when I get home tonight. I'm at my day job with ASP.3 now. LOL

Thanks!
Rich

 
Old October 18th, 2004, 04:17 PM
Authorized User
 
Join Date: Jul 2003
Location: , , .
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks everyone!!
When I did this response.write with the webconfig file in the root of the virtual server it wrote nothing but when I moved the webconfig file into the folder I was working on it wrote the string.
<%
Dim NWind As String
Response.Write("<h3>AppSettings</h3>")
   For Each NWind In ConfigurationSettings.AppSettings.Keys
      Response.Output.WriteLine(NWind & "=" & _
         ConfigurationSettings.AppSettings.Item(NWind))
   Next
%>
The book says to create a folder named begasnetdb and save the webconfig file to the root of that folder. That didn't work for me I had to move the webconfig file into the ch03 directory I was working in for the connection to work. Did I simply misunderstand the book?

 
Old October 19th, 2004, 06:45 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Harrisburg, PA, USA.
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

I have a feeling that they were loading all the chapters into one solution, and used one web.config for all of them... Don't have the book so I don't know.

Brian
 
Old October 19th, 2004, 07:46 PM
Authorized User
 
Join Date: Jul 2003
Location: , , .
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It would make sense to do that. So why does it not work for me?

 
Old October 20th, 2004, 06:59 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Harrisburg, PA, USA.
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Well, you could quickly create a new solution for this case...

Brian




Similar Threads
Thread Thread Starter Forum Replies Last Post
web.config vs. app.config darlo Visual Studio 2005 11 August 20th, 2008 07:23 AM
connection string issues, web.config file issues kaliaparijat ASP.NET 2.0 Professional 1 June 12th, 2008 08:07 AM
setting connection string in the web.config ACE2084 ADO.NET 2 January 6th, 2005 04:33 PM
Database Connection with Web.Config RPG SEARCH Classic ASP Databases 7 July 26th, 2004 06:10 PM
Variable Path in web.Config Connection String? ElPato General .NET 4 June 21st, 2004 05:44 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.