Archive for the ‘Microsoft .net’ Category

I had scenario where one dependencyproperty is binded to multipe UI elelments so I was not able to update source by simply using Mode=TwoWay property of Binding. I was using MVVM architecture. I need to validate field before updating to source and if field is empty then display message to user and dont set the value of that dependencyproperty.
Here is the solution to access UI textbox in ViewModel by using UpdateSourceTrigger and Binding.CommandParameter.

To get access of control in ViewModel for validation you need to use Binding.CommandParameter property of Button control. see code below:

<TextBox Text="{Binding Path=DisplayName,Mode=TwoWay,UpdateSourceTrigger=Explicit}" />   //DisplayName is also binded to Grid . have defined Explicit so update will not done automatically.
<Button Name="btnSave" Text="Save" Command="{Binding Path=SaveCommand}">
   <Binding ElementName="txtName" /> // this will pass TextBox object to SaveCommand.

So I am done with UI changes now I need to get this TextBox in ViewModel. See ViewModel code below:

{ // constructor of ViewModel
// Command registration process - passing UI object to method
this.SaveCommand = new RelayCommand(param => SaveToDatabase(param), CanSave);

private bool CanSave(object param)
    return true;

public void SaveToDatabase(object param)
if(param != null)
   // cast param to TextBox so you will be able to get Textbox from UI 
  var txt = param as TextBox;
     MessageBox.Show("Blank not allowed");  // this will not update the binding
     txt.GetBindingExpression(TextBox.TextProperty).UpdateSource(); // this will update the source and you will see other   UI element also get updated value
// Now access the property and update database.

I hope this helps to understand UpdateSourceTrigger.Explicit & Button.CommandParameter usage in WPF MVVM architecture.
Comments/Suggestions are always welcome.

Read Full Post »

Before jumping into the Munq, Let’s understand IOC first:
Inversion of Control (IOC) is a pattern which decouples the use of an interface from the concrete implementation of that interface. By eliminating this coupling:
* The code can be tested by using special test implementations of the interface.
* Fewer DLLs need to be deployed on each tier of a multi-tiered deployment.
* Project compilation may be sped up as there are fewer project dependencies.
* Alternate implementations of the interface can be configured for use without requiring changes to the main application code. This could mean changing from MSSQL to Oracle with a simple configuration file change.

An IOC Container is a pattern which allows the application to create an instance of the interface without knowing the specific concrete class that will be used.

We use UnityContainer for Enterprise application development e.g. PRISM uses Unity. This article will give you an idea about Munq tool (replacement of Unity for Asp.net).

Munq is a very small, fast dependency injection container designed for ASP.NET applications, both Webforms and MVC. It has, by design, limited features. The main goal has been to minimize the CPU required for Resolving types from the container.

Munq is an open source project and available at codeplex: Munq@Codeplex

Some good Articles by Author Matthew Dennis:

Read Full Post »

Asp.net provides feature to display browser specific property for any control. It works for almost all the properties i.e. OnClientClick
<asp:Button ie:Text="IE Click" runat="server" mozilla:Text="Firefox Click" />

Figure shows “IE Click” text of button on IE and “Firefox Click” text of button on Firefox.

Read Full Post »

Daily tech links for .net and related technologies

Web Development

Web Design

Silverlight, WPF & RIA

Database & Related Technologies

Web Service & Related Technologies


Architecture, Methodology & Project Management


Reference: http://weblogs.asp.net/sanjeevagarwal/archive/2010/01/11/daily-tech-links-for-net-and-related-technologies-jan-10-12-2010.aspx

Read Full Post »

C# 3.0 Features

1. Implicitly Typed Variable (var)

Implicitly typed local variable is a variable that can be declared without specifying the .NET type explicitly. The type of that variable will be decided by the complier from the expression on the right side. Here’s an example of implicitly typed variable (var)

    var i = 5;
    var y = "dotnet";
    var it = new int[] { 1, 2, 3 };

The above mentioned C# 3.0 syntax will be equivalent to the below mentioned C# 2.0 syntax.

    int i = 5;
    string y = “dotnet”;


    • variable must be initialized to some expression that can not be null

      var x;

    • variable can not be assigned to null

      var x = null;

    • The initializer must be an expression. You can’t initialize it with an array.

      var r = { 1, 2, 3 };

    • The implicitly-type local variable can’t be initialized with different types more than one time.

      var t = 1;
      t = "dotnet"; // ERROR

2. Object Initializers

To get started, let’s look at the standard way of initializing an object with data in C# 2.0 using constructors.

    Employee e = new Employee("Harry", "Patel", "586-879-2563");

In C# 3.0, you can easily assign data to specific properties in a type with having to create an explicit constructor. Here’s an example of using an object initializer to assign property data to an employee type.

    Employee e = new Employee() {FirstName="Harry", LastName="Patel", Phone="586-879-2563", City="NewYork"};

You can even use object initializers to create the sub object.

    Employee e = new Employee()
    FirstName = "Harry",
    LastName = "Patel",
    Address = new Address()
    Street = "256, xyz",
    City = "NewYork"

You can create collection initializer also. Here’s an example of collection initializer.

    List square = new List()
    new Point(){x=10},
    new Point(){X=10}

3. Anonymous Types

Types those are not having any names are Anonymous types. Here’s an example of Anonymous types.

    var cust = new { custID = 10, custName = "Harry" };
    var cust1 = new { custID1 = "Harry", custName1 = 20 };

In above example, custID and custName declared with no specific data types.

4. Extension Methods

If you want to have your own custom method in string class then you can do that using extension methods.Here’s an example of extension methods.

    double d = 12.25263;
    // Calling the Extension method here.
    double newD = d.RoundMe();

Static Class implemented Extension Methods
Extension method must have static method and class having it must be a static class.

    public static class stringExtensionClass
    // Add "this" keyword to make it extension method enabled.
    public static double RoundMe(this double i)
    return Math.Round(i, 0);


    • Extension methods cannot be used to override existing methods
    • An extension method with the same name and signature as an instance method will not be called
    • The concept of extension methods cannot be applied to fields, properties or events

5. Lambda Expressions

Lambda Expression is another way to writing an Anonymous method(C# 2.0). Consider the following C# example:

    delegate bool Delegate1(int i);
    public void Method1()
    Delegate1 d = new Delegate1(Method2);
    // other code here
    private bool Method2(int i)
    return i > 2;

Simplified above code using C# 2.0 Anonymous methods.

    delegate bool Delegate1(int i);
    private void Method1()
    Delegate1 d = delegate(int i){return i > 2;};
    // other code here

Lambda expressions take this to the next level of conciseness. Here’s the above code re-written using lambda expressions.

    private void Method1()
    // line using C# 2.0 Anonymous types.
    Delegate1 d = delegate(int i){return i > 2;};
    // line using C# 3.0 Lambda Expressions.
    Delegate1 d = i=> i > 2;

6. Auto Implemented Property

We are used to creating property field using lots of statements. Auto implemented property is a solution:

Property declaration before .net fx 3.0

    private int x1;
    public int X
    get { return x1; }
    set { x1 = value; }

C# 3.0 auto implemented property replaces above code with following line.

    public int x { get; set; }

For creating read-only property just prefix set tag with private access modifier.

    public int ReadOnlyProp { get; private set; }


    • Both Get and Set properties are required to create property.

      public int ReadOnlyProp { get; set; } // ERROR

    • Can not assign private to both Get and Set property.

      public int ReadOnlyProp { private get; private set; } // ERROR

    • Get and Set method’s access modifiers must be more restrictive than Property.

      private int ReadOnlyProp {public get; set; } // ERROR

That’s All for C# 3.0…

Happy dotnetting… 🙂

Read Full Post »

AJAX Control Postback Error:

Sys.ArgumentTypeException: Object of type ‘Sys.UI._UpdateProgress’ cannot be converted to type ‘Sys.UI._UpdateProgress’.

Remove SmartNavigation = true from web.config file or
set ScriptMode=”Release” in ScriptManager.

SmartNavigation property and AJAX Controls are not compatible with each other so use “maintainScrollPositionOnPostBack=”true”” instead of “SmartNavigation=”true””.

Read Full Post »

Beginner’s Guide to jQuery with Asp.net.


Read Full Post »

Older Posts »