Archive for the 'C#' Category

Visual Studio 2008 keyboard shortcuts

Sometime back Bryan posted a blog in which he detailed his favorite shortcuts. While browsing through the net I found this good poster which details all the shortcuts.

Keyboard Shortcuts in Visual Studio (C#)

Hope this helps.

Happy Programming!!!



NetworkCredential in SMTP mail (Forms Authentication)

In the project I am currently working on we have to send email confirmation to the registered user giving them their User ID and Password (yup… saw that right password….not secured but that is the requirement :-(). we created a no-reply account and to our amazement all emails sent inside our domain were reaching fine but external domains were not. My hardware tech lead (Mihai) helped me out in checking exchange server(since I didnt have access to it). We saw that no mails to external domain was even queued in exchange. Then Mihai came up to me and asked me to check the credentials which was used for sending email and that was the missing piece :-). Prior to this application all our apps were windows authenticated so there was no need to specify the Network Credentials but in this app we have to use forms authentication since it is an internet site not an intranet site. So added the following line and it worked like charm 🙂

System.Net.NetworkCredential objAuthInfo = new NetworkCredential("<username>", "<password>");

objSMTP.Credentials = objAuthInfo;

Hope this helps.

Happy Programming!!!



Easy Dirty Checking with LINQ to SQL


What is dirty checking?

Dirty checking is simply a way to check an object to see if it contains changes. This can be used to prevent unnecessary database updates. Properly implemented it should also account for the object changing back to its original state.

For example:

We change the spelling of a dog’s name “Spottie” to “Spotty". If we were to test the object it should return dirty.

Then if we decide the original spelling was correct and change it back to “Spottie” the object should no longer return dirty.

How we implemented it:

We wanted to extend our LINQtoSQL generated classes to implement dirty checking. All we had to do was extend the OnLoaded() method to call our copy constructor so that we had a private copy of the original object. Then we can simply check that private object against the current object state property by property to determine if the object was dirty.

partial class Dog
    private Dog _Original; 

    // Copy constructor.
    public Dog(Dog previous)
        Name = previous.Name;
        Breed = previous.Breed;
        Age = previous.Age;
        Sex = previous.Sex;
        Weight = previous.Weight;
        Color = previous.Color; 

    //Here we copy the original object inside of itself for dirty checking
    partial void OnLoaded()
        _Original = new Dog(this);

    //dirty checking using _Original from copy constructor
    public bool IsDirty
                this.Name == _Original.Name &&
                this.Breed == _Original.Breed &&
                this.Age == _Original.Age &&
                this.Sex == _Original.Sex &&
                this.Weight == _Original.Weight &&
                this.Color == _Original.Color