Yet another coding blog covering everything from C#, WPF, SQL, and Silverlight to any other random thought that passes my way…
RSS icon Home icon
  • Handling Secondary Thread Exceptions in WPF

    Posted on May 26th, 2009 biggert No comments

    As I’m sure you know, if you’ve played around in WPF enough, you’ve realized that the WPF way to catch unhandled exceptions is through the App.xaml with the DispatcherUnhandledException event. What you may not know is that this only catches unhandled exceptions in the WPF application itself. If you have any threads that throw exceptions or especially secondary threads (created by threads) that throw exceptions, you can kiss your pretty little “catch-all” event bye because your app with crash into the fiery pits of Windows system event log hell.

    Recently on a project I’ve been involved with, we’ve been seeing this issue pop up a few times and without any knowledge from the event log, we really had a tough time finding the source of the errors. Enter the throwback to .NET 2.0, CurrentDomain’s UnhandledException event. Our old little pal is back from the past to make an appearance in our pretty little WPF app and, lo and behold, we are able to catch those nasty unhandled exceptions in our secondary threads and perform a more graceful shut down. Very nice and very clean all with a simple code block:

    
    private void Application_Startup(object sender, StartupEventArgs e)
    {
    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
    }
    
    void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
    //Oh noes! An error occurred!
    }
    

    Now to actually fix the source of the errors!

    NOTE: For added fun, if you fancy the use of legacyUnhandledExceptionPolicy, this gets event better.

  • Update to previous mystery WPF issue

    Posted on May 6th, 2009 biggert No comments

    Just wanted to make it obvious that I’ve updated the previous post about the mystery WPF issue regarding a strange crash. I wouldn’t really call it a solution… more of a workaround for our systems. Let’s hope it holds up in the long run (or at least until this issue is addressed by Microsoft, which probably won’t happen anytime soon since they haven’t responded to any requests).

  • Copy and paste to Visual Studio ASP.NET page from SQL Management Studio

    Posted on May 1st, 2009 biggert No comments

    As I’m bringing over some older posts from my previous blog, I thought this one might be helpful to some (notice I was using VS2005 at the time but this also works for VS2008).

    So I was doing some grunt work of making a few textboxes which will be filled from a SQL table from a remote database. I decided to pop open SQL Management Studio in my second monitor and copy the field names straight from the table and paste them onto the ASP.Net page. I clicked on a field name and unknowingly left the whole row selected, hit CTRL-C, and then pasted right into the ASP.Net page. To my surprise, a Gridview showed up and its related datasource. Also, connection string was added to my Web.config. I was not aware of this functionality at all but I thought I’d post it as it could save some time for a few people.

    1. With Visual Studio 2005 .NET open to an ASPX page, open SQL Management Studio.
    2. Right click on the table you want to create a Gridview with and select ‘Design’.
    3. Highlight the field(s) you want to show in the Gridview (use shift-select for multiples).
    4. Press CTRL-C to copy to the clipboard.
    5. Place the cursor in the ASPX page where you want the Gridview to appear and press CTRL-V.
    6. That’s it! You will have a simple Gridview with the fields you selected, a SqlDataSource underneath it, and a connection string in your Web.Config.

    Here’s an example of the resulting ASPX output:

    <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource2"
    EmptyDataText="There are no data records to display." AutoGenerateColumns="False">
    <Columns>
    <asp:BoundField DataField="Processor" SortExpression="Processor" HeaderText="Processor"></asp:BoundField>
    <asp:BoundField DataField="MinAmount" SortExpression="MinAmount" HeaderText="MinAmount"></asp:BoundField>
    <asp:BoundField DataField="Requestor" SortExpression="Requestor" HeaderText="Requestor"></asp:BoundField>
    <asp:BoundField DataField="OutputFilesPath" SortExpression="OutputFilesPath"
    HeaderText="OutputFilesPath"></asp:BoundField>
    </Columns>
    </asp:GridView>
    
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" SelectCommand="SELECT [Processor],
     [MinAmount], [Requestor], [OutputFilesPath] FROM [tbl_Settings]”
    ConnectionString=”<%$ ConnectionStrings:MPConnectionString1 %>”
    ProviderName=”<%$ ConnectionStrings:MPConnectionString1.ProviderName %>”>
    </asp:SqlDataSource>