Link to home
Start Free TrialLog in
Avatar of ramma
ramma

asked on

simple include in asp.net

hi..
i want to include conn.aspx inside index.aspx using vb.net syntax.
any idea ?
Avatar of vadivhere
vadivhere

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!
Avatar of Bob Learned
Excuse me:

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

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

Bob

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
I agree with using User Controls, I was just pointing out that Includes are still supported.

Bob
oh, and make sure your module is Public
Avatar of ramma

ASKER

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.?

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.
Avatar of ramma

ASKER

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 ?
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
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
Avatar of ramma

ASKER

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
----------------------------------------------------------------------------
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>
Avatar of ramma

ASKER

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 ?
ASKER CERTIFIED SOLUTION
Avatar of stengelj
stengelj
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ramma

ASKER

Thank's alot ..
I really appreciate all your replies.