April 30, 2006
Displaying Records in Random Order using ASP
One of my client had a requirement of displaying a list of Products in a Random Order. He had around 50 products which he wanted to display on the same page but the products should appear in random order each time the page is refreshed.
Here is the code which I used, to do the work
<%
Dim rs
Dim ctr
Dim cnnstr
Dim cnn
cnnstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("products.mdb") & ";"
set cnn = Server.CreateObject ("ADODB.Connection")
cnn.Open cnnstr
set rs=Server.Createobject("Adodb.Recordset")
rs.open "SELECT * FROM Products",cnn,1,1
ctr=rs.RecordCount
Dim a1
ReDim a1(ctr)
Do Until ctr = 0
Randomize
rnd_no = Int(Rnd*rs.RecordCount) +1
ctr=ctr-1
if rnd_no>rs.RecordCount then rnd_no=1
rs.MoveFirst
rs.move rnd_no
if a1(rnd_no)=1 then
while a1(rnd_no)=1
rnd_no=rnd_no+1
if not rs.eof then rs.MoveNext
if rnd_no>rs.RecordCount then
rnd_no=1
rs.MoveFirst
end if
wend
end if
if not rs.eof and not rs.bof then
a1(rnd_no)=1
%>
// Display the recordset Here.
<%
end if
loop
rs.Close()
Set rs = Nothing
cnn.close
%>
This code works like a charm for around 50 records. Didnt tested it for more.
Now in this code I am using a array 'a1' to mark the recordset which has been displayed. This is required since sometimes the Random funtion calculation gives the same number back, so the same record may get printed more than once. This was undesirable. So by using this array and marking each record after printing ensures that each record is printed once only.
Do tell me your reviews on this code.
April 27, 2006
Changing Page across Frames through ASP
During the development of ApplyTeacher.com, I had a requirement to change the webpage in a different Frame. (After successful Login, the menu in top frame needs to be refreshed so as to change Login to Logoff). Also the login page needs to be redirected to the user page.
The following code did the trick
response.write(vbCrLf & "< script >" & vbCrLf)
response.write("parent.frames['
response.write("< / script >" & vbCrLf)
P.S. Remove the extra spaces in script tag.
Replace the
This helped me to change the webpage in another frame and for the current frame (through which the user logged in), I simple used used response.redirect to send the user to his account page.
This script works fine in Internet Explorer 6 and Firefox 1.5 but it should work on all browsers.
April 25, 2006
SQL Lesson Learnt - Reserved Keywords
I ran into a petty trouble with SQL today which took me almost 3 hours to solve.
I was working on a Web Application for a client. Everything was going fine, except for two pages that were misbehaving in the SQL Query. (I am using MS Access 2003 as a test database)
Query was pretty straighforward:
* select * from language
* select * from tutor where language like 'English'
The ASP page gave me the message "Undefined error.". I kept scraching my head for nothing. At last I solved the issue by renaming the table name to "laguagetbl" and field name to "languagefld". That was the trick which solved the "Undefined error". I wish if there was some user friendly error message.
It took me so much time find out that "language" was a reserved keyword.
My Advise is be careful while keeping the table names and field names in your database. You never know when trouble is waiting.
Freelancing - Working Online and Making Money
Being a Graduate student, I had keen interest in development work. I always wanted to get involved in various types of projects. And it is always nice if you can make some money out of the projects you do.
Then I came across two such websites through which I could get what I was looking for.
* RentACoder
* Scriptlance
These websites provides opportunities to developers worldwide to get their hands on various types of projects that are of interest to them. Well that’s not as easy as it seems. To get a project you need to make a bid (Yes, make a Bid). The buyer reviews the bid by various people, sees their portfolios before accepting the bid. Now how to get a good rating at such a site? Well, you can get a rating directly from the buyer only after completing a project. A buyer may rate you from 1-10 (10 being best) after he accepts the complete project.
But about payment, which is an important factor that drives people there. These sites provides us with various option such as a Wire Transfer, Direct cheque, Paypal Transfer, etc. (Check the respective sites for details). Personally I prefer Paypal, since it is fast and easy. After receiving the payment using Paypal, I can do online shopping also. (Now that’s cool). Most of the sites accept payment through Paypal.
What is the guaranty that I will receive the payment from my Buyer? Well sometimes a dispute may occur, whether a buyer may refuse to accept the deliverables or the coder may be unable to complete the work on time. In such cases, RentACoder has an arbitration mechanism in which the arbitrator looks into the details of the project and the deliverables and makes a decision. And the person who looses the arbitration gets a bad rating. So, it is generally advisable that people use Escrow as payment option so that money is safe for both – the buyer and developer.
All this seems easy, but now I’ll say about the most difficult part. How to get your bid accepted? Plenty of people bidding for a project with different amount and time duration. Well, it depends on your current rating, your existing work, bidding amount and of course LUCK.
What does these websites get by providing this facility? Registration is generally free for the developers (I don’t have any buyer account). Well they deduct a percent of the bidding amount to keep themselves going on.
These days there are many different sites coming up with similar idea. A simple search leads to – www.elance.com, www.getafreelancer.com, www.getacoder.com, www.indianlance.com, etc
Happy Bidding !!!
(P.S. - I might be wrong, but thats just my opinion. This is my first blog post, will post soon more on current research areas - Grid Computing)