Wednesday, March 9, 2016

Convert Datatable to List

private static List<T> ConvertDataTable<T>(DataTable dt)  
{  
    List<T> data = new List<T>();  
    foreach (DataRow row in dt.Rows)  
    {  
        T item = GetItem<T>(row);  
        data.Add(item);  
    }  
    return data;  
}  
private static T GetItem<T>(DataRow dr)  
{  
    Type temp = typeof(T);  
    T obj = Activator.CreateInstance<T>();  
  
    foreach (DataColumn column in dr.Table.Columns)  
    {  
        foreach (PropertyInfo pro in temp.GetProperties())  
        {  
            if (pro.Name == column.ColumnName)  
                pro.SetValue(obj, dr[column.ColumnName], null);  
            else  
                continue;  
        }  
    }  
    return obj;  

Tuesday, March 8, 2016

Visual Studio Expand/Collapse keyboard shortcuts


Collapse to Definitions: CTRL + M + O

Expand all Outlining: CTRL + M + X

Thursday, March 3, 2016

Not Running in Release Mode in Visual Studio

  • Right click Project
  • Properties
  • Build
  • Advanced
  • Set Debug Info to full or pdb only

Tuesday, March 1, 2016

Difference Between "string" and "String"

what is the difference between both (String and string)? After completing the execution of this program when we mouse over on string is shows class System.String and  when we mouse over on String it also shows Class System.String. Class System.String statement clarifying that string is the alias name of String.

Finally:
String is the alias name of String because both contain the same things 

Difference Between "string" and "String"

what is the difference between both (String and string)? After completing the execution of this program when we mouse over on string is shows class System.String and  when we mouse over on String it also shows Class System.String. Class System.String statement clarifying that string is the alias name of String.

Finally:
String is the alias name of String because both contain the same things 

Wednesday, February 24, 2016

Convert Number To Word

public string ConvertNumbertoWords(int number)
       {
           if (number == 0)
               return "ZERO";
           if (number < 0)
               return "minus " + ConvertNumbertoWords(Math.Abs(number));
           string words = "";
           if ((number/1000000) > 0)
           {
               words += ConvertNumbertoWords(number/1000000) + " MILLION ";
               number %= 1000000;
           }
           if ((number/1000) > 0)
           {
               words += ConvertNumbertoWords(number/1000) + " THOUSAND ";
               number %= 1000;
           }
           if ((number/100) > 0)
           {
               words += ConvertNumbertoWords(number/100) + " HUNDRED ";
               number %= 100;
           }
           if (number > 0)
           {
               if (words != "")
                   words += "AND ";
               var unitsMap = new[]
               {
                   "ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "ELEVEN",
                   "TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN", "NINETEEN"
               };
               var tensMap = new[]
               {"ZERO", "TEN", "TWENTY", "THIRTY", "FORTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHTY", "NINETY"};

               if (number < 20)
                   words += unitsMap[number];
               else
               {
                   words += tensMap[number/10];
                   if ((number%10) > 0)
                       words += " " + unitsMap[number%10];
               }
           }
           return words;

       }

Monday, February 22, 2016

Difference between TextBox and TextBoxFor:

@Html.TextBox() is loosely typed method whereas @Html.TextBoxFor() is a strongly typed (generic) extension method.
TextBox() requires property name as string parameter where as TextBoxFor() requires lambda expression as a parameter.
TextBox doesn't give you compile time error if you have specified wrong property name. It will throw run time exception.
TextBoxFor is generic method so it will give you compile time error if you have specified wrong property name or property name changes. (Provided view is not compile at run time. )

Friday, February 19, 2016

Convert List to Dataset

       public DataSet ToDataSet<T>(IList<T> list)
       {
           Type elementType = typeof(T);
           using (DataSet ds = new DataSet())
           {
               using (DataTable t = new DataTable())
               {
                   ds.Tables.Add(t);
                   //add a column to table for each public property on T
                   PropertyInfo[] _props = elementType.GetProperties();
                   foreach (PropertyInfo propInfo in _props)
                   {
                       Type _pi = propInfo.PropertyType;
                       Type ColType = Nullable.GetUnderlyingType(_pi) ?? _pi;
                       t.Columns.Add(propInfo.Name, ColType);
                   }
                   //go through each property on T and add each value to the table
                   foreach (T item in list)
                   {
                       DataRow row = t.NewRow();
                       foreach (PropertyInfo propInfo in _props)
                       {
                           row[propInfo.Name] = propInfo.GetValue(item, null) ??                                       DBNull.Value;
                       }
                       t.Rows.Add(row);
                   }
               }
               return ds;

           }

Sunday, February 14, 2016

SQL Server Languages – DML, DDL, DCL & TCL

There are mainly 4 types of language statements or commands in SQL Server.
DML
DML is abbreviation of Data Manipulation Language. It is used to retrieve, modify, add, and delete data in database.
Examples: SELECT, UPDATE, INSERT, DELETE statements
1.    SELECT – used for retrieving data from the database.
2.    UPDATE – used for modifying the data in the database.
3.    INSERT – used for adding or inserting new data into database.
4.    DELETE – used for deleting the already existing data from database.
DDL
DDL is abbreviation of Data Definition Language. It is used to create, modify and destroy the structure of database objects in database.
Examples: CREATE, ALTER, DROP, TRUNCATE  statements
1.    CREATE – used for create database objects like tables, stored procedures, functions etc.
2.    ALTER – used for modifying the existing database objects like tables, stored procedures, functions etc.
3.    DROP – used to drop or deleted the existing database objects.
4.    TRUNCATE – used to delete all the records from table and to reset identity of column to initial value.
DCL
DCL is abbreviation of Data Control Language. It is used to create roles, permissions, and referential integrity as well it is used to control access to database by securing it.
Examples: GRANT, REVOKE statements
1.    GRANT – used for creating access permissions for users to database.
2.    REVOKE – used for revoking the already assigned permissions.
TCL
TCL is abbreviation of Transactional Control Language. It is used to manage different transactions occurring within a database.
Examples: COMMIT, ROLLBACK, SAVE TRANSACTION statements
1.    COMMIT – used for saving the work done in a particular transaction. For example: “Ctrl + S” in word file.
2.    ROLLBACK – used for reverting the transaction to the original state before commit. For example: “Ctrl + Z” in word file.

3.    SAVE TRANSACTION – used for setting save point in transactions.



Thursday, February 11, 2016

Difference between == and === with Example-JavaScript

This is one of the most frequently asked question in interview when one tries to judge your jQuery orJavaScript concepts.

JavaScript provides different types of operators. Here, we will be talking about strict equality andType converting equality.
Strict equality (===) means values which we are comparing must have the same type. 
This means "2" will not be equal to 2 ("2"===2 it will return false)
Type converting equality (==) means automatically it will covert variable to value irrespective of data type, either it is string or number. This means "2" will be equal to 2 ("2" == 2 it will return true)
So the double equal (==) is an auto type converting equality and three equal (===) is strict equality operator i.e it will not covert values automatically.
Difference between == and === with Example-JavaScript
Below is the demonstration with simple examples:

Demo 1

1==”1″ // it will return true because here string will converted as number
1 === “1” // it will return false because here 1 is number and “1” is string

Demo 2

0 == false // it will return true because here false is equivalent of 0
0 === false // it will return false because both are different operands
I have included the complete example for you to check the result yourself as shown below:
<!DOCTYPE HTML>
<html>
<head>
<title>Jquery - Difference between == and ===</title>
<script type="text/javascript">
function CheckDifference() {
 var val = "2";
 document.write("Value for a variable is : " +val +"<br/>"); 

 if (val == 2)
 document.write("== returns True <br/>")
 else
 document.write("== returns False <br/>")

 if (val === 2)
 document.write("=== returns True <br/>")
 else
 document.write("=== returns False <br/>")
}
</script>
</head>
<body>
<h2>JQuery or JavaScript - Difference between == and ===</h2>
<hr/>
<br/>
<div id="div1">
<input type="button" id="Check" 
 onclick="CheckDifference()" 
 value="Click to Check Difference" />
</div>
</body>
</html>