Print Page | Close Window

Using Includes in ASP.Net

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: ASP.NET Discussion
Forum Description: Discussion and chat on ASP.NET related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=14935
Printed Date: 29 March 2026 at 10:15am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Using Includes in ASP.Net
Posted By: Misty
Subject: Using Includes in ASP.Net
Date 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?




Replies:
Posted By: michael
Date 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


-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: Misty
Date 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>


Posted By: Misty
Date 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")


Posted By: Lofty
Date 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.


Posted By: Rincewind2
Date Posted: 06 May 2005 at 10:47am
include file doesn't work in .Net...
 
use "include virtual" instead


Posted By: michael
Date 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.

-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: Mart
Date 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.


Posted By: Rincewind2
Date Posted: 09 May 2005 at 8:29pm
I use classes myself, along with WUC's...
 
Was only pointing out that the option is there for (as you say) backward compatibility with classic ASP (I assume to make porting easier)
 
the "include file" thing is something I ran up against moving some legacy code from a win2k box to our new 2k3 one
 
(side note: I'm still learning the intricacies of .Net - WUC's are something I have begun to use, but I still got a lot to learn about ADO.Net - sticking with ADO for now)


Posted By: michael
Date Posted: 10 May 2005 at 11:55am
It is actually very simple to port from ado to ado.net I would put all ado code into a seperate class and when you are ready to upgrade, you just have to change your datalayer class.

-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net