Monday, December 22, 2008

Validate Decimals with Javascript

Validating a decimal such as a measurement of time or currency (10.5 or 0.4) is as easy as:

function valNum(inNum {
   var RegEx = /[0-9]{1,8}\.?[0-9]{0,2}/;
   var ValidNumber = inNum.match(RegEx);
   return ValidNumber;
}

This function will return the correct number. If you feed in "10.5foo" the function will spit back 10.5. If you feed in "bar" the function will return nothing.

Tuesday, November 18, 2008

Automatically Scroll to the bottom of a DataGridView

DataGridView.CurrentCell = DataGridView(1, DataGridView.RowCount - 1)


Where DataGridView is the instance name of your DataGridView component. You're passing in Column and Cell selection into DataGridView()

Friday, October 24, 2008

Generate a Random String

If you need to generate a random string of mixed characters, this simple function will do it for you:
Public Function GenerateRandomString() As String
   GenerateRandomString= System.Guid.NewGuid.ToString
End Function

This will return something like this: 80a7a705-9c23-47bc-92b6-2b5c4f9d728d. Which you then can do various string functions to make it the way you want.

Thursday, October 23, 2008

Formatting Dates in GridView

If you have a date coming back to your GridView and it displays "11/01/2008 12:00am" and you'd rather just display the date and not the time, you can fill in the DataFormatString field when editing the column, with: {0:MM/dd/yyyy}

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