Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Using Includes in ASP.Net
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Using Includes in ASP.Net

 Post Reply Post Reply Page  12>
Author
Misty View Drop Down
Senior Member
Senior Member
Avatar

Joined: 06 February 2002
Location: United States
Status: Offline
Points: 711
Post Options Post Options   Thanks (0) Thanks(0)   Quote Misty Quote  Post ReplyReply Direct Link To This Post Topic: Using Includes in ASP.Net
    Posted: 03 May 2005 at 3:44pm
I have created a separate ASP.net page that contains a function for the connectionstring for SQL Server. It is not working. Here's some of my code:

 '---------------------------------------------
' name: BindDataList()
'---------------------------------------------
Sub BindDataList()

             Dim strConnect As String
             Dim objConnect As New System.Data.SqlClient.SQLConnection(ConnectionString())
             Dim objCommand As New System.Data.SqlClient.SQLCommand
             Dim strSQL As String
             Dim dtaSpa As New System.Data.SqlClient.SQLDataAdapter()
             Dim dtsSpa As New DataSet()
             Dim strAreaID As String
    
             objConnect.Open()
   
    'Get incoming querystring values
              strAreaID = request.params("AreaID")

             'Start SQL statement
             strSQL = "Select * From SpaStore"
    
             'Add Order By
    strSQL = strSQL & " where AreaID = '" & strAreaID & "'"
             strSQL= strSQL & " and ListingType IN ('Featured','National')"
    strSQL = strSQL & " and Approve=1"

    strSQL = strSQL & " Order By SpaStoreName"  
 

        'Set the Command Object properties
        objCommand.Connection = objConnect
        objCommand.CommandType = CommandType.Text
        objCommand.CommandText = strSQL

        'Create a new DataAdapter object
        dtaSpa.SelectCommand = objCommand

        'Get the data from the database and
        'put it into a DataTable object named dttSpa in the DataSet object
        dtaSpa.Fill(dtsSpa, "dttSpa")

        'Set the DataSource property of the DataGrid
        dtlSpa.DataSource = dtsSpa

        'Bind all the controls on the page
        dtlSpa.DataBind()
       
        objCommand.ExecuteNonQuery()
       
        'this is the way to close commands
        objCommand.Connection.Close()
       
        objConnect.Close()

End Sub    

'-----------------------------------------------------
' END OF CODE. START OF HTML
'-----------------------------------------------------

     <!--#include file="SQLServer_Include.aspx"-->
 

</script>

Here's my code for SQLServer_Include.aspx:

<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQLClient" %>

<script runat="server">

'================================================================

    Private Function ConnectionString () as String

    

        Dim servername, username, password, databasename As String

        servername = ""

        username = ""

        password = ""

        databasename = ""

        Dim conString As String

        conString = conString.Format("server={0};User ID={1};Password={2};Database={3};", servername, username, password, databasename)

        Return conString

    

    End Function

 </script>

 

  I am getting the following error message:

Parser Error Message: Server includes are not allowed in server script tags.

Source Error:

Line 216:'----------------------------------------------------- 
Line 217:
Line 218:     <!--#include file="SQLServer_Include.aspx"-->

Can someone please help me with this?



Edited by Misty - 03 May 2005 at 3:46pm
Back to Top
michael View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
Post Options Post Options   Thanks (0) Thanks(0)   Quote michael Quote  Post ReplyReply Direct Link To This Post Posted: 03 May 2005 at 4:56pm
Why would you want to do it this way? It makes pretty much no sense. Includes are obsolete, they are SSI and have nothing to do with .net
You can simply put the connection string into your web.config and call it with configuration settings, or if you want it compiled create a class or class library and reference it like
Dim strSQLCon as String = new yourclass.SQLConnectionString
 
Consider in your example, theoretically if it would work, you would include the whole aspx page which in turn would be compiled so you are trying to include html within the script tags. When you use .net try to think more Object Oriented and create the modules as needed and call them as specified by .net and not by legacy applications/methods
Back to Top
Misty View Drop Down
Senior Member
Senior Member
Avatar

Joined: 06 February 2002
Location: United States
Status: Offline
Points: 711
Post Options Post Options   Thanks (0) Thanks(0)   Quote Misty Quote  Post ReplyReply Direct Link To This Post Posted: 04 May 2005 at 2:56am
Originally posted by michael michael wrote:

Why would you want to do it this way? It makes pretty much no sense. Includes are obsolete, they are SSI and have nothing to do with .net
You can simply put the connection string into your web.config and call it with configuration settings, or if you want it compiled create a class or class library and reference it like
Dim strSQLCon as String = new yourclass.SQLConnectionString
 
Consider in your example, theoretically if it would work, you would include the whole aspx page which in turn would be compiled so you are trying to include html within the script tags. When you use .net try to think more Object Oriented and create the modules as needed and call them as specified by .net and not by legacy applications/methods
 
I didn't realize that you could not use an include file in ASP.Net. I had never tried this before. I use a lot of includes in Classic ASP pages. I still have a lot to learn about ASP.Net.
 
I'd like to do it in Web.Config, but I need help with the code. I tried doing it, but I got the following error:
Parser Error Message: Unrecognized element

Source Error:

Line 4:      <system.web>
Line 5:          <customErrors mode="Off"/>
Line 6:      </system.web>
 
Here's my code:
 
<!-- Web.Config Configuration File -->
<configuration>
    <system.web>
        <customErrors mode="Off"/>
    </system.web>

    Private Function ConnectionString () as String
    
        Dim servername, username, password, databasename As String
        servername = ""
        username = ""
        password = ""
        databasename = ""
        Dim conString As String
        conString = conString.Format("server={0};User ID={1};Password={2};Database={3};", servername, username, password, databasename)
        Return conString
    
    End Function
</configuration>
Back to Top
Misty View Drop Down
Senior Member
Senior Member
Avatar

Joined: 06 February 2002
Location: United States
Status: Offline
Points: 711
Post Options Post Options   Thanks (0) Thanks(0)   Quote Misty Quote  Post ReplyReply Direct Link To This Post Posted: 05 May 2005 at 2:52pm
I finally figured out how to do it in Web.Config.
 
I did this in the Web.Config file:
 
<!-- Web.Config Configuration File -->
<configuration>
   
  <appSettings>
    <add key="ConnectionString"
      value="server=server;database=databasename;uid=username;password=password;" />
  </appSettings>
 
<system.web>
        <customErrors mode="Off"/>
    </system.web>
</configuration>
Then I got the connectionstring by the following code:
 
'Get connection string from Web.Config
                &nbs p; strConnect = ConfigurationSettings.AppSettings("ConnectionString")
Back to Top
Lofty View Drop Down
Newbie
Newbie
Avatar

Joined: 03 April 2005
Status: Offline
Points: 19
Post Options Post Options   Thanks (0) Thanks(0)   Quote Lofty Quote  Post ReplyReply Direct Link To This Post Posted: 06 May 2005 at 4:41am
just as a side note.

you can use includes in .net.

just not from within a script tag like you had.
Back to Top
Rincewind2 View Drop Down
Newbie
Newbie
Avatar

Joined: 06 May 2005
Location: United Kingdom
Status: Offline
Points: 2
Post Options Post Options   Thanks (0) Thanks(0)   Quote Rincewind2 Quote  Post ReplyReply Direct Link To This Post Posted: 06 May 2005 at 10:47am
include file doesn't work in .Net...
 
use "include virtual" instead
Back to Top
michael View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
Post Options Post Options   Thanks (0) Thanks(0)   Quote michael Quote  Post ReplyReply Direct Link To This Post Posted: 08 May 2005 at 3:56pm
Rubbish, Sure do includes still work, thus they are just there for backward compability. There is not reason to use the anymore as they are too inflexible.
Back to Top
Mart View Drop Down
Senior Member
Senior Member
Avatar

Joined: 30 November 2002
Status: Offline
Points: 2304
Post Options Post Options   Thanks (0) Thanks(0)   Quote Mart Quote  Post ReplyReply Direct Link To This Post Posted: 09 May 2005 at 2:43am
Bottom line is that there are 2 main replacements, web user controls if your "include" wants to display something (WUC's are like pages inside pages) or classes if your "include" is pure code. You then reference the class by YourClass class = new YourClass(); or Dim class As New YourClass. Both classes and WUC's are much more powerful than includes and allow you to do things includes do not.
Back to Top
 Post Reply Post Reply Page  12>

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.