Wriju's BLOG

.NET, Cloud and everything

Closing a Windows Form by pressing ESC key

While I was developing one Windows Forms application, it was required for me to close the popup forms when user will press ESC key. I tried adding the Form’s KeyPress event. It was not working as expected. Then I thought I should write a code to find all the controls to my form and add… Read more

LINQ to SQL : Windows Forms Dynamic Where Clause

While I was reading the book LINQ in Action, I found this chapter. Below is how we can do in LINQ to SQL. The scenario is if you want to create a LINQ and pass the where clause based on user input from UI, you need to write different queries. Instead, how you can do… Read more

C# 4.0 and Parallel Programming Resources

C# 4.0   The Future of C# by Anders Hejlsberg at PDC 2008 https://channel9.msdn.com/pdc2008/TL16/   Slide Deck http://mschnlnine.vo.llnwd.net/d1/pdc08/PPTX/TL16.pptx   Sample Code and Whitepapers http://code.msdn.microsoft.com/csharpfuture/   C# 4.0, Dynamic Programming and JSON http://www.nikhilk.net/CSharp-Dynamic-Programming-JSON.aspx   Easy to understand Co- and Contra Varinace http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx   Parallel Programming   PDC 2008 Session by Daniel Moth   Parallel Programming for… Read more

LINQ to SQL : Returning Scalar Value from Stored Procedure

In LINQ to SQL it is not that easy thing to achieve as compared to other features. Let us assume you have a Stored Procedure like, Case 1: With Output Parameter CREATE PROCEDURE [dbo].[GetEmployeeCount]       @OutVal DateTime OUTPUT AS BEGIN       SELECT @OutVal = GetDate() END You need to write code which will look like,… Read more

C# 4.0 : New Extension Method “Zip”

While browsing the features I found an interesting extension method called “Zip”. How it works, let’s check it. List<string> names = new List<string> {“Debajyoti”, “Sumitra”, “Wriju” , “Writam”, “Tupur” };   List<int> ages = new List<int>{ 67, 58, 30, 24, 26 };   var zipped = names.Zip(ages, (name, age) => name + ” is “… Read more

C# 4.0 : Named Arguments

For methods having numerous arguments we tend to get confused. And this is also pain for others while reviewing the code. C# 4.0 gives us an opportunity to pass parameters with name. How it works, let’s check it. Suppose you have a method like below, public static class TestClass {    public static void ParamMethod(string… Read more

C# 4.0 : Optional Parameters

Days of creating multiple overloads to avoid parameters are gone. You no more have to create many methods and pass the default one there. C# 4.0 brings to us the concept of “optional” parameters. How it works, let’s check it. Suppose you have a method like below, public static class TestClass {    public static… Read more

ADO.NET Entity: LINQ to Entity with Relationship

Relationship and getting data from Entity Framework using Northwind database Simple Query   using (NorthwindEntities ctx = new NorthwindEntities()) {     var query = from o in ctx.Orders                 select o.OrderID;       foreach (var k in query)     {         Console.WriteLine(k);     } }   Now if you want to filter this with the… Read more

ADO.NET Entity: Insert Update and Delete with Relationship

Few days back I had written an article on Insert/Update/Delete for simple standalone tables at ADO.NET Entity: Insert Update and Delete. Now after that many of you had requested me to put article on how it works with relationship.   Here I will use a database created by me. There will be two tables connected… Read more

ADO.NET Entity: Insert Update and Delete

For small blogs, it requires more references and explanation, which sometimes are redundant. So I thought of writing single blog which is combination of topics more or less related to one thing.   I am exploring ADO.NET Entity Framework and have been trying things out there. Here I am planning to demonstrate how to do… Read more

Windows Forms – Enable Shortcut Key all over the Form

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… Read more

Programmatically invoking the Live.com search

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… Read more

LINQ to SQL vs. ADO.NET – A Comparison

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… Read more

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

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… Read more

LINQ to SQL: Making it N-Tier

You can create your application use LINQ to SQL and make it N-Tier. Go ahead and watch out the MSDN Documents N-Tier and Remote Applications with LINQ to SQL LINQ to SQL N-Tier with ASP.NET LINQ to SQL N-Tier with Web Services LINQ to SQL with Tightly-Coupled Client-Server Applications Implementing Business Logic (LINQ to SQL)… Read more

LINQ to XML : Querying XML with Namespaces

This question arises in one of discussions. If you have a XML document with “xmlns” then LINQ to XML does not return any IEnumerable<T> J. This is strange but true for XPath and XQuery. There is a trick to get it done, Let us assume that you have a XML as follows, <?xml version=“1.0“ encoding=“utf-8“?>… Read more

LINQ : IEnumerable<T> and IQueryable<T>

IEnumerable<T> and IQueryable<T> are the two most used terms of any LINQ discussion. What I am trying to here is that I am trying to simplify the two interfaces depending on their behavior. In LINQ world we generally have few providers available within .NET Framework, like LINQ to Object, LINQ to SQL, LINQ to XML…. Read more

LINQ to XML : Adding Namespace quickly

I have written a post earlier on how to attach Namespaces. After reading my article someone complained about the redundancy of the code. I also realized the pain. I was reading the book C# 3.0 In a Nutshell. There I got a very elegant solution. Let me explain the scenario,   If you create a… Read more

LINQ to XML : Join Xml Data

Let’s say I have created two Xml files using LINQ to XML from Northwind database. I have taken two tables Category and Products and tried to join between two different files.   Category XML   <?xml version=“1.0“ encoding=“utf-8“?> <categories>   <category id=“1“>     <CategoryName>Beverages</CategoryName>   </category>   <category id=“2“>     <CategoryName>Condiments</CategoryName>   </category>   <category… Read more

LINQ to XML : Working with Prefixes

Based on my previous post on LINQ to XML : Working with Namespaces, if you want to add prefixes to your Xml things becomes little crazy. The trick is that you have to use XAttribute() to attach prefixes. So the Xml as below need the following code,     <pfx:root xmlns:pfx=“urn:mynamespace-com“>   <pfx:child /> </pfx:root>… Read more

LINQ to XML : Working with Namespaces

If you are working with complex real world XML then you have to deal with namespaces to disambiguate. There are several ways to implement namespaces in LINQ to XML. One is you can use pure string to add your namespace with each element declaration, like   <root>   <child /> </root>   To create the… Read more

C# 3.0 Query Expressions by Bruce Eckel and Jamie King

Bruce, a renowned author of the famous book “Thinking in Java” is now exploring C# 3.0. He wrote many other books on Ø       The Hands-On Java Seminar CD ROM (available on the Web site) Ø       Thinking in C++ (PH 1995; 2nd edition 2000, Volume 2 with Chuck Allison, 2003) Ø       C++ Inside & Out (Osborne/McGraw-Hill… Read more

Sharing “var” across the method

One of the biggest limitation in “var” is that it cannot be shared across the methods. So if you are doing something using that keyword then you have to stick to the local. I was reading Alex’s Blog where he explained it very nicely,   Here is my implementation,   Create an Extension method  … Read more

LINQ to SQL : What is DataContext.Translate Method

DataContext’s Translate method converts existing IDataReader to an IEnumerable<T>. This is very useful when you may need to run raw SQL Query though System.Data.Linq. I am getting few similarities with DataContext’s ExecuteQuery.   How Translate works +++++++++++++ Assuming I am using Northwind database.     using (NWDataContext db = new NWDataContext()) {     DbConnection conn… Read more

C# 3.0 : Joining two in-memory collection through LINQ

LINQ allows us to join different collection source provided we are mapping with two properties with the same type. It is easy, it is quick and it is concise.     //Create Employee Class class Employee {     public int ID { get; set; }     public string Name { get; set; }     public… Read more

C# 3.0 : Exploring Lambda Expression

I started playing with Lambda expression after the TechEd demo by Anders Hejlsberg. Couple of nice things I would like to share with you.   As there might be many definitions for Lambda expression but to me Lambda Expression is the concise way to write functional implementation for Anonymous Method. This is been used by… Read more

C# 3.0 : Evaluation of Lambda Expression to Language INtegrated Query (LINQ)

From code name “cool” to C# 3.0, it’s been a long journey for this amazing language with .NET Runtime. Here I am going to show you the evaluation step by step.   Let us assume we have generic list of integers and I need to find the even numbers from that list,   List<int> arrInt… Read more

LINQ to SQL: Working with hierarchical data

LINQ to SQL supports hierarchical data and you can easily create a query and get output from there. Let us take an example of Northwind database. Northwind has Category -> Products -> Order_Details tables. -> indicates one to many relationships here. So ideally for a Category can have multiple products and a product can have… Read more

ADO.NET 2.0 : Fine tune your DataSet Performance

ADO.NET 2.0 offers us a unique feature through which we can make a real lightweight DataSet. This is very important and much neglected development practice to overlook the performance part. So when you have huge data and you are getting multiple hit this small tune may help you a lot.   Assume I am working… Read more

C# 3.0: Vote for the keyword “var”

You all probably know what is “var”. You might have a question why this new keyword is required in programming language like C#. One of the few strong reasons is to have an Anonymous Type stored in a variable. Whenever you have to initialize a type you have to mention the type before you give… Read more

LINQ to SQL Windows Form Binding Data Step by Step

LINQ to SQL designer in Visual Studio allows us to create mapped class using the drag and drop feature from Server Explorer. Now that is business object which ideally you could use as your datasource and from Visual Studio 2008. So let us have this step by step, Open your Visual Studio 2008 and create… Read more

C# 3.0 : Anonymous Type and .NET Reflection Hand-in-Hand

Anonymous Type is nothing but the type which gets defined by CLR (not you). So that type is as rich as any other normal type. Probably the conventional way to find the components of an Anonymous Type through reflection would be clearer to us,   static void Main() {     //Anonymous Type     var anyType… Read more

LINQ to SQL : Change in DataContext from Beta 1 to Beta 2

During the demonstration I have noticed that two major methods of DataContext has changed from Beta 1 to Beta 2.   First one, is the new name for the class DataShape(). Now there is no class called DataShape() but we have DataLoadOptions().     var loadOption = new DataLoadOptions();            loadOption.LoadWith<Customer>(c => c.Orders);   db.LoadOptions =… Read more

LINQ to SQL : Paging Data

When we work with relational database and especially when with larger data we generally go for custom paging. This allows us to create superfast application.   The simple logic for paged query is like,   Let’s suppose you have 100 rows. Now you want to get data of page size 10 for page 2.  … Read more

LINQ to SQL : Using Transaction

LINQ to SQL uses optimistic concurrency be default. LINQ to SQL is pure in memory operation, because you normally get the data from database and store them in .NET memory and perform some pure in memory update to those objects and finally when you are done with your modification you give those data back to… Read more

LINQ and C# 3.0 Articles and Samples

LINQ: .NET Language Integrated Query By Don Box, Anders Hejlsberg LINQ to SQL: .NET Language-Integrated Query for Relational Data By Dinesh Kulkarni, Luca Bolognese, Matt Warren, Anders Hejlsberg, Kit George .NET Language-Integrated Query for XML Data By Michael Champion The .NET Standard Query Operators By Anders Hejlsberg, Mads Torgersen Overview of C# 3.0 By Anders… Read more

Performance in C#

When we consider managed code in our application we also have to think about the performance. .NET has its own way of fine tuning things but just by remembering couple of things we can prevent the unnecessary push backs from our end. One of the biggest concerns is that why Generics? It is strongly typed,… Read more

Design Patterns Resources

A nice site at http://www.dofactory.com/Patterns/Patterns.aspx     Creational Patterns   Abstract Factory   Creates an instance of several families of classes   Builder   Separates object construction from its representation   Factory Method   Creates an instance of several derived classes   Prototype   A fully initialized instance to be copied or cloned   Singleton   A class of which only a single instance can… Read more

C# 3.0 Enhancements: Expression Trees (recording)

I have discussed about the Expression Trees in my previous blog at C# 3.0 Enhancements: Expression Trees. I have done a small recording on that showing the demo there. I have uploaded that to MSN Soapbox and you can find that here.   Video: CSharp 3.0 Expression Trees    Namoskar!!!… Read more

C# 3.0 Enhancements: Expression Trees

Expression Tree is the feature which enables us to write function which can be complied on-demand basis. It stores the function in form of structured data rather than executable code. Since this is structured we can easily iterate through it anytime. This is easier than finding something in IL.   Let’s suppose if you have… Read more

C# 3.0 Enhancements: Lambda Expression (more on what)

Lambda Expression gives us the more concise way of Anonymous Method implementation through functional programming language. Let me share some interesting aspects of Lambda Expression while exploring the C# 3.0 Specification. There it is being clearly described what Lambda Expression is.   (param) => expr   Can be expressed as   param => expr  … Read more

C# 3.0 Enhancements: Collection Initializers

In C# 3.0 we can easily initialize collection. It is smarter and concise way of writing code.   There are couple of things we should consider while initializing the collection. Ø       The collection should implement ICollection<T> Ø       The collection should have a provision to invoke ICollection<T>.Add(T)     Here is couple of them. I am… Read more

C# 3.0 Enhancements: Extension Methods

Extension Methods are different way of implementing static method. In C# 3.0 it allows us to extend any type and add method for that type. While writing extension method you need to Ø       declare a public static method Ø       the first parameter of that method should be the type of the object Ø       “this” keyword… Read more

C# 3. Resources

I found a very nice link on C# 3.0 resources and sharing it for you.   Query Composition using Functional Programming Techniques in C# 3.0 by Eric White   This talks about various aspects of C# 3.0 with details.   Namoskar!!!  … Read more

C# 3.0 Enhancements: Object Initializers

C# 3.0 allows us to initialize object in far better way in one single line. Let’s talk with an example here. Back again to my favorite Customer object.   public class Customer {     public Customer() { }       private int _ID;     public int ID     {         get { return _ID; }… Read more

C# 3.0 Features: Automatic Property (Part 2)

Thank you everyone for showing interest on my blog entry C# 3.0 Features: Automatic Property. Questions like, why this approach? What if there is any condition in property? Let me tell you, we have implemented the higher level of abstraction in C# 3.0. If you are simply creating a property (we often do) then the… Read more

C#: Yesterday, Today, and Tomorrow: An Interview with Anders Hejlsberg, Part 2

  A very interesting interview I found on the net. The discussion on C# and LINQ is in the Page 4.   For full conversation please visit here.   Other interviews with Anders   Deep Inside C#: An Interview with Microsoft Chief Architect Anders Hejlsberg   C#: Yesterday, Today, and Tomorrow: An Interview with Anders… Read more

LINQ: Query vs Lambda Expression

  LINQ is not just for the developers who used to write queries for databases but also for the Functional Programmers. So do not worry if you are not very comfortable with SQL kind of queries, we have a very nice option called “Lambda Expression”. Here I am going to demonstrate the scenario for both… Read more