Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Articles
Search: 

Bonnie Berent's Tips
Bonnie DeWitt, September 1, 2007
Great tips for .NET developers.
Summary
Great tips for .NET developers.
Description

Getting the EXE FileName
The Application.ExecutablePath provides you with the full path to your EXE. The static System.IO.Path provides you with lots of options for retrieving parts of that FileName:
System.IO.Path.GetDirectoryName(fileName)
System.IO.Path.GetExtension(fileName)
System.IO.Path.GetFileName(fileName)
System.IO.Path.GetFileNameWithoutExtension(fileName)
System.IO.Path.GetPathRoot(fileName)

from a solution provided by Çetin Basöz in Message #1244919

Debugging Web Pages
"I can't debug my web page. What am I doing wrong?".
Here's a checklist:
1) Open IIS, right-click on your virtual directory and open Properties. 
2) Make sure that application name is not empty.
3) Click on the Configuration button, go to the debugging tab.
4) Check the two checkboxes and the "Send detailed asp error messages to client".
5) Click OK.
6) Next click the Directory Security tab and click the authentication Edit button. 
7) Make sure the top check box (anonymous access) is checked AND 
   the bottom one (Integrated Windows authentication).
8) OK back through all the windows.
9) Make sure your web.config contains <compilation debug="true" />
The above is for XP, but Windows 2003 is very similar.

from a solution provided by Ric Parodi in Message #1086861

Test for the Existence of a SQL Table
Need a quick way to test for the existence of a table in SQL Server? Try this method:
protected bool TableExists(string tableName)
{
    string sql =  "SELECT table_name FROM INFORMATION_SCHEMA.tables WHERE table_name = '" 
       + tableName + "'";
    bool exists = false;
    using (SqlCeConnection cn = new SqlCeConnection(this.connectionString))
    {
        using (SqlCeCommand cm = new SqlCeCommand(sql, cn))
        {
            cn.Open();
            SqlCeResultSet r = cm.ExecuteResultSet(ResultSetOptions.Scrollable);
            exists = r.HasRows;
        }
    }
    return exists;
}

from a solution provided by Craig McGuff in Message #1227222

Printing the Active Window
Have you ever wanted to print your Windows Form? Here's a waaaay cool way to print the active Windows Form:
Friend Class PrintHelper

    Private m_PrintBitmap As Bitmap
    Private WithEvents m_PrintDocument As System.Drawing.Printing.PrintDocument
    Private m_PrintDialog As PrintDialog
    Private m_PrintDialogResult As DialogResult
    Private Declare Auto Function BitBlt Lib "gdi32.dll" 
        (ByVal hdcDest As IntPtr, ByVal nXDest As Integer, 
         ByVal nYDest As Integer, ByVal nWidth As Integer, 
         ByVal nHeight As Integer, ByVal hdcSrc As IntPtr, 
         ByVal nXSrc As Integer, ByVal nYSrc As Integer, 
         ByVal dwRop As System.Int32) As Boolean
    Private Const SRCCOPY As Integer = &HCC0020
    Private mFormToPrint As System.Windows.Forms.Form

    Friend Function Print(ByVal formToPrint As System.Windows.Forms.Form) As Boolean
        Try
            mFormToPrint = formToPrint

            ' Copy the form's image into a bitmap.
            m_PrintBitmap = GetFormImage()

            ' Make a PrintDocument, bring up PrintDialog, and print
            m_PrintDocument = New System.Drawing.Printing.PrintDocument
            m_PrintDialog = New PrintDialog
            m_PrintDialog.Document = m_PrintDocument
            m_PrintDialogResult = m_PrintDialog.ShowDialog
            If m_PrintDialogResult = DialogResult.OK Then
                m_PrintDocument.Print()
            End If
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    '**********************************************************************
    ' Returns the Form as a Bitmap
    '**********************************************************************
    Private Function GetFormImage() As Bitmap
        ' Get this form's Graphics object.
        Dim me_gr As Graphics = mFormToPrint.CreateGraphics

        ' Make a Bitmap to hold the image.
        Dim bm As New Bitmap(mFormToPrint.ClientSize.Width, mFormToPrint.ClientSize.Height, me_gr)
        Dim bm_gr As Graphics = me_gr.FromImage(bm)
        Dim bm_hdc As IntPtr = bm_gr.GetHdc

        ' Get the form's hDC. We must do this after 
        ' creating the new Bitmap, which uses me_gr.
        Dim me_hdc As IntPtr = me_gr.GetHdc

        ' BitBlt the form's image onto the Bitmap.
        BitBlt(bm_hdc, 0, 0, mFormToPrint.ClientSize.Width, _
            mFormToPrint.ClientSize.Height, me_hdc, 0, 0, SRCCOPY)
        me_gr.ReleaseHdc(me_hdc)
        bm_gr.ReleaseHdc(bm_hdc)

        ' Return the result.
        Return bm
    End Function

    '**********************************************************************
    ' Draws the form on the page.  Not currently used, but part of original code.
    '**********************************************************************
    Private Sub m_PrintDocument_PrintPage(ByVal sender As Object, 
        ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles m_PrintDocument.PrintPage

        ' Draw the image centered.
        Dim x As Integer = e.MarginBounds.X + _
            (e.MarginBounds.Width - m_PrintBitmap.Width) \ 2
        Dim y As Integer = e.MarginBounds.Y + _
            (e.MarginBounds.Height - m_PrintBitmap.Height) \ 2
        e.Graphics.DrawImage(m_PrintBitmap, x, y)

        ' There's only one page.
        e.HasMorePages = False
    End Sub

End Class

from a solution provided by Mike Cole in Message #1239236

Bonnie DeWitt, Geneva Systems Group
Bonnie is currently one of the principals of Geneva Systems Group. Call her the Senior Software Engineer, or even call her the VP of Engineering. She has no official title at the moment. Bonnie has been writing software in various languages for about 30 years. Bonnie's current focus on C# .NET applications began in early 2002. She has been a Microsoft C# MVP since Oct 2003 and an active member of the .NET online community.
More articles from this author
Bonnie DeWitt, September 1, 2005
Great tips for .NET developers
Bonnie DeWitt, October 1, 2005
Great tips for .NET developers
Bonnie DeWitt, November 1, 2005
Great tips for .NET developers
Bonnie DeWitt, December 1, 2005
Great tips for .NET developers
Bonnie DeWitt, April 1, 2009
Great tips from the .NET developer community compiled by Bonnie Berent.
Bonnie DeWitt, February 1, 2006
Great tips for .NET developers
Bonnie DeWitt, March 1, 2006
Great tips for .NET developers
Bonnie DeWitt, April 1, 2006
Great tips for .NET developers
Bonnie DeWitt, May 1, 2006
Great tips for .NET developers
Bonnie DeWitt, June 1, 2006
Great tips for .NET developers
Bonnie DeWitt, July 1, 2006
Great tips for .NET developers
Bonnie DeWitt, August 1, 2006
Great tips for .NET developers
Bonnie DeWitt, September 1, 2006
Great tips for .NET developers
Bonnie DeWitt, October 1, 2006
Great tips for .NET developers
Bonnie DeWitt, November 1, 2006
Great tips for .NET developers
Bonnie DeWitt, December 1, 2006
Great tips for .NET developers
Bonnie DeWitt, January 1, 2007
Great tips for .NET developers
Bonnie DeWitt, February 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, March 1, 2006
Great tips for .NET developers.
Bonnie DeWitt, April 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, March 1, 2007
Good tips for .NET developers.
Bonnie DeWitt, May 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, June 1, 2007
Great tips and tricks for .NET developers.
Bonnie DeWitt, July 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, August 1, 2007
Great tips for .NET developers
Bonnie DeWitt, February 1, 2008
Great tips from the Universal Thread .NET community.
Bonnie DeWitt, March 1, 2008
Great tips for .NET developers selected from the community by Bonnie Berent.
Bonnie DeWitt, April 1, 2008
Great tips from the .NET developer community compiled by Bonnie Berent.
Bonnie DeWitt, January 1, 2006
Great tips for .NET developers