ramma
asked on
simple include in asp.net
hi..
i want to include conn.aspx inside index.aspx using vb.net syntax.
any idea ?
i want to include conn.aspx inside index.aspx using vb.net syntax.
any idea ?
Excuse me:
Web Forms Syntax Reference:
http://samples.gotdotnet.com/quickstart/aspplus/doc/webformssyntaxref.aspx
Server-Side Include Syntax: <-- #Include File="conn.aspx" -->
Bob
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
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
Bob
oh, and make sure your module is Public
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.?
in my index.aspx i connect to my sqlserver database using this line :
Conn = "Server=localhost;initial catalog=realestate;uid=sa;
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;in itial catalog=realestate;uid=sa; pwd=;" />
</appsettings>
</configuration>
Reference in your .aspx page:
<%# System.Configuration.Confi gurationSe ttings.App Settings(" myConn") %>
Useage Example:
Dim strConn = System.Configuration.Confi gurationSe ttings.App Settings(" myConn")
Dim MyConn as New OleDBConnection (strConn)
If you MUST do this with an include, TheLearnedOne's example is correct.
In web.config:
<configuration>
<appsettings>
<add key="myConn" value="Server=localhost;in
</appsettings>
</configuration>
Reference in your .aspx page:
<%# System.Configuration.Confi
Useage Example:
Dim strConn = System.Configuration.Confi
Dim MyConn as New OleDBConnection (strConn)
If you MUST do this with an include, TheLearnedOne's example is correct.
ASKER
I wrote this segment in web.config after <system.web>:
<configuration>
<appsettings>
<add key="myConn" value="Server=localhost;in itial 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;in itial catalog=realestate;uid=sa; pwd=;" />
Source File: c:\inetpub\wwwroot\realest ate\web.co nfig Line: 3
Any idea ?
<configuration>
<appsettings>
<add key="myConn" value="Server=localhost;in
</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;in
Source File: c:\inetpub\wwwroot\realest
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
Just put the appsettings section in the file.
Bob
onlt write
<appsettings>
<add key="myConn" value="Server=localhost;in itial 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.AppS ettings("m yConn")
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.D ata.Comman dBehavior. CloseConne ction)
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
<appsettings>
<add key="myConn" value="Server=localhost;in
</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.AppS
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.D
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
ASKER
I wrote these 3 lines after <system.web>
<appsettings>
<add key="myConn" value="Server=localhost;in itial atalog=realestate;uid=sa;p wd=;" />
</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;in itial catalog=realestate;uid=sa; pwd=;" />
Line 7: </appsettings>
Source File: c:\inetpub\wwwroot\realest ate\web.co nfig Line: 5
-------------------------- ---------- ---------- ---------- ---------- ----------
<appsettings>
<add key="myConn" value="Server=localhost;in
</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;in
Line 7: </appsettings>
Source File: c:\inetpub\wwwroot\realest
--------------------------
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>
So,
<configuration>
<system.web>
...
</system.web>
<appsettings>
...
</appsettings
</configuration>
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 .CrystalRe ports.Engi ne, Version=10.0.3300.0, Culture=neutral, PublicKeyToken=692fbea5521 e1304"/><a dd assembly="CrystalDecisions .ReportSou rce, Version=10.0.3300.0, Culture=neutral, PublicKeyToken=692fbea5521 e1304"/><a dd assembly="CrystalDecisions .Shared, Version=10.0.3300.0, Culture=neutral, PublicKeyToken=692fbea5521 e1304"/><a dd assembly="CrystalDecisions .Web, Version=10.0.3300.0, Culture=neutral, PublicKeyToken=692fbea5521 e1304"/><a dd assembly="CrystalDecisions .Enterpris e.Framewor k, Version=10.0.3300.0, Culture=neutral, PublicKeyToken=692fbea5521 e1304"/><a dd assembly="CrystalDecisions .Enterpris e.InfoStor e, Version=10.0.3300.0, Culture=neutral, PublicKeyToken=692fbea5521 e1304"/></ assemblies ></compila tion>
<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="tcp ip=127.0.0 .1:42424" sqlConnectionString="data source=127.0.0.1;Trusted_C onnection= yes" cookieless="true" timeout="20"/>
<globalization requestEncoding="utf-8" responseEncoding="utf-8"/>
</system.web>
</configuration>
-------------------------- ---------- ---------- ---------- ---------- ------
can you try on your machine ?
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><
<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="tcp
<globalization requestEncoding="utf-8" responseEncoding="utf-8"/>
</system.web>
</configuration>
--------------------------
can you try on your machine ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank's alot ..
I really appreciate all your replies.
I really appreciate all your replies.
Cheers!