Sunday, February 17, 2008

Simple Port Scan

The following function will check to see if connectivity is enabled for a particular host on a particular port via TCP/IP:

Function ScanPort(ByVal strHost As String, ByVal intPort As Integer) As Boolean
    Dim aobjIPs() As System.Net.IPAddress = System.Net.Dns.GetHostEntry(strHost).AddressList
    If aobjIPs.Length > 0 Then
        Dim objEndpoint As New System.Net.IPEndPoint(aobjIPs(0), intPort)
        Using s As New System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp)
            Try
                s.Connect(objEndpoint)
                Return s.Connected
            Catch ex As Exception
                Return False
            End Try
        End Using
    Else
        Return False
    End If
End Function

Thursday, January 31, 2008

JavaScript button disable with postback

When an ASP.NET server-side button is given client-side JavaScript code to execute when handling onclick, the button no longer posts back by default. However, there is a small piece of code you can add to the onclick attribute of the button to re-enable the postback behavior.

Adding this code in the Page_Load method will remove a server-side button's postback call:

btnConfirm.Attributes.Add("onclick", "Confirm_Click();")

But writing the code like this will reinstate the postback call:

btnConfirm.Attributes.Add("onclick", "Confirm_Click(); " + ClientScript.GetPostBackEventReference(btnConfirm, ""))

FileUpload control in an UpdatePanel

By nature, FileUpload controls require an entire page postback to submit the file contents, so they are explicitly excluded from support within an UpdatePanel...however, if you put the Submit button that performs the postback intended to upload the file into a PostbackTrigger, you can place your FileUpload control within the UpdatePanel.


<asp:UpdatePanel runat="server">
    <ContentTemplate>
        <asp:FileUpload runat="server" ID="fupTest" />
        <asp:Button runat="server" ID="btnSubmit" Text="Upload"/>
    </ContentTemplate>
    <Triggers>
        <asp:PostBackTrigger ControlID="btnSubmit" />
    </Triggers>
</asp:UpdatePanel>

Wednesday, January 9, 2008

Transactions and strongly typed TableAdapters

To do this, create a new class file containing a Partial Class declaration with the name of your table adapter. Add this code to the class:


Private _transaction As SqlTransaction

Private Property Transaction() As SqlTransaction
Get
Return Me._transaction
End Get
Set(ByVal Value As SqlTransaction)
Me._transaction = Value
End Set
End Property

Public Sub BeginTransaction()
' Open the connection, if needed
If Me.Connection.State <> ConnectionState.Open Then
Me.Connection.Open()
End If
' Create the transaction and assign it to the Transaction property
Me.Transaction = Me.Connection.BeginTransaction()
' Attach the transaction to the Adapters
For Each command As SqlCommand In Me.CommandCollection
command.Transaction = Me.Transaction
Next
Me.Adapter.InsertCommand.Transaction = Me.Transaction
Me.Adapter.UpdateCommand.Transaction = Me.Transaction
Me.Adapter.DeleteCommand.Transaction = Me.Transaction
End Sub

Public Sub CommitTransaction()
' Commit the transaction
Me.Transaction.Commit()
' Close the connection
Me.Connection.Close()
End Sub

Public Sub RollbackTransaction()
' Rollback the transaction
Me.Transaction.Rollback()
' Close the connection
Me.Connection.Close()
End Sub

Monday, December 3, 2007

Min-height fast hack (IE6 sucks)

Blatantly plagiarized from http://www.dustindiaz.com/min-height-fast-hack/:

selector {
min-height:500px;
height:auto !important;
height:500px;
}

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)