Tuesday 24 June 2008

Get Name of Month from Date - MonthName Function

God bless W3schools

I've just had a right old drama trying to work out how to get the name of a month rather than the Integer returned by Date.Month and apparently there is a built in function called MonthName.

Eg.
Dim lastmonth As String
lastmonth =
MonthName(Date.Today.Month - 1)
'so the value of lastmonth is now the name of last month.
source http://www.w3schools.com/Vbscript/func_monthname.asp

And the go-faster version:

'''
''' Gets name of previous month to current
'''

''' string
'''
Public Function GetLastMonthName() As String
Dim lastmonth As String
lastmonth = MonthName(Date.Today.Month - 1)
Return lastmonth
End Function

Monday 23 June 2008

SQL Wildcard Search

I'm always forgetting the syntax for wildcard queries in SQL Server 2005 so here's an example:

SELECT * FROM myTable WHERE (myColumn LIKE '%' + @str + '%')

DBNull

I keep having to google this bad-boy every time it comes up so thought I'd make a note of it here.

Let's say you've got a custom object which is populated using a DataReader, unless you've got a crystal ball it's quite likely that you will end up with some NULL values in your database. Here's what to do about it:

If Not IsDBNull(rdr("Telephone")) Then

m.Tel = rdr("Telephone")
Else

m.Tel = ""
End If

Security - Stop Secure Page Displaying After Logout - Browser Back Button Drama

The title for this post should be fairly self-explanatory but here goes anyway. Let's say you've given up tearing your hair out trying to use Microsofts' new Membership / Role Provider controls set to implement forms authentication and have gone ahead and created your own custom user authentication system.

In your secure area you have a logout button. The user clicks logout and gets re-directed somewhere. Great so far, however when they click the back button in their browser the "secure" page they were looking at before they logged out can still be viewed. Aaaaaaaargh!

Well here's the solution, place the following code in your secure pages page load event and the problem should go away:

Response.Buffer = True
Response.ExpiresAbsolute = DateTime.Now.AddDays(-1)
Response.Expires = -1500
Response.CacheControl = "no-cache"

' in this case I simply check whether my custom login logic is returning true or false, you would 'need to write your own code here.

If CustomLogin = false then
response.redirect("login.aspx")
end if

PS. This doesn't seem to work in Firefox , aaaaaaaargh.

However, this fixes it:

HttpContext.Current.Response.Cache.SetNoStore()

Hallelujah!


Wednesday 18 June 2008

GridView - PageIndexChanging

When you enable paging on a GridView control you are more than likely to come accross this exception: "The GridView 'gvBla' fired event PageIndexChanging which wasn't handled. "

Here's how to sort it out.

In your code behind select your GridView controls' PageIndexChanging Event and set it's PageIndex to e.NewPageIndex. Don't forget to rebind your data to the GridView also.

Protected Sub gvBla_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvgvBla.PageIndexChanging
Me.gvBla.PageIndex = e.NewPageIndex
' call your data bind sub routine here

End Sub