.NET code (RSS)

.NET code

favorite classes in .NET 2.0

reject a user/web request with following classes 

IPAddressRestriction

IPDomainRestriction
IPRestriction
IPRestrictionCollection
IPSecurity

to insert lots of stuff fast into sql server use (using some funnel pattern):

SqlBulkCopy
SqlBulkCopyColumnMapping
SqlBulkCopyColumnMappingCollection

 

Download manager in C# with BITS (Background Intelligent Transfer Service)

Download Manager in C#

I' ve used the wrapper of the Background Intelligent Transfer Service (BITS) from http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwxp/html/WinXP_BITS.asp, the tecnology behind Automatic Windows Updates and modified the given sample winforms app slightly to allow adding new items to download.

It is very simple, but to work , you need to download and install the .MSI from http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwxp/html/WinXP_BITS.asp and add Microsoft.Msdn.Samples.BITS.dll as refrence in VS.

You can find the code at http://www.xpertdotnet.com/code/Form1.cs.txt

some links with BITS (Background Intelligent Transfer Service)

http://www.kbcafe.com/iBLOGthere4iM/?guid=20040904223316
http://weblogs.asp.net/sbchatterjee/archive/2003/06/12/8585.aspx
http://dotnetjunkies.com/WebLog/demiliani/archive/2004/08/26/23309.aspx
http://weblogs.asp.net/mikehall/archive/2004/07/11/180144.aspx
http://pensieve.thinkingms.com/CommentView,guid,3cbfe252-63b5-47da-a27a-74de0d79a6a7.aspx

export datagrid to excel

this code can be run outside of page, or can be converted to run in and extended datagrid control

imports System.Drawing
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace demetz


    Public Class DemExportGridExcel

        Sub RenderGridToExcelFormat(ByVal grid As DataGrid, ByVal saveAsFile As String)
            ' check Excel rows limit
            If grid.Items.Count.ToString + 1 < 65536 Then
                HttpContext.Current.Response.Clear()
                HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"
                HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" & saveAsFile & ".xls")
                ' Remove the charset from the Content-Type header.
                HttpContext.Current.Response.Charset = ""
                'HttpContext.Current.Response.WriteFile("style.txt")
                ' Turn off the view state.
                grid.EnableViewState = False
                Dim tw As New System.IO.StringWriter()
                Dim hw As New System.Web.UI.HtmlTextWriter(tw)
                ' Get the HTML for the control.
                grid.HeaderStyle.ForeColor = Color.Black
                grid.HeaderStyle.BackColor = Color.Red
                grid.ItemStyle.ForeColor = Color.Black
                grid.BorderColor = Color.White
                ClearControls(grid)
                grid.RenderControl(hw)
                ' Write the HTML back to the browser.
                HttpContext.Current.Response.Write(tw.ToString())
                ' End the response.
                HttpContext.Current.Response.End()
            Else
               
                HttpContext.Current.Response.Write("Too many rows - Export to Excel not possible")
            End If
        End Sub

        Sub ClearControls(ByVal control As Control)
            Dim i As Integer
            For i = control.Controls.Count - 1 To 0 Step -1
                ClearControls(control.Controls(i))
            Next i

            If TypeOf control Is System.Web.UI.WebControls.Image Then
                control.Parent.Controls.Remove(control)
            End If

            If (Not TypeOf control Is TableCell) Then
                If Not (control.GetType().GetProperty("SelectedItem") Is Nothing) Then
                    Dim literal As New LiteralControl()
                    control.Parent.Controls.Add(literal)
                    Try
                        literal.Text = CStr(control.GetType().GetProperty("SelectedItem").GetValue(control, Nothing))
                    Catch
                    End Try
                    control.Parent.Controls.Remove(control)
                Else
                    If Not (control.GetType().GetProperty("Text") Is Nothing) Then
                        Dim literal As New LiteralControl()
                        control.Parent.Controls.Add(literal)
                        literal.Text = CStr(control.GetType().GetProperty("Text").GetValue(control, Nothing))
                        control.Parent.Controls.Remove(control)
                    End If
                End If
            End If
            Return
        End Sub 'ClearControls

    End Class

End Namespace

Website trackback HttpModule

I' ve written a HttpHandler to create aggregated trackbacks for websites all in one page.
See the code at
http://www.xpertdotnet.com/code/dmtzHandler.cs.txt
http://www.xpertdotnet.com/code/dmtzLogger.cs.txt