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:
<%
dim conn, connstring
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:
<!--
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
<%
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.
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.