Tuesday, October 18, 2011

Mathematical Functions in C#

Popular mathematical functions are summarized in the following table. Note that certain functions do not require the Math. prefix.

Function Use
Math.Abs() Returns the absolute value.

Math.Abs(-10) returns 10.

Math.Ceiling() Returns an integer that is greater than or equal to a number.

Math.Ceiling(5.333) returns 6.

Fix() Returns the integer portion of a number.

Fix(5.3333) returns 5.

Math.Floor() Returns an integer that is less than or equal to a number.

Fix(5.3333) returns 5.

Int() Returns the integer portion of a number.

Int(5.3333) returns 5.

Math.Max() Returns the larger of two numbers.

Math.Max(5,7) returns 7.

Math.Min() Returns the smaller of two numbers.

Math.Min(5,7) returns 5.

Math.Pow() Returns a number raised to a power.

Math.Pow(12,2) returns 144.

Rnd() Returns a random number between 0 and 1. Used in conjunction with Randomizestatement to initialize the random number generator.

Math.Round() Rounds a number to a specified number of decimal places. Rounds up on .5.

Math.Round(1.1234567,5) returns 1.12346.

Math.Sign() Returns the sign of a number. Returns -1 if negative and 1 if positive.

Math.Sign(-5) returns -1.

Math.Sqrt() Returns the square root of a positive number.

Math.Sqrt(144) returns 12.

Sunday, October 9, 2011

How to Convert DataReader to DataTable


string connString = ConfigurationManager.ConnectionStrings["NorthwindConn"].ConnectionString;
        SqlConnection conn = new SqlConnection(connString);
        string query = "SELECT * FROM Customers";
        SqlCommand cmd = new SqlCommand(query, conn);
        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        DataTable dt = new DataTable();

        dt.Load(dr);

        conn.Close();

Wednesday, October 5, 2011

how to query sql server database size


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string strCount;
            SqlConnection Conn = new SqlConnection
               ("Data Source=W3INTECH-PC\\sqlexpress;Initial Catalog=aliintDatb;Integrated Security=True;");
            SqlCommand testCMD = new SqlCommand
               ("sp_spaceused", Conn);

            testCMD.CommandType = CommandType.StoredProcedure;

            Conn.Open();

            SqlDataReader reader = testCMD.ExecuteReader();

            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    Console.WriteLine("Name: " + reader["database_name"]);
                    Console.WriteLine("Size: " + reader["database_size"]);
                }
            }

            Console.ReadLine();
        }
    }
}

Tuesday, October 4, 2011

Convert HTML Table to DataSet in Asp.Net


private DataSet ConvertHTMLTablesToDataSet(string HTML)
    {
        // Declarations 
        DataSet ds = new DataSet();
        DataTable dt = null;
        DataRow dr = null;
        DataColumn dc = null;
        string TableExpression = "<table[^>]*>(.*?)</table>";
        string HeaderExpression = "<th[^>]*>(.*?)</th>";
        string RowExpression = "<tr[^>]*>(.*?)</tr>";
        string ColumnExpression = "<td[^>]*>(.*?)</td>";
        bool HeadersExist = false;
        int iCurrentColumn = 0;
        int iCurrentRow = 0;

        // Get a match for all the tables in the HTML 
        MatchCollection Tables = Regex.Matches(HTML, TableExpression, RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase);

        // Loop through each table element 
        foreach (Match Table in Tables)
        {
            // Reset the current row counter and the header flag 
            iCurrentRow = 0;
            HeadersExist = false;

            // Add a new table to the DataSet 
            dt = new DataTable();

            //Create the relevant amount of columns for this table (use the headers if they exist, otherwise use default names) 
            if (Table.Value.Contains("<th"))
            {
                // Set the HeadersExist flag 
                HeadersExist = true;

                // Get a match for all the rows in the table 
                MatchCollection Headers = Regex.Matches(Table.Value, HeaderExpression, RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase);

                // Loop through each header element 
                foreach (Match Header in Headers)
                {
                    dt.Columns.Add(Header.Groups[1].ToString());
                }
            }
            else
            {
                for (int iColumns = 1; iColumns <= Regex.Matches(Regex.Matches(Regex.Matches(Table.Value, TableExpression, RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase)[0].ToString(), RowExpression, RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase)[0].ToString(), ColumnExpression, RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase).Count; iColumns++)
                {
                    dt.Columns.Add("Column " + iColumns);
                }
            }


            //Get a match for all the rows in the table 

            MatchCollection Rows = Regex.Matches(Table.Value, RowExpression, RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase);

            // Loop through each row element 
            foreach (Match Row in Rows)
            {
                // Only loop through the row if it isn't a header row 
                if (!(iCurrentRow == 0 && HeadersExist))
                {
                    // Create a new row and reset the current column counter 
                    dr = dt.NewRow();
                    iCurrentColumn = 0;

                    // Get a match for all the columns in the row 
                    MatchCollection Columns = Regex.Matches(Row.Value, ColumnExpression, RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase);

                    // Loop through each column element 
                    foreach (Match Column in Columns)
                    {
                        // Add the value to the DataRow 
                        dr[iCurrentColumn] = Column.Groups[1].ToString();

                        // Increase the current column  
                        iCurrentColumn++;
                    }

                    // Add the DataRow to the DataTable 
                    dt.Rows.Add(dr);

                }

                // Increase the current row counter 
                iCurrentRow++;
            }


            // Add the DataTable to the DataSet 
            ds.Tables.Add(dt);

        }

        return ds;

    }

Ajax CalendarExtender displaying at wrong position in Chrome

< script type ="text/javascript" language ="javascript">     function onCalendarShown(sender, args)...