Month: September 2011

JqGrid with MVC 2.0 / MVC 3.0

From last couple of months I was planning to create working demo for JQgrid using MVC and finally succeeded .It turns out it is not that hard to do. You just have to be very careful with the java-script you put in the page, because if you don’t do that nothing will happen. Just needs following things ready JQuery Latest Version JQGrid Latest version JQuery UI Themes (Optional) Now Create a new ASP.NET MVC2/MVC3 application. Include the JS files in your master page. I only include the ones I need. Most important thing while working with JQgrid is Url...

Read More

Drop all objects of SQL Server Database

Recently on my shared hosting server I need to delete all the tables and starts from scratch , I got a ready-made script on Internet page. declare @n char(1) set @n = char(10) declare @stmt nvarchar(max) -- procedures select @stmt = isnull( @stmt + @n, '' ) + 'drop procedure [' + name + ']' from sys.procedures -- check constraints select @stmt = isnull( @stmt + @n, '' ) + 'alter table [' + object_name( parent_object_id ) + '] drop constraint [' + name + ']' from sys.check_constraints -- functions select @stmt = isnull( @stmt + @n, '' ) + 'drop function [' + name + ']' from sys.objects where type in ( 'FN', 'IF', 'TF' ) -- views select @stmt = isnull( @stmt + @n, '' ) + 'drop view [' + name + ']' from sys.views -- foreign keys select @stmt = isnull( @stmt + @n, '' ) + 'alter table [' + object_name( parent_object_id ) + '] drop constraint [' + name + ']' from sys.foreign_keys -- tables select @stmt = isnull( @stmt + @n, '' ) + 'drop table [' + name + ']' from sys.tables -- user defined types select @stmt = isnull( @stmt + @n, '' ) + 'drop type [' + name + ']' from sys.types where is_user_defined = 1 exec sp_executesql @stmt Above script will delete all SQL objects from databse....

Read More

Distinct with LINQ

Here is one more LINQ Problem. Let’s think of scenario having record as below ProductId:1 , ProductName=”Product 1″ , CategoryId=2; ProductId:2 , ProductName=”Product 2″ , CategoryId=3; ProductId:3 , ProductName=”Product 3″ , CategoryId=1; ProductId:4 , ProductName=”Product 4″ , CategoryId=1; ProductId:4 , ProductName=”Product 5″ , CategoryId=2;` And we want the records of Product 1,Product 2, Product 3 (which are actually distinct according to our requirement for CategoryId). Here if we use distinct in our select query then it will return all(above 5 records) the Product records. So, to achieve our requirement we need to use GroupBy like below: List<Product> DistinctProduct = AllProduct.GroupBy(product => product.CategoryId).Select(products => products.First()).ToList(); We can also define groups on multiple properties, like below: List<Product> DistinctProduct = AllProduct.GroupBy(product => product.CategoryId, product.Name).Select(products => products.First()).ToList(); There must be other simpler ways to achieve above. If you have any suggestions for improvement let me know in comments. Happy Coding...

Read More