[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

simple include in asp.net

hi..
i want to include conn.aspx inside index.aspx using vb.net syntax.
any idea ?
0
ramma
Asked:
ramma
  • 5
  • 3
  • 3
  • +2
1 Solution
 
vadivhereCommented:
If you're talking about ASP "Include" it is not possible in ASP.NET, but you can achieve the same thing in OO. I hope, you have not clearly mentioned your problem here. Plz post your question with more elaboritve, so that it will help us to provide you right solution.

Cheers!
0
 
Bob LearnedCommented:
Excuse me:

Web Forms Syntax Reference:
http://samples.gotdotnet.com/quickstart/aspplus/doc/webformssyntaxref.aspx

Server-Side Include Syntax: <-- #Include File="conn.aspx" -->

Bob

0
 
YZlatCommented:
with asp.net you should use User Controls instead of ASP includes. You can easily modify your .aspx file into a user Control by changing extension to .ascx, changing @Page directive to @Control directive and removing any <html> or <body> tags.

But in your case it sounds like you should greate a global module (file with .vb extension) and just put all your connection information in there
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Bob LearnedCommented:
I agree with using User Controls, I was just pointing out that Includes are still supported.

Bob
0
 
YZlatCommented:
oh, and make sure your module is Public
0
 
rammaAuthor Commented:
ok here is the details :
in my index.aspx i connect to my sqlserver database using this line :
Conn = "Server=localhost;initial catalog=realestate;uid=sa;pwd=;"
I need to put this connection string in separate  'Conn.aspx' and call it inside index.aspx like in classic ASP.
Infact i am still fresh in the dot net and i did not understand the solution mentioned by YZlat .
Can i have more details.?

0
 
stengeljCommented:
If that's all you need to do, I would recommend you put your connection string in your web.config file. Then you don't have to worry about includes and extra pages.

In web.config:

<configuration>
    <appsettings>
         <add key="myConn" value="Server=localhost;initial catalog=realestate;uid=sa;pwd=;" />
    </appsettings>
</configuration>

Reference in your .aspx page:

<%# System.Configuration.ConfigurationSettings.AppSettings("myConn") %>

Useage Example:

Dim strConn = System.Configuration.ConfigurationSettings.AppSettings("myConn")
Dim MyConn as New OleDBConnection (strConn)

If you MUST do this with an include, TheLearnedOne's example is correct.
0
 
rammaAuthor Commented:
I wrote this segment in web.config  after <system.web>:
<configuration>
    <appsettings>
         <add key="myConn" value="Server=localhost;initial catalog=realestate;uid=sa;pwd=;" />
    </appsettings>
</configuration>


but I got this error :

Configuration Error
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.

Parser Error Message: Unrecognized configuration section 'configuration'

Source Error:


Line 1:  <?xml version="1.0" encoding="utf-8"?>
Line 2:  <configuration>
Line 3:      <configuration>
Line 4:      <appsettings>
Line 5:           <add key="myConn" value="Server=localhost;initial catalog=realestate;uid=sa;pwd=;" />
 

Source File: c:\inetpub\wwwroot\realestate\web.config    Line: 3

Any idea ?
0
 
Bob LearnedCommented:
The web config file already had a <configuration> tag, by default, and you didn't need to add another one.

Just put the appsettings section in the file.

Bob
0
 
YZlatCommented:
onlt write

 <appsettings>
         <add key="myConn" value="Server=localhost;initial catalog=realestate;uid=sa;pwd=;" />
    </appsettings>
also create a file conn.vb  and put the following code in there:


Imports System.Data
Imports System.Data.SQLClient
Public Module conn
    '*************************************************************************
    '
    '   ROUTINE: GetConnection
    '
    '   DESCRIPTION: This routine returns an open connection
    '-------------------------------------------------------------------------
    Public Function GetConnection() As SQLConnection
        Dim Conn As SQLConnection
        Dim strConn As String

        strConn = ConfigurationSettings.AppSettings("myConn")
        SQLConnection = New SQLConnection(strConn)
        Try
            Conn.Open()
        Catch sqlerr As SQLException
     
            Console.Write("SQL error: " + sqlerr.ToString + ", SQL error message:" & sqlerr.Message)
        Catch ex As Exception
         
            Console.Write("Error Opening Connection: " + ex.ToString())
        End Try

        Return Conn
    End Function
    '*************************************************************************
    '
    '   ROUTINE: GetData
    '
    '   DESCRIPTION: This routine retrieves the data from a database
    '   based on a sql string passed to it
    '-------------------------------------------------------------------------
    Public Function GetData(ByVal sql As String) As SQLDataReader
        Dim dr As SQLDataReader
        Dim Conn As SQLConnection = GetConnection()
        Dim Cmd As SQLCommand = New SQLCommand(sql, Conn)
        Try
            dr = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
     
        Catch ex As Exception
         
            Console.Write("Error Retrieving Data: " + ex.ToString())
        Finally
            'If Conn.State = ConnectionState.Open Then
            'Conn.Close()
            'End If
        End Try
        Return dr
    End Function
End Module
0
 
rammaAuthor Commented:
I wrote these 3 lines after <system.web>
<appsettings>
         <add key="myConn" value="Server=localhost;initial atalog=realestate;uid=sa;pwd=;" />
</appsettings>

i still have the same error :
----------------------------------------------------------------------------
Configuration Error
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.

Parser Error Message: Unrecognized configuration section 'appsettings'

Source Error:

Line 3:      
Line 4:    <system.web>
Line 5:        <appsettings>
Line 6:           <add key="myConn" value="Server=localhost;initial catalog=realestate;uid=sa;pwd=;" />
Line 7:      </appsettings>
 

Source File: c:\inetpub\wwwroot\realestate\web.config    Line: 5
----------------------------------------------------------------------------
0
 
stengeljCommented:
Don't put the <appsettings> inside any other tags other than the <configuration> tag.  You have it inside the <system.web> tag.

So,

<configuration>
   <system.web>
       ...
   </system.web>
   <appsettings>
       ...
   </appsettings
</configuration>
0
 
rammaAuthor Commented:
Sorry stengelj but i tried this also .. the same error still !!
this is web.config before adding the new lines :
----------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<configuration>
   
  <system.web>
      
    <compilation defaultLanguage="vb" debug="true"><assemblies><add assembly="CrystalDecisions.CrystalReports.Engine, Version=10.0.3300.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/><add assembly="CrystalDecisions.ReportSource, Version=10.0.3300.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/><add assembly="CrystalDecisions.Shared, Version=10.0.3300.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/><add assembly="CrystalDecisions.Web, Version=10.0.3300.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/><add assembly="CrystalDecisions.Enterprise.Framework, Version=10.0.3300.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/><add assembly="CrystalDecisions.Enterprise.InfoStore, Version=10.0.3300.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/></assemblies></compilation>

    <customErrors mode="RemoteOnly"/>

    <authentication mode="Windows"/>

    <authorization>
            
            <allow users="*"/>

    </authorization>


    <trace enabled="false" requestLimit="10" pageOutput="false" traceMode="SortByTime" localOnly="true"/>


    <sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" cookieless="true" timeout="20"/>

    <globalization requestEncoding="utf-8" responseEncoding="utf-8"/>

   
  </system.web>

</configuration>
------------------------------------------------------------------------
can you try on your machine ?
0
 
stengeljCommented:
My apologies, "appSettings" is case sensitive.  I tested the following and it works for me:

<?xml version="1.0" encoding="utf-8"?>
<configuration>  
    <system.web>
        <compilation defaultLanguage="vb" debug="true">
            <assemblies>
                <add assembly="CrystalDecisions.CrystalReports.Engine, Version=10.0.3300.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
                <add assembly="CrystalDecisions.ReportSource, Version=10.0.3300.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
                <add assembly="CrystalDecisions.Shared, Version=10.0.3300.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
                <add assembly="CrystalDecisions.Web, Version=10.0.3300.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
                <add assembly="CrystalDecisions.Enterprise.Framework, Version=10.0.3300.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
                <add assembly="CrystalDecisions.Enterprise.InfoStore, Version=10.0.3300.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
        </assemblies>
        </compilation>
        <customErrors mode="RemoteOnly"/>
        <authentication mode="Windows"/>
        <authorization>
        <allow users="*"/>
        </authorization>
        <trace enabled="false"
            requestLimit="10"
            pageOutput="false"
            traceMode="SortByTime"
            localOnly="true"/>
        <sessionState mode="InProc"
            stateConnectionString="tcpip=127.0.0.1:42424"
            sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes"
            cookieless="true"
            timeout="20"/>
        <globalization requestEncoding="utf-8" responseEncoding="utf-8"/>
    </system.web>
    <appSettings>
        <add key="myConn" value="Server=localhost;initial catalog=realestate;uid=sa;pwd=;" />
    </appSettings>
</configuration>

Also in my original post I said you could call it with:
<%# System.Configuration.ConfigurationSettings.AppSettings("myConn") %>

But it should have been:
<%= System.Configuration.ConfigurationSettings.AppSettings("myConn") %>

Sorry about that.  
0
 
rammaAuthor Commented:
Thank's alot ..
I really appreciate all your replies.

0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 5
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now