Access Denied Error while Cross Domain Communication with File upload control Internet explorer (ie)

Ok its quite tested by me that if you doing cross domain communication and raising event with file input control then it wont get uploaded. You will receive Access Denied error. So just use input control as is, no event generation from other control like $(“a”).Click(function(){$(“#file”).click()}) will work with ie. but if you using FF or Chrome than it will work fine.

Posted in Uncategorized | Tagged , , , , | Leave a comment

Solving mysteries: Facebook Share functionality

Following are few fix for Facebook share functionality

Symptoms : No image or thumbnail image is shared on wall
Cure:
1. Add “image_ref” tag on top, now they are proposing to use “og:image”.

2. Facebook cache the images once you shared with them, so if you think that you changed image and it is not appearing than don’t worry try using “Linter” it will clear the cache if you are fortunate or else your fate is in hand of Facebook Bot(it will take time to get page crawled again).

3. Ohh. have you checked the size of image, which you are sharing on facebook? it should be more than 50px of height.

Posted in Facebook | Tagged , , , , , , , | Leave a comment

For Loop – worst case performance – Big O notation – o(n)

Recently I came across one of many Big O notations ,O(N), which means “that for large enough input sizes the running time increases linearly with the size of the input – wikipedia”.

In other words if i want to search one value from array by iterating each element than the “worst case scenario” would be to get desired value at last position. so, I have to iterate array.length times to search desired item. below for loop does same,

For i=0 to array.length -2
{
If(array[i] = 1)
return true;
}
In above example we can’t guarantee that at which place “array” may contain “1” or it doesn’t contain.

In algorithmic terms it is called as “Linear Time” or O(n) time.

So no one want to face such scenario, than there are many algorithms present today for the solution like,
Binary Search and its variations. At the ends it is upto developer that how he sees the scenario and implement the solution.

Further reading from here,

Posted in Algorithms | Tagged , , , | Leave a comment

Application Design

Many times I read the questions posted on online forums that “How should be my application design”,”Where to put entities”, “Which is the best place to put POCO classes”, “Where to put EF generated entities”, sometimes like “I am building nTier application and can you help me to place my context, entities and/or model” (sometimes questions posted with MVC tag). And people answered them very well too, but still my two cents on this topic,

So in my opinion people should read the “Domain Driven Design-Eric Evans” which helped me lot to understand the layering of the application, writing code with Domain in mind.

Layering of the application is very important part of the project design and if this part is not designed well than often you will learn words “Code Smell”, “Fatty Classes” etc. So be cautioner while design the application, give good amount of time to design it.

So how should be the good design and the answer is “it depends”, but my design should,

– Accommodate new changes, at the same time it should follow the legendary Open Close Principle.
– Pluggable Architecture with Separation of concerns, means the libraries which we build, should be plugged into another application (With MVC, WPF, Win App, WCF).
– And finally developer should DRY(Don’t Repeat Yourself). That means whatever code you have written, it should be reusable. I know most of the time it doesn’t happen but still, one should think before writing “how this piece of code can be reusable”.

To reduce Developer task,
– While making an application, should use tools like T4 templates which gives us ready made code(CRUD or Creation of Master Forms) for similar kind of modules, it will save the time of writing and testing it.
– Tools like Automapper which copies one object to another. e.g. Copying Entity to POCO or vice versa.

Posted in Application Design | Tagged , , , , , | Leave a comment

Adding custom query to typed dataset

Below code will work if you working with typed dataset and you want to execute your own queries against adapters i.e. custom queries with typed dataset.

So you have to utilize the partial class of adapters.

namespace MyWin.DataSet1TableAdapters //partial class to add my custom method to update the command collection and queries.
{

public partial class EmpTableAdapter
{
public void fillformyquery(MyWin.DataSet1.EmpDataTable emp1)
{
System.Data.OleDb.OleDbCommand[] mycol = new System.Data.OleDb.OleDbCommand[1];

mycol[0] = new global::System.Data.OleDb.OleDbCommand();
mycol[0].Connection = this.Connection;
mycol[0].CommandText = “Select * from emp where name = ‘%alliswell%'”;
mycol[0].CommandType = global::System.Data.CommandType.Text;
this._commandCollection = mycol;

//this._commandCollection = new System.Data.OleDb.OleDbCommand[1];
this.Fill(emp1);
}
}
}

//calling code

public void RunMyQuery()
{
dataset1.EmpTableAdapter eda = new dataset1.EmpTableAdapter();//assume you have added adapter to the designeer
dataset1.EmpDataTable emp1 = new dataset1.EmpDataTable();
eda.fillformyquery(emp1);

}

Posted in Uncategorized | Tagged , , , , , | Leave a comment

Custom Attribute to Validate Duplicate Entity

This post is for Duplicate Entity check using C# Attributes,(Based on the question which I recently answered)

If you want to check whether any entity present in database or not, so this can be achieved using Custom Attribute which is mentioned below,

I put this as a Class level attribute but you can extend it and make it for Property levels also.

Attribute Class code as goes..
[AttributeUsage(AttributeTargets.Class)]
public class DuplicateUserAttribute : ValidationAttribute
{
private const string _defaultErrorMessage = “user ‘{0}’ Already exist”;

public DuplicateUserAttribute ()
: base(_defaultErrorMessage)
{
}

public override string FormatErrorMessage(string name)
{
return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString);

}

public override bool IsValid(object value)
{
UserEntity NewUser = value as UserEntity;
//Write here logic to validate the user is already exist in database like
context.UserList.Where(u=>u.Name ==NewUser .UserName)
return ;
}
}

Now you can use simply,
[DuplicateUser]
Class User
{

}

You need give reference of System.ComponentModel.DataAnnotations.dll.
In System.ComponentModel.DataAnnotations.dll you can find many predefined attributes also like, RequiredAttribute, DataTypeAttribute.

Posted in Uncategorized | Tagged , , , , , , , , , , | 1 Comment

Securing .Net Assembly or Products

Recently I came across the question regarding securing .Net assemblies from copying and decrypting.

Following are my thoughts on that,

But before starting anything I wanted to let you know that this is just concept.

So securing your project from re-engineering and reading files of it illegally, you can opt for  obfuscation . Obfuscation secure your application at CERTAIN level. So what is obfuscation? and the answer is obfuscation change the class/methods and variable name into meaningless words. So anyone who disassemble your source code he can read it but it will be no meaning so its safer way to save your intellectual work.

Disassembly/Re-Engineering reveal  all your source code. Tools like ILDASM does that.

Some obfuscation tools provide the Product key generation also but i guess they charge for it.

Ok if don’t want to pay extra for it then I suggest creating your own key. Product key will secure your application from copying. Product key can be any combination of your choice like,

Client + Other Details(Harddisk#+BIOS#)

Note : Maintained the separate database for it.

Also like to mention that Obfuscation secure your application at certain level. I mean a Good hacker (need extensive experience) can break it. After all it all will go to MSIL and then CLR converts to binary.

Posted in Security | Tagged , , , , , | Leave a comment

SQL Inner Join And Group By with Linq And Lambda Expression

SQL Inner Join and Group by Clause is described as follows,

 

SQL –

select Products.Name,sum(OrderLines.Quantity) as OrderQuantity  from     OrderLines

inner join Products on OrderLines.Product_Id = Products.Id

where OrderLines.Product_Id =270

group by OrderLines.Product_Id,Products.Name

Linq –

 

var result = from Orders in Context.OrderLines

group Orders by Orders.Product_Id into grpOrders

join Products in Context.Products on grpOrders.Key equals Products.Id

where Products.Id == 270

select new {ProductName = Products.Name,osum = grpOrders.Sum(o=>o.Quantity)};

Lambda –

 

var res = Context.OrderLines.Join(Context.Products, o => o.Product_Id, p => p.Id, (o, p) => new { o, p }).Where(o => o.o.Product_Id == 270)

.GroupBy(grp => new { grp.o.Product_Id, grp.p.Name })

.Select(result => new {ProductName = result.Key.Name, osum = result.Sum(o => o.o.Quantity) });

 

 

 

Posted in SQL Queries with Linq To Lambda Expressions | Tagged , , , , , , | 1 Comment

SQL Group By With Linq and Lambda Expression

A simple SQL Group by for Linq and Lambda would be as follows,

SQL –

select SUM(OrderLines.Quantity) as OrderQuantity  from OrderLines where OrderLines.Product_Id =270

group by OrderLines.Product_Id

Linq –

var result = from Orders in Context.OrderLines

where Orders.Product_Id == 270

group Orders by Orders.Product_Id into GroupedRecords

select GroupedRecords.Sum(c=>c.Quantity);

Lambda Expression –

var result = Context.OrderLines.Where(o=>o.Product_Id ==270)

.GroupBy(grp=>grp.Product_Id)

.Select(grp =>new {key = grp.Key,osum = grp.Sum(o=>o.Quantity)});

 

Posted in SQL Queries with Linq To Lambda Expressions | Tagged , , , , , | Leave a comment