Monday, November 5, 2007

T-SQL IP octet split

Found a neat piece of T-SQL that can be used to separate the octets of an IP address:

Declare @IP varchar(20)
Select @IP = '239.147.8.165'
select @IP AS IP,
cast(substring(@IP, 1, charindex('.', @IP) - 1) as int) AS Octet1,
cast(substring(@IP, charindex('.', @IP) + 1,
charindex('.', @IP, charindex('.', @IP) + 1) -
charindex('.', @IP) - 1) as int) as Octet2,
cast(reverse(substring(reverse(@IP), charindex('.', reverse(@IP)) + 1,
charindex('.', reverse(@IP), charindex('.', reverse(@IP)) + 1) -
charindex('.', reverse(@IP)) - 1)) as int) AS Octet3,
cast(reverse(substring(reverse(@IP), 1, charindex('.', reverse(@IP)) - 1)) as int) as Octet4

The source for this is http://www.umachandar.com/technical/SQL6x70Scripts/Main.htm
.

Thursday, October 18, 2007

Auto-refresh an ASPX page

This is especially useful when using a master page but you don't want every page using the master page to refresh.

In the Page_Load() event handler, add this code:

'Refresh the page every 30 seconds.
Dim lit As New LiteralControl("<meta equiv=""refresh"" content=""30"" />")
Header.Controls.Add(lit)

Sunday, October 14, 2007

Startup object must be a form when "Enable Application Framework" is checked.

If you're reusing a form and are trying to make it the main form of an application, and you get this error, ensure that the constructor for the form takes no arguments.

Tuesday, August 21, 2007

Programmatically open Access and show a table

Just a note on one way of accomplishing this:

Dim objAccess As Object = CreateObject("Access.Application")
objAccess.OpenCurrentDatabase(strFileName)
objAccess.Visible = True
objAccess.DoCmd.Maximize()
objAccess.DoCmd.OpenTable(strTableName)

Wednesday, August 15, 2007

Console and Windows Form in the same application

I recently needed to write a program that had an attended and unattended mode:

1. Create a console application in Visual Studio .NET.

2. Use this as a template:

Module Module1

    Sub Main()
        If Command$() = "-winform" Then
            Dim frm As New testform()
            Windows.Forms.Application.Run(frm)
        Else
            Console.WriteLine("this is the console option")
        End If
    End Sub

End Module

Saving window settings in My.Settings

This is useful for saving window position and size when an application closes and then restoring these settings when the application starts again.

1. In app.config, create three new settings:
- IsMaximized (Boolean)
- MainFormPosition (System.Drawing.Point)
- MainFormSize (System.Drawing.Size)

2. In the form's Load event handler, add the following code:

If My.Settings.MainFormPosition <> Nothing _
        AndAlso My.Computer.Screen.Bounds.Contains(My.Settings.MainFormPosition) Then
    Me.StartPosition = FormStartPosition.Manual
    Me.Location = My.Settings.MainFormPosition
    Else
    Me.StartPosition = FormStartPosition.CenterScreen
End If
If My.Settings.IsMaximized <> Nothing _
        AndAlso My.Settings.IsMaximized Then
    Me.WindowState = FormWindowState.Maximized
Else
    Me.WindowState = FormWindowState.Normal
End If
If My.Settings.MainFormSize <> Nothing Then
    Me.Size = New Size(Math.Max(100, My.Settings.MainFormSize.Width), Math.Max(100, My.Settings.MainFormSize.Height))
End If

3. In the form's FormClosing event handler, add the following code:

If Me.WindowState = FormWindowState.Maximized Then
    My.Settings.IsMaximized = True
    My.Settings.MainFormPosition = New Point(Me.RestoreBounds.Left, Me.RestoreBounds.Top)
    My.Settings.MainFormSize = New Size(Me.RestoreBounds.Width, Me.RestoreBounds.Height)
Else
    My.Settings.IsMaximized = False
    My.Settings.MainFormPosition = Me.Location
    My.Settings.MainFormSize = Me.Size
End If

Turning off auto-scrolling in a bound DataGridView

I was running into an issue where I didn't want the grid to auto-scroll or change the selected row when the underlying DataSource property was changed. Here's what I came up with:


' dgv is a DataGridView control with SelectionMode set to FullRowSelect and MultiSelect set to False.
Dim intSelectedIndex As Integer = 0
If dgv.SelectedRows.Count > 0 Then
    intSelectedIndex = dgv.SelectedRows(0).Index
End If
Dim intDisplayIndex As Integer = dgv.FirstDisplayedScrollingRowIndex
' Set dgv.DataSource here
dgv.ClearSelection()
If intSelectedIndex < dgv.Rows.Count Then
    dgv.Rows(intSelectedIndex).Selected = True
Else
    dgv.Rows(dgv.Rows.Count - 1).Selected = True
End If
If intDisplayIndex > -1 Then
    If intDisplayIndex <>
        dgv.FirstDisplayedScrollingRowIndex = intDisplayIndex
    Else
        dgv.FirstDisplayedScrollingRowIndex = dgv.Rows.Count - 1
    End If
End If