consultancy chorley ASP.NET Server Side Scripting Wigan
Website Development Radcliffe

A Coder's Notes

Email :
bespoke invoice systems Standish Information Technology Advice Worsley
    development systems Ramsbottom
data manipulation Farnworth access databases Whitefield SQL connections Atherton
online accessible software Leyland software compatible Coppull

Get connected and Online Tottington
remote management Makerfield code and design Aspull

They Joy of ASP.NET's APP_CODE Folder - 30/09/2010

Ahhhh...that's better...

I program a lot in ASP.NET and of course many pages use exactly the same code for the most part, there a usually a few minor changes to each page.  This means a lot of copy and paste - OR -  a lot of re-typing.  Then I discovered the app_code folder.

In the app_code folder of an ASP.NET website (most real techies call this an "app") I can place a ".vb" file.  This .vb file contains code just as you'd expect to see it in any other part of the website...being vb in my case it's cisual basic code.  Then we create classes.  Being self taught I'm not quite sure what a class is...I mean I get the idea I just could not tell you exactly.  But these classes can be accessed from any other page on the website.

Here, let me show you, it will be easier...

Here's the code inside "test.vb" which lives in the app_code folder...

public Class myclass

dim public shared connstring as string = "Provider = Microsoft.Jet.OLEDB.4.0; data source = " & httpcontext.current.Server.MapPath("/mydatabases.mydatabase.mdb")
dim public shared myoledbdatareader as oledbdatareader
dim public shared myoledbconnection as New OleDbConnection (connstring)

Public shared Function myreader(sqlstring as string)
 dim mycommand as New OleDbCommand(sqlstring, myoledbconnection)
 myoledbdatareader  = mycommand.ExecuteReader()
End Function

end Class

So that small snippet of code sits insed the app_code folder.  Outside of the app_code folder I can now access the code inside the folder.  I may have a web page called mypage.aspx, and inside this page I wish to access my database, so now I can use this code.

dim mysql as string
mysql = "SELECT * FROM mytable"


mytable.datasouce = myclass.myoledbdatareader


This is considerably shorter than the usual code I have to repeat endlessly!


Post A Comment

Name Comment
Julian Thanks, I am also self taught and don't understand half of the tech terminolgy I read. Your example is so simple. Just what I've been searching for.
programming services specialist
Valid XHTML 1.0 Transitional