Click here to Skip to main content
16,020,840 members
Articles / Programming Languages / ASP
Article

Avoid using same code in various pages

Rate me:
Please Sign up or sign in to vote.
3.22/5 (9 votes)
2 May 20032 min read 51.9K   16   4
Avoid using same code in various pages

Introduction

Sometimes we're anxious to create some good applications, and we forget simple things like, code being repeated in various pages. For example - when we want to create a connection to Database, we write the same code in all the ASP pages, which can be avoided easily, by creating a separate .asp or .inc page with the instructions, and then we can include (SSI - Server Side Includes) the file in all our ASP pages.

I use the technique of SSI quite often, because it helps me a lot, on changing or renewing the site.

I'm new learner in ASP, and I've realized this common mistake in all the applications I've been checking, and some of the applications are made by people working with ASP for so many years. But, I'm not writing this article to point mistakes made by others, rather I would prefer that programmers like me, at the beginning stage, read this simple but effective article and avoid these kind of mistakes, I hope somehow I can help them & of course others who are more experienced.

Now, I'll try to explain my point of view:

Step 1

Create a file, you can name it - connection.asp; and you can put the following code into it:

VBScript
<%
dim conn, connstring 
'declaration of variables
set conn = server.createobject("ADODB.connection")
connstring = "provider = microsoft.jet.OLEDB.4.0; data source = _
    " & server.mappath("../yourDBfolder/yourDB.mdb")
%>

Step 2

You can include this file, in any of your ASP files, by the following code:

HTML
<!--#include file="connection.asp"-->

Step 3

After the inclusion of your path to Database, you can write the other code to call the Recordset, SQL statements, to fetch data from your Database, like

VBScript
<%
'open the connection
conn.open = connstring
dim rs
set rs = server.CreateObject("adodb.recordset")
sqlstring = "select * from tablename"
rs.Open sqlstring,conn
%>

This is what I've done so far, and realized that many programmers write the code we wrote in first file, many times thereby repeating it through many pages, but when comes the time to change the path to your database, or other changes, then instead of changing in so many files, you just change in one file, and it will affect all the others adversely.

Conclusion

So far I've been using ACCESS 2000, but when I start dealing with more data, then I'll start working with SQL Server, or other Database. I've created my own website: http://www.kundan.web.pt/. I sincerely hope I managed to help a little by this simple but effective article. Sorry for my poor English, I'm uses to writing and speaking in Portuguese. If I get a good response for this article, hopefully I'll try to write some more articles for beginners like me.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Portugal Portugal
I'm from Lisbon-Portugal. I started my career as Computer Instructor in 1998- presentely I'm teaching Operating Systems (all except Linux, Microsoft Office Applications (all), Networking and Internet technology - as user and as programmer- to create websites - using Frontpage. In the beggining I also teached VB 5.0, so I've good knowledge of programming, but still, I feel that I need to learn more & more.
I gave my first ASP basic course (duration of 60 hours), in March/April 2003. And then I found CODEPROJECT.com, because I was looking for basics ASP, to teach them, and nowadays it is very hard to find material about ASP for begginers, so I started writing this little articles, hoping somebody will use it to teach ASP.
Still I'm a ASP begginer and quick learner trying to improve by the experience gained by others, people who started more time ago.... and I learn from their applications, try to improve them, and try to use them for teaching ASP, step by step because they are very well commented...( that is good habit, after all)
Maybe if anybody wants to share their experience, and provide me with their work, I would appreciate, it will be used for Educational purpose only.
Thanks for reading about Me.

Comments and Discussions

 
QuestionIs there a way to have variable includes? Pin
Dimitris Vasiliadis6-May-03 7:18
Dimitris Vasiliadis6-May-03 7:18 
GeneralFactorization is good but... Pin
Stephane Rodriguez.4-May-03 0:01
Stephane Rodriguez.4-May-03 0:01 
GeneralRe: Factorization is good but... Pin
Uwe Keim4-May-03 3:46
sitebuilderUwe Keim4-May-03 3:46 
GeneralRe: Factorization is good but... Pin
Daniel Turini4-May-03 6:09
Daniel Turini4-May-03 6:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.