What's up!

Pyaarey Allah!

Saturday, September 3, 2011

Organize your desktop icons

0 comments

http://ping.fm/BJXd7

Friday, September 2, 2011

Social Networking from one place

0 comments

Testing a cool application ping.fm which is really cool. Micro and macro blog fro one place.

0 comments

Testing ping.fm for quick microblogging from one place.

Add Firewall Exception for Sql Sever Connection over Network

0 comments

  1. On the Start menu, click Run, type WF.msc, and then click OK.
  2. In the Windows Firewall with Advanced Security, in the left pane, right-click Inbound Rules, and then click New Rule in the action pane.
  3. In the Rule Type dialog box, select Port, and then click Next.
  4. In the Protocol and Ports dialog box, select TCP. Select Specific local ports, and then type the port number of the instance of the Database Engine, such as 1433 for the default instance. Click Next.
  5. In the Action dialog box, select Allow the connection, and then click Next.
  6. In the Profile dialog box, select any profiles that describe the computer connection environment when you want to connect to the Database Engine, and then click Next.
  7. In the Name dialog box, type a name and description for this rule, and then click Finish.

Saturday, August 6, 2011

My style of Generic repository with EF4.1(x=anything First)

0 comments

/* People even in Microsoft are breaking rules of 
Repository Pattern
by making code first generic repository.
If you wanna break rules then
nothing is better than my repository pattern 
:) No pain and lots of gain.
No need to make a lots of concrete classes like 
public class UserRepository : Repository<User>
Simple make one Generic Repository 
and start rocking.
Criticism is welcomed, warmly. */ 
 
class Program
    {
        static void Main(string[] args)
        {
            var ctx = new GiobyContextContainer();
            GenericRepository r = new GenericRepository(ctx);
            User user = r.Find<User>(8);
            Folder f = r.Find<Folder>(1);
 
            AccessOnFolder aof = new AccessOnFolder
            {
                AllowedUserID = user.UserID,
                FolderID = f.FolderID,
                ReadPermission = true,
                WritePermission = true
            };
            r.Create<AccessOnFolder>(aof);
            r.Save();
        }
    }

Friday, July 29, 2011

Cascading and Bulk Delete through one stored procedure

0 comments

Create PROCEDURE CascadeDelete
@table varchar(100), -- Table name
@column varchar(100), -- Primary key column name
@value varchar(max) -- Must be comma separated list of primary keys 
AS

Print 'Need to delete from ' + @table + ' where ' + @column 
+ ' IN (' + convert(varchar, @value) + ')'

DECLARE @refrencingTable varchar(100)
DECLARE @refrencingColumn varchar(100)
DECLARE @refrencingTablesPrimaryKeyColumn varchar(100)

DECLARE @sql varchar(4000)
DECLARE @keyval bigint

BEGIN TRY
SET @sql = 'delete from ' + @table + ' where ' + @column 
+ ' IN (' + convert(varchar, @value) + ')'
EXEC (@sql)
PRINT 'Bulk delete worked' 
-- Deleting was successful becuase the table does not seem to refer anything
END TRY
BEGIN CATCH
-- first, find all the objects which refer to this object
DECLARE ref CURSOR LOCAL FOR -- ref is a cursor to find references
SELECT DISTINCT
OBJECT_NAME(f.parent_object_id) AS table_name
, COL_NAME(fc.parent_object_id, fc.parent_column_id) AS constraint_column_name
, sc.name as table_key
FROM sys.foreign_keys AS f
INNER JOIN sys.foreign_key_columns AS fc
ON f.object_id = fc.constraint_object_id
JOIN sys.columns as sc
ON f.parent_object_id = sc.object_id
JOIN sys.indexes as i
on f.parent_object_id = i.object_id
AND i.is_primary_key = 1
JOIN sys.index_columns as ic
on i.index_id = ic.index_id
AND i.object_id = ic.object_id
AND i.is_primary_key = 1
AND sc.column_id = ic.column_id
WHERE f.referenced_object_id = OBJECT_ID(@table);

-- loop over the referring objects
OPEN ref
FETCH NEXT FROM ref INTO @refrencingTable, @refrencingColumn, 
@refrencingTablesPrimaryKeyColumn
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC cascadeDelete @refrencingTable, @refrencingColumn, @value 
-- Self call to delete references
print @table + ' is referenced by ' + @refrencingTable + '.' + @refrencingColumn

-- get all the id values for all the referring records and put them into a temp table
SET @sql = 'SELECT ' + @refrencingTablesPrimaryKeyColumn 
+ ' as keyval FROM ' + @refrencingTable + ' WHERE ' + @refrencingColumn 
+ ' IN (' + CONVERT(varchar, @value) + ')'

CREATE TABLE #temp (
keyval int
)

INSERT INTO #temp
EXEC (@sql)

-- loop over the table and for each row, use cascase delete to delete it.
DECLARE del CURSOR LOCAL FOR
SELECT keyval FROM #temp

OPEN del
FETCH NEXT FROM del INTO @keyval
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC cascadeDelete @refrencingTable, @refrencingTablesPrimaryKeyColumn, @keyval

FETCH NEXT FROM del INTO @keyval
END
CLOSE del
DEALLOCATE del

DROP TABLE #temp

FETCH NEXT FROM ref INTO @refrencingTable, @refrencingColumn, 
@refrencingTablesPrimaryKeyColumn
END
CLOSE ref
DEALLOCATE ref

SET @sql = 'DELETE FROM ' + @table + ' WHERE ' + @column 
+ ' IN (' + CONVERT(varchar, @value) + ')'
PRINT @sql
EXEC (@sql)
END CATCH

Friday, July 15, 2011