Monday, February 16, 2009

ASP's Fast Way To Pull Data from Database

This is a great way to pull in all records from a (MSSQL) database in ASP.

use the GetString() function for your record set!  It is pretty simple and really cool.  Here is my example:

set rs = Server.CreateObject("ADODB.Recordset")
myvariable = rs.GetString (2, , vbTab, vbCrLf, "Null")

Here is the info on the function GetString:

string = recordsetobject.GetString (StringFormat, NumRows, ColumnDelimiter, RowDelimiter, NullExpr)

For more details go here: DevGuru ADO Recordset::GetString Method

MSSQL Searching for Datetime value

This is a great resource for trying to make a query in SQL Server for the datetime data type.  Use this if you're trying to find all entries from a date like yesterday, last month, or anything else.


What I found even more useful is this little code:

query = "SELECT * FROM your_table WHERE DATEDIFF(dd, your_date_field, GETDATE()) = 0"

What that does, is get you all entries from your_table where your_date_field and the current date (today's date) are not different.  The 'dd' means day, so it is comparing the day.

This will get all entries from yesterday:

query = "SELECT * FROM users WHERE DATEDIFF(dd, datecreated, GETDATE()) = 1"

And this will get from last week:

query = "SELECT * FROM users WHERE DATEDIFF(ww, datecreated, GETDATE()) = 1"

For a full list of what DATEDIFF does take a look at this: DATEDIFF (Transact-SQL)

Have fun!

Fast String in ASP

ASP is an old framwork and really outdated but sometimes we still need to use it.  One major problem is the handling of strings.  We recently worked on a project with ASP and tried to use the standard string method to create some reports.  Basically, a report with 1,000 entries timedout, until we set the server timeout to 10 minutes...!

So here is a way to overcome this.  This is a class that you can use if you want to create strings and need to concatenate them.

Class FastString
Dim stringArray,growthRate,numItems
Private Sub Class_Initialize()
growthRate = 50: numItems = 0
ReDim stringArray(growthRate)
End Sub
Public Sub Append(ByVal strValue)
' next line prevents type mismatch error if strValue is null. Performance hit is negligible.
strValue=strValue & ""
If numItems > UBound(stringArray) Then ReDim Preserve stringArray(UBound(stringArray) + growthRate)
stringArray(numItems) = strValue:numItems = numItems + 1
End Sub
Public Sub Reset
Erase stringArray
Class_Initialize
End Sub
Public Function concat()
Redim Preserve stringArray(numItems)
concat = Join(stringArray, "")
End Function
End Class 

I will give full credit to: A Fast String Class for ASP Pages

If you want to implement this class, do something like this:

Set mystring = New FastStrign
mystring.Append("My Info")
mystring.concat()
Set mystring = nothing

This will add "My Info" to the string and then print it.  The last line is to detroy the old thing, you can also use the build in erase function.  This will make concatenating of strings much faster.

Remember I said 10 minutes...? Now it takes 20 seconds to create the report!

Thursday, February 12, 2009

Resources for RSS Feed Creation

Here are some resources on how to create a RSS 2.0 feed.

Automatic and Dynamic Title URLs from Titles using Javascript

This is a handy little javascript snipplet that will convert text in one text field to url friendly text in another.  This is very useful if you're trying to automatically create title URLs from the title of your post.  This helps with search engine optimization by giving you friendly URLs.  Below is the javascript and a test form.  You can copy it all, paste it, and test it.

<html>
<head>
<script type="text/javascript">
function urltitle(title)
{


// Create the url friendly title
var url = title
.toLowerCase() //change everything to lowercase
.replace(/^\s+|\s+$/g, "") //trim leading and trailing spaces
.replace(/[&]+/g, "and") //replace ampersand
.replace(/[#]+/g, "sharp") //replace pound
.replace(/[@]+/g, "at") //replace at
.replace(/[%]+/g, "percent") //replace percent
.replace(/[+]+/g, "plus") //replace plus
.replace(/[-|\s]+/g, "_") //replace spaces and hyphens to underscore
.replace(/[^a-z0-9_]+/g, "") //remove all non-alphanumeric characters except the underscore
.replace(/[_]+/g, "_") //remove duplicate underscores
.replace(/^_+|_+$/g, "") //trim leading and trailing underscores
;
document.getElementById('title_url').value = url;
}
</script>
</head>
<body>


<input type="text" onkeyup="javascript:urltitle(this.value);" />
<input type="text" id="title_url" />


</body>
</html>


As we usually give credits for what we find, this is where we got the original script before modifying it: Rewrite input to friendly URL

Hide Link Dotted Borders in Firefox

You may have noticed that links in firefox have a light dotted border around them.  It was intended for accessibility, so that you'd know which link you're currently focused on.  Try tabbing through a webpage and you'll see its use.

Anyway, sometimes the border is annoying, ugly, and useless.  In such cases, you can remove the border by using this piece of CSS:

outline-style:none;

Tuesday, February 10, 2009

Simple Javascript Back Link

Alright, this is pretty simply and most people know it but hey, never hurts to post.

If you want to create a link that goes back to the previous page (just like the browser back button) simply use this code in your link tag:

href="javascript: history.go(-1)"