Thursday, 3 October 2013

How to hard reset the Canvas 4 Android

I’ve been using the Micromax Canvas4 from few months. There was an update from Gmail app and I downloaded and installed it. Then It asked me to “AutoSyncOption” to set to “ON” and I clicked “OK”. Later the device was in a restart loop after every 3-4 second. It’s like a mess for me as I bought it recently. Later I had to hard reset the device to factory setting and wiped all the data. Then It worked like a charm. I’ll share the steps but do it at you own risk but it worked for me.

How to Hard Reset the Canvas 4 Android device:

1. Switch off the device and then Boot it with holding volume up + volume down + unlock key together.


2. Now it will start booting when I vibrates first time release the key and It’ll start in the boot mode.

3. Now select the RecoveryMode. You can navigate through the option via volume up & down key and select the option by “Option/Menu”  button.


4. It’ll further go to another option menu. Now navigate to select the Wipe user data/Factory Reset option and select ok by Menu key.

5. Now this will start restoring to it’s factory setting*(state of the device when you purchased it).

6. When the reset will finish it’ll go back to the menu again. Now navigate to the Restart the device by selecting Restart option and click ok.

Now your device will be restarted and will be running again.

Warning/Disclaimer: Do this at your own risk. It worked perfectly for me.

EF4 using TableValue type in Storeprocedure

The EF5 has been released with .Net framework 4.5 and now it’s supporting the user defined table valued types as parameter in stored procedure. In earlier versions of Entity framework it was not supported. For e.g. EF4 is widely being used with .net framework 4.0. And these types are not supported in that version. Even some people raise a User voice ticket for the same but none action has been taken and ticket was closed since this feature is shipped with .net 4.5.

For those who can not migrate to 4.5 and are looking for a work around how to work with these kinda stored procedures. I’ve created a work around solution for that. Let’s take some database test objects.


CREATE TABLE [dbo].[testTable](
[mID] [uniqueidentifier] NOT NULL,
[nID] [int] NOT NULL,
[Desc] [varchar](2000) NOT NULL,
[mID] ASC,

User Defined Table Type:
CREATE TYPE [dbo].[tbltypeTestTable] AS TABLE(
[mID] [uniqueidentifier] NULL,
[nID] [int] NULL,
[Desc] [varchar](2000) NULL

Multi Row Insert stored procedure:

CREATE PROCEDURE [dbo].[Insert_TableValuedTypeData]
@Paramtable dbo.[tbltypeTestTable] READONLY


merge dbo.testTable as target
using ( select mID, nID, [Desc]
from @Paramtable) as source
on ( target.mID = source.mID and target.nID = source.nID)
when matched then
update set target.[Desc] = source.[Desc]
when not matched then
insert values (source.mID,source.nID,source.[Desc]);

return @@error

Now if you try to generate the model from database this StoredProcedure will not be listed even if you use FunctionImport to call the stored procedure. Now to workaround I’ve created a custom class that executes this stored procedure as a Sql statement from DBContext object.


Extension method:
public static class DbContextExtension
// Extension method of DbContext object
public static void ExecuteStoreProcedure(this DbContext @this, string storeProcName, params object[] parameters)
string command = "EXEC " + storeProcName + " @Paramtable";

@this.Database.ExecuteSqlCommand(command, parameters);

Now we’ll look at the class that will prepare the data and pass it to this extension method to execute.


public class mnEntity
public bool AddMultipleRecords()

Test_Entities testEntities = new Test_Entities();

var dt = new DataTable();

dt.Rows.Add(Guid.NewGuid(), 1, "Test2");
dt.Rows.Add(Guid.NewGuid(), 2, "Test2");
dt.Rows.Add(Guid.NewGuid(), 3, "Test2");
dt.Rows.Add(Guid.NewGuid(), 4, "Test2");
dt.Rows.Add(Guid.NewGuid(), 5, "Test2");

var items = new SqlParameter("Paramtable", SqlDbType.Structured);
items.Value = dt;
items.TypeName = "dbo.tbltypeTestTable";

testEntities.ExecuteStoreProcedure("Insert_TableValuedTypeData", items);

return true;

Now let’s test this method if it work’s and call the SP with multiple values insertion:


static void Main(string[] args)
mnEntity entity = new mnEntity();
catch (Exception exception)






Voila!!!!!! It worked like charm.

If you have any comments/suggestions please feel free to drop.

Tuesday, 1 October 2013

Working with flagged enumeration in Csharp

Flags enumerations are used for masking bit fields andimage doing bitwise comparisons. They are the correct design to use when multiple enumeration values can be specified at the same time. This was quite an official definition and if you are confused then let’s take an example.

For example, theFileShare enumeration contains the ReadWrite value to specify that a shared file can be opened for reading or writing. This shows developers that they can open a shared file for reading or writing, and eliminates the need for them to learn how to specify a combination of enumeration values as a single value.

Let’s take you to the world of Enums where you are allowed to use multiple values rather than using only single value for comparison/storing. To create an Flagged Enumeration you need an attribute [Flags] to be specified on the Enum.

Here are few guidelines while declaring an Enum as flagged.

  • Do apply the System.FlagsAttribute to flags enumerations. Do not apply this attribute to simple enumerations.
  • Do use powers of two for a flags enumeration's values so they can be freely combined using the bitwise OR operation.

Note: If you do not use powers of two or combinations of powers of two, bitwise operations will not work as expected.

for e.g. Let’s take an example of Identity cards type allowed to fill an application form before applying for a position. So we’ll create an Flagged Enum.

public enum IdentityProofType
Passport = 1,
DrivingLicense = 2,
PANCard = 4,
UID = 8,
EmployeeIDCard = 16

Now we’ll create a class that keep the information about the Form’s eligibility information. for e.g. AllowedIDs to fill the form:

private IdentityProofType AllowedIDs 
= IdentityProofType.Passport | IdentityProofType.UID | IdentityProofType.DrivingLicense;

This is the magic of flagged enumeration now the single field is holding multiple values of the Enumeration. So let’s create some methods in the class EntryForm:

public class EntryForm
private IdentityProofType AllowedIDs
= IdentityProofType.Passport | IdentityProofType.UID | IdentityProofType.DrivingLicense;
// Check for eligibility validation
private bool CanApply(IdentityProofType identityProofType)
if (AllowedIDs.HasFlag(identityProofType))
Console.WriteLine("valid identity provided: {0}", identityProofType);
return true;
return false;
// Apply check and fill the form if valid
public void FillForm(string username, IdentityProofType identityProofType)
if (this.CanApply(identityProofType))
Console.WriteLine("Ready to fill the form: {0}", username);
Console.WriteLine("Not a valid id provided by: {0}.", username);

Now Let’s create a test for this class:


EntryForm form1 = new EntryForm();
EntryForm form2 = new EntryForm();

form1.FillForm("Sunny", IdentityProofType.DrivingLicense | IdentityProofType.Passport);
form2.FillForm("Amit", IdentityProofType.PANCard);

This will result in output:


So now you saw the magic of Flagged Enumerations but ask a question.


How it works??

This works because you previously used multiples of two in you enumeration. Under the covers your enumeration values looks like this (presented as bytes, which has 8 bits which can be 1's or 0's)

 Passport: 00000001
DrivingLicense: 00000010
UID: 00000100
PassPort: 00001000

Likewise, after you've set your property AllowedIDs to Passport, DrivingLicense and UId (which values where OR'ed by the pipe |), AllowedIDs looks like this

AllowedIDs: 00001110

So when you retreive the value you are actually bitwise AND'ing the values

myProperties.AllowedIDs: 00001110
IdentityProofType.DrivingLicense: 00000010
00000010 // this is the same as IdentityProofType.DrivingLicense!

Different forms of Flagged Enums

There are different ways you can define a Flagged  Enumeration. Since you know that it only allow values to the power of 2. So you can do something like this with your flagged enumeration in case you don’t want to count for the huge collection of Enum.

/// <summary>
/// Flagged with Enum with bitwise shifting
/// </summary>
public enum IdentityProofType : byte
None = 0,
Passport = 1 << 0, // 1
DrivingLicense = 1 << 1, // 2
PANCard = 1 << 2, // 4
UID = 1 << 3, // 8
EmployeeIDCard = 1 << 4, // 16

Or you can also write this as below:

public enum Options : byte
None = 0,
Passport = 1 << 0, // 1
// now that value 1 is available, start shifting from there
DrivingLicense = Passport << 1, // 2
PANCard = DrivingLicense << 1, // 4
UID = PANCard << 1, // 8
EmployeeIDCard = UID << 1 //16

Note: If you have notices than there’s a new name introduced in these called None having value 0. It is not allowed to have value starting with 0 in these enum types. But as per the MSDN:

Use None as the name of the flag enumerated constant whose value is zero. You cannot use the None enumerated constant in a bitwise AND operation to test for a flag because the result is always zero. However, you can perform a logical, not a bitwise, comparison between the numeric value and the None enumerated constant to determine whether any bits in the numeric value are set.

Hope you enjoyed reading this. Please leave comments or suggestions.