Friday, August 22, 2008

Call a Function in your MasterPage from your .aspx page

If you're going to be using a common function throughout the Web site, you can include the function in your MasterPage and gain access to it from each aspx that inherits from this MasterPage; like so:

CType(Me.Master, MasterPages_MasterPageName).FunctionOrSubName()


Simple as that!

Thursday, August 14, 2008

Randomize Query in SQL

Sometimes you want to return a bunch of data from a table, but you want it coming back in a random order. Maybe it's for keeping some content on a home page fresh. Here's how:

ssql="SELECT TOP 10 * FROM [someTable] ORDER BY NEWID()"


The key here is "ORDER BY NEWID()"

Loop through a DataReader

If you have stuff in a table, and you just want to loop through it, using a DataReader is your best bet:

Dim objDR As SqlClient.SqlDataReader
Dim objCommand As SqlClient.SqlCommand
Dim ConnectionString As String = "YOUR CONNECTION STRING HERE"
Dim objConnection As SqlClient.SqlConnection
Dim ssql As String

objConnection = New SqlClient.SqlConnection(ConnectionString)
ssql = "SELECT * FROM someTable"

If objConnection.State <> ConnectionState.Open Then
   objConnection.Open()
End If
objCommand = New SqlClient.SqlCommand(ssql, objConnection)
objDR = objCommand.ExecuteReader(CommandBehavior.CloseConnection)
objCommand = Nothing

If objDR.HasRows Then
   While objDR.Read()
      ' do your code here
      ' the following gives access
      ' to the table's field:
      ' objDR.Item("someField")
   End While
End If
objDR.Close()
objDR = Nothing