Sunday, April 20, 2014

Connection String

<add name="Default" connectionString="Database=DefaultDB;Server=192.168.1.1;user id=man;password=boy;MultipleActiveResultSets=true" providerName="System.Data.SqlClient" />

MultipleActiveResultSets=true"

            rdr1=cmd1.ExecuteReader();
            rdr2=cmd2.ExecuteReader();
   
Default is False, If set to "true", it allow user to use ExecuteReader more than 1 query with a single database connection.

  • Mutiple Query and Stored Prod is allowed
  • Read Only Data 

If set to "false", each sqlconnection object and datareader must must be disposed.Otherwise it will get the error "There is already an open DataReader associated with this Connection- error".

Friday, April 18, 2014

Advanced SQL Statement 1.0

WITH
WITH x AS (...)
SELECT * FROM x

Write subqueries for use in a larger SELECT query.
a temporary table or inline view.
________________________________________________________________

ROW_NUMBER
SELECT ROW_NUMBER() OVER(ORDER BY Name DESC) AS Row

Returns the sequential number of a row
________________________________________________________________
CASE
CASE is used to provide if-then-else type of logic to SQL.

1) Simple CASE expression:  
SELECT  ProductNumber, Category =
      CASE ProductLine
         WHEN 'R' THEN 'Road'
         WHEN 'M' THEN 'Mountain'
         ELSE 'Not for sale'
      END,
   Name
FROM Production.Product
compare an expression to static values

2) Searched CASE expression:
SELECT ProductNumber, Name, "Price Range" = 
   CASE 
      WHEN ListPrice =  0 THEN 'not for resale'
      WHEN ListPrice < 50 THEN 'Under $50'
      WHEN ListPrice > 50 and ListPrice < 250 THEN 'Under $250'
      ELSE 'Over $1000'
      END
FROM Production.Product
compare an expression to one or more logical conditions.
Combination
WITH SortedCode AS (SELECT ROW_NUMBER() OVER 
(ORDER BY cm1.ServiceCode asc) AS RowNumber, p.PriceCodeID, 
(CASE p.Category 
 WHEN 0 THEN 'Content Fee'
 WHEN 1 THEN 'Subscription Fee' ELSE '' END) AS Description
 FROM PriceCodes p 
 INNER JOIN Countries c On c.CountryID = p.CountryID  
 WHERE p.Category != 2 ) 
SELECT * FROM SortedCode WHERE RowNumber BETWEEN 1 AND 25

Monday, April 14, 2014

ASP.NET Find Control in Gridview

foreach (GridViewRow row in gvTest.Rows)
{
 var id = ((HiddenField)row.Cells[1].Controls[3]).Value;
 var rowIndex = row.RowInex;

}

Cells[] is start from 0, boundField is count but you cannot get the Controls.

Controls[] is start from 1,3,5,7

How to get it ?
Put Break Point at the code > Debug > "Immediate Window" > row.Cells[1].Controls[1].ClientID

Friday, April 11, 2014

C# ENUM

public enum Project
{
  Regular,
  Important,
  Critical=6,
  Super

}

Console.WriteLine(Project.Regular); //Regular

Console.WriteLine(Convert.ToInt32(Project.Regular)); //0
Console.WriteLine((int)Project.Regular); //0

From INT

Console.WriteLine(Project)6; //Critical

From String
Console.WriteLine((Project)Enum.Parse(typeof(Project), "Important"));  //Important

Make it as array and change it to Listitem(Both is String)
Enum.GetNames(typeof(Project)).Select(x => new ListItem(x));

Make it as array and change it to Listitem(Value int , Text is String)
Enum.GetNames(typeof(Project)).Select(x => new ListItem {
 Value= Convert.ToInt32((Enum. Parse(typeof(Project),x))).ToString(),
 Text = x

});

*Remember 
rblStatus.DataValueField = "Value";
rblStatus.DataTextField = "Text";


public class EnumUtils<T>
{

public static string GetDescription(T enumValue, string defaultDesc)
{
    string desc = defaultDesc;
    FieldInfo fi = enumValue.GetType().GetField(enumValue.ToString());

    if (null != fi)
    {
        object[] attrs = fi.GetCustomAttributes(typeof(DescriptionAttribute), true);
        if (attrs != null && attrs.Length > 0)
        {
            object attr = attrs.FirstOrDefault(p => p.GetType().Name ==                                                  typeof(DescriptionAttribute).Name);
            if (attr != null)
            {
                desc = ((DescriptionAttribute)attr).Description;
            }
        }
    }
    return desc;

}

public static List<Enum> ToEnumList()
{
     return Enum.GetValues(typeof(T)).Cast<Enum>().ToList();
}

public static List<string> ToNameList()
{
     return Enum.GetNames(typeof(T)).ToList();
}

public static T FromName(string name)
{
            return (T)Enum.Parse(typeof(T), name);
        }

public static T FromDescription(string description)
{
       Type t = typeof(T);
       T result = default(T);
       foreach (FieldInfo fi in t.GetFields())
       {
           object[] attrs = fi.GetCustomAttributes(typeof(DescriptionAttribute), true);
           if (attrs != null && attrs.Length > 0)
           {
               foreach (DescriptionAttribute attr in attrs)
               {
                   if (attr.Description.Equals(description))
                   {
                       result = (T)fi.GetValue(null);
                   }
               }
           }
       }
       return result;
}
}

//Calling the Mehod
public static List<SelectListItem> TransRiskLevel()
{
    var list = new List<SelectListItem>();

    var actionList = EnumUtils<TransRiskLevel>.ToValueList<TransRiskLevel>();
    foreach (var action in actionList)
    {
        list.Add(new SelectListItem
        {
            Text = EnumUtils<TransRiskLevel>.GetDescription(action),
            Value = ((int)action).ToString()
        });
    }

    return list;


}


C# Property

GET
  • The get { } implementation must include a return statement.
SET
  • The set { } implementation receives the implicit argument "value." This is the value to which the property is assigned.

Example example = new Example();
example.Number = 10; // set { }

Console.WriteLine(example.Number); // get { }