.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Code contracts replacing try/catch throw pattern?

Posted By:      Posted Date: April 14, 2011    Points: 0   Category :.NET Framework
I have started reading about the code contracts (design by contract) that's new in the .NET framework 4 which you can implement using System.Diagnostics.Contracts assembly.  I'm still trying to wrap my head around this concept.  Does this in some way replace the try/catch pattern?   I'm not seeing the value in this yet, although I'm sure there is one.

View Complete Post

More Related Resource Links

CLR Inside Out: Code Contracts


This article will share some of the best practices that the Base Class Libraries (BCL) team devised as they added the code contract libraries and started to take advantage of them in their own code.

Melitta Andersen

MSDN Magazine August 2009

The Weekly Source Code 56 - Visual Studio 2010 and .NET Framework 4 Training Kit - Code Contracts, P

Do you like a big pile of source code? Well, there is an imperial buttload of source in the Visual Studio 2010 and .NET Framework 4 Training Kit . It's actually a 178 meg download, which is insane. Perhaps start your download now and get it in the morning when you get up. It's extremely well put together and I say Kudos to the folks that did it. They are better people than I. I like to explore it while watching TV myself and found myself looking through tonight. I checked my blog and while I thought I'd shared this with you before, Dear Reader, I hadn't. My bad, because it's pure gold . With C# and VB, natch. Here's an outline of what's inside. I've heard of folks setting up lunch-time study groups and going through...(read more)

Using the BDC API how to catch the returned code when executing a stored procedure?

using BDC API, how can i catch the return code a stored procedure returns?

I tried Entity.Execute to execute a generic method. However, it returns an empty Entity table.

I know you can do it by setting up an output variable. I prefer returning an integer for tracking errors.

Any ideas?



How to catch Save Ribbon event from code-behind for the custom webpart

Hi there, How can a custom web part(sitting on top of PDP) catch the Save Ribbon event from code? I’ve been looking around for solution, but not yet find one…. Thank you, —JoeJoe

Catch Save Ribbon Event from code-behind for webpart control

Hi there, How can a custom web part catch the Save Ribbon event from code-behind? Thank you, —JoeJoe

Catch Save Ribbon Event from code-behind for webpart control

Hi there, How can a custom web part catch the Save Ribbon event from code-behind? Thank you, —JoeJoe

No return from unmanaged code, methods to catch exception?


I am developing a c# application that needs to calls functions from a c header file. I use

[System.Runtime.InteropServices.DllImport("xxx.dll", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true,SetLastError=true)]
        public static extern Byte xxxx(IntPtr hResourceContext, ushort xxxx, ref structa aaa,ref Result_struct Result, Byte b);

to import the dll method. When I call this method in C#, the application simply exits without throwing an exception or error. I've used similar approach for other methods from the same dll which involves passing nested structs as parameter and they work fine. I would like to know how I can debug the code/ catch errors/exceptions from the unmanaged code.

I tried to include the SetLastError=true during DllImport but it does not help.


WPF XBAP Release Doesn't Throw/Catch Errors


On my development box, launching the XBAP in either Debug or Release with Visual Studio 2010... it throws and catches errors just fine.  So I published the app to be hosted on our Win2008 R2 server...  Now here is what is wierd.  On my machine, hitting the published app still throws/catches errors just fine.  But on other users workstations, it just locks up IE for them, no error, no eventvwr message, nothing.

Any ideas?

Thanks a lot!!!



In App.xaml.cs I am catching all unhandled exceptions this way:

  public partial class App : Application
    protected override void OnStartup(StartupEventArgs e)
      Application.Current.DispatcherUnhandledException += AppDispatcherUnhandledException;
    protected void AppDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
      e.Handled = true;
    protected static void HandleException(Exception ex)
      ErrorWindow errorWindow = new ErrorWindow("Error connecting to servers, please try reloading the page!", ex, false);

Negative Lookbehind assertion to find all the strings from source code of particular pattern

I wrote a regular expression to match all the double quoted strings in the source code but excluding those which are initializers to const declaration.

My expression was 

The first group is const declaration of string type. Second is used to match double quoted strings capable of handling escape sequences.


If i simple run a match using second group, all the strings are matched. But if i use the entire expression, it provides me all the characters between the end double quote of last string and first double quote of next string.

Please help.


throw and catch exception handling



iam inserting record in three tables in a database, iam using begin trans , commit transaction and rollback,

i want to use throw method, if  any value insert wrong, pls correct me, i want to throw the error and

its go to rollback

running = false;
                        int updaterec = DBmgr.ExecuteNonQuery(CommandType.Text, "update ASArrivedcontainer set billgen ='Y' where billgen ='y' and acontinerid in ("+SelValues+")");
                        if (updaterec > 0)
                            running = true;
                            DataSet BillContainerDetails = BillingDetails();

fill grridview columns throw code


hi for all i have storedprocedure to retrive some data from databse

like this


and in design page i have gridview to display the values from database

  SqlCommand cmd = new SqlCommand("searchfile", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@name", Session["value"].ToString());
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())

i want inside the while loop i add the values from database to gridview colummns

like this

gridview column1=dr["column1"]


and so on

i stop at this point any one can help me in this problem


Cutting Edge: Code Contracts Settings in Visual Studio 2010


Code contracts in the Microsoft .NET Framework 4 rely on a few features in Visual Studio 2010. We'll review the project options for code contracts, how they affect the rewriter tool and some best practices for argument validation.

Dino Esposito

MSDN Magazine May 2011

Dialogs and ViewModel - Using Tasks as a Pattern

The ViewModel/MVVM pattern continues to gain popularity, with a blog post showing up every so often, and with tweets and retweets popping up even more often :-). At the same time, there are some interesting topics beyond the core pattern that continue to fuel experimentation. A big one amongst those is how should applications use dialogs when using the view model pattern.

The crux of the problem is the desire to keep the view model independent of UI concerns, and ensure it can be tested in a standalone manner, but that often comes to odds when you want the view model to launch a dialog, and/or do some work after the dialog is closed.

Prototype Design Pattern in C#. Vb.NET

Specify the kind of objects to create using a prototypical instance, and create new objects by copying this prototype

Adapter Design Pattern in C#, VB.NET

Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

Singleton Design Pattern in C#, Vb.NET

Ensure a class has only one instance and provide a global point of access to it.

Code Optimized Web Development Profile (VS 2010 and .NET 4.0 Series)

When you first run VS 2010 it prompts you to select an IDE profile to use. The profile you select will configure how tool windows are displayed/docked in the IDE by default and set the default keyboard shortcuts. You can then customize any of these settings by using the Tools->Options menu within the IDE and then override/change them. You can also later reset your profile and pick a different one by choosing the Tools->Import and Export Settings menu command.
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend