I know in PHP it's possible to import another PHP file, so that you
don't have to type the same code over and over again. I've been trying
to find out how to do this with ASP.NET but haven't found a way. The
code I have so far is:
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">

' Insert page code here
'
Sub Page_Load(sender as Object, e as EventArgs)
Dim connString as String
connString = "Provider=Microsoft.Jet.OLEDB.4.0;DATA
SOURCE=c:\phone.mdb;"

Dim objConnection as OleDbConnection
objConnection = New OleDbConnection(connString)
objConnection.Open()

Dim strSQL as String = "SELECT * FROM staff"

Dim objCommand as OleDbCommand
objCommand = New OleDbCommand(strSQL, objConnection)

Dim objDataReader as OleDbDataReader
objDataReader =
objCommand.ExecuteReader(CommandBehavior.CloseConnection)

lstNames.DataSource = objDataReader
lstNames.DataBind()

End Sub

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<!-- Insert content here -->
<asp:listbox id="lstNames" runat="server"
DataValueField="FIRSTNAME" DataTextField="FIRSTNAME"
Rows="1"></asp:listbox>
</form>
</body>
</html>

I'd like to be able to chop out the code for the database connection,
so that it starts right at lstNames.DataSource
How do I go about doing this?

RE: ASP.NET import code by NoSpamMgbworld

NoSpamMgbworld
Wed Jan 26 08:41:04 CST 2005

Create additional classes. For data, downloading the Microsoft Data Access
Application block is a good start. You can then create business classes that
use the block to return data to pages.

If you want to merely pass through, make the methods in the class static
(Shared keyword in Visual Basic).


---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************

"cougar_1236@yahoo.com" wrote:

> I know in PHP it's possible to import another PHP file, so that you
> don't have to type the same code over and over again. I've been trying
> to find out how to do this with ASP.NET but haven't found a way. The
> code I have so far is:
> <%@ Page Language="VB" %>
> <%@ Import Namespace="System.Data" %>
> <%@ Import Namespace="System.Data.OleDb" %>
> <script runat="server">
>
> ' Insert page code here
> '
> Sub Page_Load(sender as Object, e as EventArgs)
> Dim connString as String
> connString = "Provider=Microsoft.Jet.OLEDB.4.0;DATA
> SOURCE=c:\phone.mdb;"
>
> Dim objConnection as OleDbConnection
> objConnection = New OleDbConnection(connString)
> objConnection.Open()
>
> Dim strSQL as String = "SELECT * FROM staff"
>
> Dim objCommand as OleDbCommand
> objCommand = New OleDbCommand(strSQL, objConnection)
>
> Dim objDataReader as OleDbDataReader
> objDataReader =
> objCommand.ExecuteReader(CommandBehavior.CloseConnection)
>
> lstNames.DataSource = objDataReader
> lstNames.DataBind()
>
> End Sub
>
> </script>
> <html>
> <head>
> </head>
> <body>
> <form runat="server">
> <!-- Insert content here -->
> <asp:listbox id="lstNames" runat="server"
> DataValueField="FIRSTNAME" DataTextField="FIRSTNAME"
> Rows="1"></asp:listbox>
> </form>
> </body>
> </html>
>
> I'd like to be able to chop out the code for the database connection,
> so that it starts right at lstNames.DataSource
> How do I go about doing this?
>
>

Re: ASP.NET import code by cougar_1236

cougar_1236
Wed Jan 26 09:39:04 CST 2005

I tried using classes, but it doesn't seem to allow me to do what I
want to do. Can you show me an example of how to create a class with
the code I have there? All I want is to import the database commands,
but when I try to import my class it doesn't recognize any of the
variables that were declared. I tried instantiating it as an object but
that didn't work either.