ASP.NET import code

C

cougar_1236

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

Nicole Schenk

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?
I think of a couple of possible ways:
1. You can extend the class that represents your page and added the
functionality at the class level,
2. You can create another class to do your db work by extending one of the
db classes, and then include an instance in your code.

I hope this helps
 
C

cougar_1236

I have no idea what you're talking about. Can you please explain
further, or provide an example?
 
A

Alvin Bruney [MVP]

..NET's model emphasizes class reuse, not code reuse. You will need to wrap
the *offending code in a class and build an assembly from it. Then, you can
import that assembly into your application. This new assembly would be a
unit capable of being re-used over and over again.

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ http://tinyurl.com/27cok
 
C

cougar_1236

Still have no idea what you're talking about. I know what the classes
are, but I have to idea how to put the code I have in a class and then
import that class and use it. I've tried everything I can think of.

.NET's model emphasizes class reuse, not code reuse. You will need to wrap
the *offending code in a class and build an assembly from it. Then, you can
import that assembly into your application. This new assembly would be a
unit capable of being re-used over and over again.

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ http://tinyurl.com/27cok
----------------------------------------------------------


I have no idea what you're talking about. Can you please explain
further, or provide an example?

one
of the
 
A

Alvin Bruney [MVP]

The exact functionality you are after is not supported in .NET.


--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ http://www.lulu.com/owc
----------------------------------------------------------


Still have no idea what you're talking about. I know what the classes
are, but I have to idea how to put the code I have in a class and then
import that class and use it. I've tried everything I can think of.

.NET's model emphasizes class reuse, not code reuse. You will need to wrap
the *offending code in a class and build an assembly from it. Then, you can
import that assembly into your application. This new assembly would be a
unit capable of being re-used over and over again.

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ http://tinyurl.com/27cok
----------------------------------------------------------


I have no idea what you're talking about. Can you please explain
further, or provide an example?

Nicole Schenk wrote:
(e-mail address removed) 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?
I think of a couple of possible ways:
1. You can extend the class that represents your page and added the
functionality at the class level,
2. You can create another class to do your db work by extending one
of the
db classes, and then include an instance in your code.

I hope this helps
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top