July, 2008

  • Wriju's BLOG

    How to find public key token for a .NET DLL or assembly

    • 21 Comments

    Many times we need to get the Public key token for a strongly named assembly in .NET. FAQ on that “how to get the public key token?”. Answer is very simple use the .NET Framework tools sn.exe. So open the Visual Studio 2008 Command Prompt and then point to the dll’s folder you want to get the public key,

    Use the following command,

    sn –T myDLL.dll

    This will give you the public key token. Remember one thing this only works if the assembly has to be strongly signed.

    Example

    C:\WINNT\Microsoft.NET\Framework\v3.5>sn -T EdmGen.exe

    Microsoft (R) .NET Framework Strong Name Utility  Version 3.5.21022.8

    Copyright (c) Microsoft Corporation.  All rights reserved.

     

    Public key token is b77a5c561934e089

    Namoskar!!!

  • Wriju's BLOG

    LINQ to SQL vs. ADO.NET – A Comparison

    • 17 Comments

    ADO.NET is our contemporary data access component and now we have written many applications. Now there has been a lot of talk on LINQ to SQL. So we are little skeptical about this component and trying to find some relation between existing ADO.NET technology.

     

    Purpose of this post is to give some quick brief on some major differences between ADO.Net and LINQ to SQL. I have not used 100% code as this is just to give an idea. The database I have used here is Northwind (SQL Server 2000 Sample database).

     

    Some comparison,

     

    Scenario 1

    +++++++

    Establishing connection between database and application,

     

    ADO.NET

    using(SqlConnection conn = new SqlConnection("Connection String"))

    {

        conn.Open();

    }

     

    LINQ to SQL

    You create a .dbml file and give a name (assume “NW.dml”). Then there will be a DataContext class created. So you need to initialize the instance of an object.

     

    NWDataContext db = new NWDataContext("Connection String");

     

    You do not need to call any Open() method. Datacontext handles well the open and close method.

     

    Scenario 2

    +++++++

    Getting data from database,

     

    ADO.NET

    using(SqlConnection conn = new SqlConnection("Connection String"))

    {

        using (SqlCommand comm = new SqlCommand("Select * from Customers"))

        {

            conn.Open();

            SqlDataReader reader = comm.ExecuteReader();

            DataTable dt = new DataTable("New Table");

            dt.Load(reader);

        }

    }

     

    LINQ to SQL

    using (NorthwindDataContext db = new NorthwindDataContext())

    {

        //You can also use "var" at "IEnumerable<Customer>"

        IEnumerable<Customer> custs = from c in db.Customers

                                      select c;

     

        foreach (Customer c in custs)

        {

            Console.WriteLine(c.CompanyName);

        }

    }

     

    WOW!!! I have received comment from our Senior Program Manager Dinesh Kulkarni from LINQ to SQL team when I requested him to visit my blog. He mentioned,

    DataTable provides you a cache that can be re-enumerated without DB roundtrip while LINQ to SQL results need to be explicitly cached with something like a ToList()/ToArray(). Identity caching in DataContext aside, the L2S code is closer to enumerating a DataReader. Although DataReader does not allow you to re-enumerate and requires another ExecuteReader, the impact of reenumerating L2S query is the same – another roundtrip to DB.

    Scenario 3

    +++++++

    Inserting into the database,

     

    ADO.NET

     

    using(SqlConnection conn = new SqlConnection())

    {

        conn.Open();  

        SqlCommand comm = new SqlCommand("INSERT INTO...", conn);

     

        comm.ExecuteNonQuery();

    }

     

    LINQ to SQL

     

    using (NorthwindDataContext db = new NorthwindDataContext())

    {

        //Create a new object

        Customer c = new Customer();

        c.CustomerID = "ABCDE";

        //.... add all the properties you need to add while inserting

     

        //Add it to the collection

        db.Customers.InsertOnSubmit(c);

     

        //Save the changes to the database

        db.SubmitChanges();   

    }

     

    Scenario 4

    +++++++

    Updating database,

    ADO.NET

     

    Same as Scenario 3

     

    LINQ to SQL

    using (NorthwindDataContext db = new NorthwindDataContext())

    {

        //Get the object from database

        Customer cust = (from c in db.Customers where c.CustomerID == "ALFKI" select c).First();

     

        //Update the exsisting value

        cust.CompanyName = "I do not know?";   

       

           

        //Save the changes to the database

        db.SubmitChanges();   

    }

     

    Scenario 5

    +++++++

    Deleting records from the database,

    ADO.NET

     

    Same as Scenario 3

     

    LINQ to SQL

    using (NorthwindDataContext db = new NorthwindDataContext())

    {

        //Get the object from database

        Customer cust = (from c in db.Customers where c.CustomerID == "ALFKI" select c).First();

     

        //Remove it from collection

        db.Customers.DeleteOnSubmit(cust);

           

        //Save the changes to the database

        db.SubmitChanges();   

    }

     

    Scenario 5

    +++++++

    Executing stored proc which returns record(s),

     

    ADO.NET

    using(SqlConnection conn = new SqlConnection())

    {

        conn.Open();

        using (SqlCommand comm = new SqlCommand("SalesByCategory", conn))

        {

            comm.CommandType = CommandType.StoredProcedure;

            comm.Parameters.AddWithValue("@param1", "value1");

            comm.Parameters.AddWithValue("@param2", "value2");

     

            SqlDataReader reader = comm.ExecuteReader();       

        }

    }

     

    LINQ to SQL

    In LINQ to SQL it becomes metod as you drag and drop it to .dbml file,

     

    using (NorthwindDataContext db = new NorthwindDataContext())

    {

     

        var outPut = db.SalesByCategory("SeaFood", "1998");        

    }

     

    Performance of LINQ to SQL and ADO.NET

     

    LINQ to SQL Performance Part 1

    LINQ to SQL Performance Part 2

    LINQ to SQL Performance Part 3

    LINQ to SQL Performance Part 4

    LINQ to SQL Performance Part 5

     

    There are a many to discuss. Hope you have enjoyed it.

     

     

    Namoskar!!!

  • Wriju's BLOG

    Windows Forms – Enable Shortcut Key all over the Form

    • 10 Comments

    Windows Application and shortcut key are synonyms. People prefer to use shortcut keys for faster activity. Assume you are in a text box and you want to hit F5 to reload the form. You do not want to move your mouse cursor. So what you will be doing is that you will be adding “Key Down” event to the text box. But if you are in a different text box or in a button that will not work. So you will add “Key Down” event to your form.

     

    But you need to enable a property to work it properly and that’s the trick.

     

    private void Form1_KeyDown(object sender, KeyEventArgs e)

    {

        if (e.KeyCode == Keys.F5)

        {

            btnRefresh_Click(null, null);

        }

    }

     

    But golden trick is, in the property window of the Form make the following changes

     

    KeyPreview = True 

    Enjoy programming. 

    Namoskar!!!

  • Wriju's BLOG

    What do I have to do if I install IIS after Visual Studio

    • 8 Comments

    What happens if you install you Visual Studio then realize that you need IIS for you application to test and run. We often do this. I have seen people uninstalling Visual Studio and then installing it again after IIS to make sure ASP.NET works fine.

    Wow!! There is an easy way to do it,

    Open the command prompt for Visual Studio 2008. Run the following command

    aspnet_regiis –i

    -i  

    Install this version of ASP.NET and update scriptmaps at  the IIS metabase root and for all scriptmaps below the root. Existing scriptmaps of lower version are upgraded to

    this version. (this is from product documentation help text)

     

    You can also check if the specific ASP.Net version is already attached with IIS by using –lv.

     

    Namoskar!!!

     

  • Wriju's BLOG

    Visual Studio 2008 : How to zoom Class Diagram

    • 3 Comments

    During my demos I have noticed that Class Diagram in Visual Studio gives a very detailed view with lot of information. Now this is hard for people sitting at the back of the room to see what is there.

    It’s very easy, if you press “CTRL” then scroll the mouse, this will zoom in and out the class diagram.

    Namoskar!!!

  • Wriju's BLOG

    LINQ to Entity : Querying data using ADO.NET Entity Framework

    • 3 Comments

    ADO.NET Entity Framework have evolved since we have released it first time. Now with the Visual Studio 2008 if you install the .NET Framework 3.5 SP1 and Visual Studio 2008 SP1 you will get the fully functional version of ADO.NET Entity Framework. This is post Beta 3 release.

    To create a .edmx file you have template in Visual Studio 2008,

    Project > Add New Item

    image

     image

    image

    image

    image

    After creating the .edmx file, write the below query,

    using (ObjectContext objC = new ObjectContext("name=NorthwindEntities"))

    {

        ObjectQuery<Customers> custs =

            objC.CreateQuery<Customers>("NorthwindEntities.Customers");

           

        foreach (Customers cust in custs)

        {

            Console.WriteLine(cust.CompanyName);

        }

    }

    Note that this returns Object ObjectQuery<T> which implements IQueryable<T>, which means it will also allow you to write LINQ and this LINQ supports external source. Meaning it will translate the code to T-SQL (if you use MS SQL Server database).

    I feel more comfortable if I use LINQ,

    using (ObjectContext objC = new ObjectContext("name=NorthwindEntities"))

    {

        var custs = from c in

                        objC.CreateQuery<Customers>("NorthwindEntities.Customers")

                    where c.City == "London"

                    select c;

           

        foreach (Customers cust in custs)

        {

            Console.WriteLine(cust.CompanyName);

        }

    }

    Now this is already strongly typed and I do not have to use ObjectContext and ObjectQuery anymore.

    So my pure LINQ to Entity will look like,

    using (NorthwindEntities db = new NorthwindEntities())

    {

        var custs = from c in db.Customers                 

                    where c.City == "London"

                    select c;

           

        foreach (Customers cust in custs)

        {

            Console.WriteLine(cust.CompanyName);

        }

    }

    Namoskar!!!

     

  • Wriju's BLOG

    How to copy my snippet in Visual Studio 2008

    • 2 Comments

    I use Visual Studio 2008 code snippet a lot when I write code for my presentation. It saves a lot of time and it is very elegant. But I used to struggle a lot while changing my demo machine. Because I had to repeat the same process again and again from Tools > Snippet Manager, I find it very inconvenient if you have many snippets.

    There is an elegant way to do that, under

    C:\Documents and Settings\<<logged in user>>\My Documents\Visual Studio 2008\Code Snippets\Visual C#\My Code Snippets  

    Copy the files from your source machine and paste them to the destination machine’s same folder. You will get all the snippets. You do not have to use Snippet Manager anymore to upload snippet.

    Namoskar!!!

  • Wriju's BLOG

    ZoomIt : Fantastic Windows Zooming Application

    • 1 Comments

    When I show demo using my desktop I often come across to the scenarios where things are very small. I was using Windows Magnifier. I found this tool later and this is very helpful. This tool was created by Microsoft Technical Fellow, Mark Russinovich.

    Some facts,

    Ø  ZoomIt runs on System Tray

    Ø  Allows you to use configurable shortcut key

    This is very lightweight and simple tool. You will enjoy if you use them.

    http://technet.microsoft.com/en-us/sysinternals/bb897434.aspx

    Namoskar!!!

  • Wriju's BLOG

    LINQ to SQL FAQ

    • 1 Comments

    List of FAQs are at MSDN http://msdn.microsoft.com/en-us/library/bb386929.aspx

    Namoskar!!!

  • Wriju's BLOG

    LINQ to SQL : Troubleshooting

    • 1 Comments

    Problem with LINQ to SQL? Want to troubleshoot?

    http://msdn.microsoft.com/en-us/library/bb386996.aspx

    Namoskar!!!

  • Wriju's BLOG

    LINQ to SQL : Missing Manual are at MSDN

    • 1 Comments

    ADO.NET and LINQ to SQL

    Describes the relationship of ADO.NET and LINQ to SQL.

     

    Analyzing LINQ to SQL Source Code

    Describes how to analyze LINQ to SQL mapping by generating and viewing source code from the Northwind sample database.

     

    Customizing Insert, Update, and Delete Operations (LINQ to SQL)

    Describes how to add validation code and other customizations.

     

    Data Binding (LINQ to SQL)

    Describes how LINQ to SQL uses IListSource to support data binding.

     

    Inheritance Support (LINQ to SQL)

    Describes the role of inheritance in the LINQ to SQL object model, and how to use related operators in your queries.

     

    Local Method Calls (LINQ to SQL)

    Describes LINQ to SQL support for local method calls.

     

    Data Retrieval and CUD Operations in N-Tier Applications (LINQ to SQL)

    Provides detailed information for multi-tier applications that use LINQ to SQL.

     

    Object Identity (LINQ to SQL)

    Describes object identity in the LINQ to SQL object model, and explains how this feature differs from object identity in a database.

     

    The LINQ to SQL Object Model

    Describes the object model and its relationship to the relational data model.

     

    Object States and Change-Tracking (LINQ to SQL)

    Provides detailed information about how LINQ to SQL tracks changes.

     

    Optimistic Concurrency Overview (LINQ to SQL)

    Describes optimistic concurrency and defines terms.

     

    Query Concepts in LINQ to SQL

    Describes aspects of queries in LINQ to SQL that differ from queries in LINQ.

     

    Security in LINQ to SQL

    Describes the correct approach to security in database connections.

     

    Serialization (LINQ to SQL)

    Describes the serialization process in LINQ to SQL applications.

     

    Stored Procedures (LINQ to SQL)

    Describes how to map stored procedures at design time and how to call them from your application.

     

    Transactions (LINQ to SQL)

    Outlines the three models of transaction that LINQ to SQL supports.

     

    Type System Mismatches (LINQ to SQL)

    Describes the challenges of mingling different type systems.

     

    User-Defined Functions (LINQ to SQL)

    Describes how to map user-defined functions at design time and how to call them from your application.

     

    Namoskar!!!

     

  • Wriju's BLOG

    Microsoft Project Code Named “Velocity”

    • 1 Comments

    "Velocity" is a distributed in-memory application cache platform for developing scalable, high-performance applications. "Velocity" can be used to cache any CLR object and provides access through simple APIs. The primary goals for "Velocity" are performance, scalability and availability.

    http://msdn.microsoft.com/en-us/data/cc655792.aspx

    Technical Articles

    Blog

    Microsoft Project Code Named "Velocity" Team Blog

    Forum

    Microsoft Project Code Named “Velocity”

    Download

    Microsoft Project Code Named “Velocity”

    Samples

    Microsoft Project Code Named “Velocity” on Code Gallery

    Namoskar!!!

  • Wriju's BLOG

    Programmatically invoking the Live.com search

    • 0 Comments

    Live.com you need to automate that in your Windows application. You will use the WebBrowser control. This is a very powerful control and allows us to control things through DOM. Here is a small code which insert values to the live.com’s seach text box and clicks the “Go” button. I have created a windows forms application and added a WebBrowser control named “webBrowser1”.

     

    //Set the value to the Search text box

    webBrowser1.Document.GetElementById("sb_form_q").SetAttribute("value", "Wriju's BLOG");

     

    //Now click the button

    //this actually submits the one and only one form

    //So you need to do that

    webBrowser1.Document.Forms[0].InvokeMember("submit");

     

    Namoskar!!!

  • Wriju's BLOG

    Celebrating 200th MSDN Blog Post

    • 0 Comments

    Wow experience!!! 200+ posts on MSDN with millions of hits it’s a great pleasure being at Microsoft with additional takeaways. I never thought in my life that I would be blogging. I had a dream that one day I will become writer (was not sure on what though). Blogging is a great tool to give values back to community for people with less power (like me or introvert).

    My journey has been always a pleasure (be it blogging or 5 day long on-road cross country cargo truck travel). But my greatest takeaway was when my blog was in MSDN C# Home page. Charlie was kind enough to put my blog in the same list with Scott Guthrie (http://weblogs.asp.net/scottgu/). I never had a clue on how Scott manages to write blog so regularly.

    You may find some of my selected and relevant past posts are at Charlie’s blog at Community Convergence and my journey stared from Community Convergence XXXV. I have no words to say thanks to Charlie (our very own C# Community Program Manager).  Many people have encouraged me in many ways, Program managers, friends, Microsoft Partners (with whom I work professionally), my super technical Microsoft teammates, people from everywhere over the web. Some of them have added my blog to their favorite list (priceless to me always).

    There are many things I have planned to write. I am a lazy person, so I need to be focused and keep on contributing to my blog.

    But I have promises to keep,

    And miles to go before I sleep,

    And miles to go before I sleep.

     

    I am not a good writer, but I love many things at Microsoft.

     

    Namoskar!!!

Page 1 of 1 (14 items)