Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Basics
Password Reminder
Register
| FAQ | Members List | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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

  #2 (permalink)  
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
  #3 (permalink)  
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
  #4 (permalink)  
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

  #5 (permalink)  
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?

  #6 (permalink)  
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
  #7 (permalink)  
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?

  #8 (permalink)  
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
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 04:51 AM.


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