Monday, July 11, 2016

List to Datatable Converter Using C#

In this article I will describe you how to convert a List objects to a DataTable.


//List to Datatable Converter
   public static DataTable ListToDataTable<T>(IList<T> data)
   {
      DataTable table = new DataTable();

      //special handling for value types and string
      if (typeof(T).IsValueType || typeof(T).Equals(typeof(string)))
      {

         DataColumn dc = new DataColumn("Value");
         table.Columns.Add(dc);
         foreach (T item in data)
         {
            DataRow dr = table.NewRow();
            dr[0] = item;
            table.Rows.Add(dr);
         }
      }
      else
      {
         PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
         //Get all the properties
         foreach (PropertyDescriptor prop in properties)
         {
            //Setting column names as Property names
            table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
         }
         foreach (T item in data)
         {
            DataRow row = table.NewRow();
            foreach (PropertyDescriptor prop in properties)
            {
               try
               {
                  //inserting property values to datatable rows
                  row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
               }
               catch (Exception ex)
               {
                  row[prop.Name] = DBNull.Value;
                  var error = ex.Message;
               }
            }
            table.Rows.Add(row);
         }
      }
      return table;
   }

  protected void Button1_Click(object sender, EventArgs e)
  {
     List<string[]> list = new List<string[]>();
     list.Add(new string[] { "Column 1", "Column 2", "Column 3" });
     list.Add(new string[] { "Row 2", "Row 2" });
     list.Add(new string[] { "Row 3" });

     // Convert to DataTable.
     DataTable table = ListToDataTable(list);
     GridView1.DataSource = table;

  }

Wednesday, February 17, 2016

SQL Query for pagination

SELECT *
FROM ( SELECT
ROW_NUMBER() OVER (ORDER BY id) as row,*
FROM tbl_Stock) a
WHERE row > 0 and row <= 20

UpdateProgress


Design Code :

<style type="text/css">
        .modal {
            position: fixed;
            z-index: 999;
            height: 100%;
            width: 100%;
            top: 0;
            background-color: Black;
            opacity: 0.6;
        }

        .center {
            z-index: 1000;
            margin: 300px auto;
            padding: 10px;
            width: 130px;
        }

            .center img {
                height: 128px;
                width: 128px;
            }
    </style>
    <form id="form1" runat="server">
        <asp:ScriptManager runat="server"></asp:ScriptManager>
        <asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel1">
            <ProgressTemplate>
                <div class="modal">
                    <div class="center">
                        <img alt="" src="loader.gif" />
                    </div>
                </div>
            </ProgressTemplate>
        </asp:UpdateProgress>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <div style="text-align:center;">
                    <asp:Button ID="Button1" Text="Click Me" runat="server" OnClick="Button1_Click" />
                </div>
            </ContentTemplate>
        </asp:UpdatePanel>
    </form>

Put code in code behind (.cs) file.

protected void Button1_Click(object sender, EventArgs e)
    {
        System.Threading.Thread.Sleep(5000);
    }

Output :





Find detail about Stored Procedure or Table


How to know table or columnname used in which stored procedure?


select * from sys.sql_modules where definition like '%id%'

How to know “stored procedure” created and modified date?


SELECT name, create_date, modify_date FROM sys.objects
WHERE type = 'P'

How to know “table” created and modified date?


SELECT name, create_date, modify_date FROM sys.objects
WHERE type = 'U'


How to know “view” created and modified date?


SELECT name, create_date, modify_date FROM sys.objects
WHERE type = 'V'