Zeeshan Hirani
-
Using LinqDataSource for Inserts,Updates and Deletes
In this small walk through I will demonstrate how to use linq datasource to do insert,update,delete and select using form view,gridview and details view control. I will use Formview control to do inserts and use gridview to do updates and deletes. When you select a row in the grid, the detail record will be displayed in a details view control. The screen shot of the page looks like this.
-
My Linq Postings So Far
I have written some articles on linq, but the ones that i consider a good read are as follows.
-
ObjectTrackingEnabled Does not Work!
Today I was messing around with the datacontext that I created for linq to SQL NorthWind database. One of the ways to improve performance for the datacontext for web scenarios is turn off object tracking on the context. If you are using the context for read only purpose rather than insert update or delete, than you do not need to bear the heavy cost of object tracking service. But what I have found out recently is, if you turn off object tracking on your datacontext, you loose the ability to traverse relationship. For example, if you traversing one one to one relation like Customer.Address.City than you would get a null reference exception. If you navigate a many to many relationship like Customer.Orders.Count(), you will get 0 despite the fact that a particular customer would have orders.
-
Invoking Generic Methods Using Reflection
This is by no means any detailed post on how to use reflection. The other day I had the need to use reflection to call a generic method. So after playing around with reflection for few minutes I figured it out. Here is how the code looks like.
-
Static fields in Generics
If you have used static fields before you would be aware that no matter how many instances of a class you create, you will always end up sharing the static field among all instances and there will be only 1 value. Changing the static value would effect and reflect the new value for all instances of the class as shown below.
-
Using LinqDataSource With GridView Control
-
How To Hide IntelliSense
Another neat trick that I learned recently is using the Cntrl key. There are times when you are coding and need to see something that gets hidden behind the intellisense window. Instead of pressing Esc to get rid of the window, I find it easier to hold down the Cntrl key which fades the intellisense as long as you have the key pressed. Nice features that saves some time.
-
Comparison Using Generics
As I was playing with generics, I found something really interesting in terms of comparing two reference types using generic method. For example, when you compare two reference types using == or !=, you are basically comparing the references of those two objects. However when you compare two strings, you are actually comparing two values to see if they are equal instead of comparing references. This is because string class overloads those operators to define a different implementation of == and != operator. When you apply comparisons in generic methods, you would be using the default implementation of == and != that is defined by the object type. This is because the compiler resolves the method using unbound generic type and does not try to accommodate all the possible method calls based on different generic type which may have overloaded the == and != operator. Because of this limitation you may not get the correct comparison achieved in generic method as shown below.
-
Type Inference in Generics
If you are using generic method, you can call the method without explicitly specifying the type arguments. Their are certain cases where the compiler cannot infer the type based on the values passed in the parameters, in those cases you can explicitly specify the type argument. Depending on the scenario,it might be concise syntax but may not be readable to another developer. Here is an example of using generic method with and without explicitly specifying the type.
-
Linq to SQL Debug Visualizer
I have been learning Linq for sometime. However I always had some difficulty figuring out what SQL statement got generated for my Linq query. Although there are numerous ways to find the SQL statement that get generated but none of them were with out code or having to leave visual studio environment. Last week I discovered a really good tool called Linq to SQL Visualizer which allows me look at my Linq query in a Visualizer that exactly shows the SQL statement generated. It also allows you to execute your query from there as well.