Monday, September 15, 2014

Linq with Datatable : Asp.net C#

1. Show all data
********************************
 protected void Button1_Click(object sender, EventArgs e)
    {
        string srt = @"select e.EmployeeId,d.Designation,u.username,u.userpwd from Employees e 
                    inner join DesignationMaster d on d.Designationid=e.DesignationID
                    inner join userdet u on u.EmpId=e.EmployeeId";
        SqlDataAdapter adt = new SqlDataAdapter(srt, con);
        DataTable dt = new DataTable();
        adt.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

Output
********************************
Image1


2. Show all Asst.Professor
********************************
 protected void Button2_Click(object sender, EventArgs e)
    {
        string srt = @"select e.EmployeeId,d.Designation,u.username,u.userpwd from Employees e 
                    inner join DesignationMaster d on d.Designationid=e.DesignationID
                    inner join userdet u on u.EmpId=e.EmployeeId";
        SqlDataAdapter adt = new SqlDataAdapter(srt, con);
        DataTable dt = new DataTable();
        adt.Fill(dt);
       


        EnumerableRowCollection<DataRow> filter = from row in dt.AsEnumerable() where row.Field<string>("Designation") == "Asst.Professor" select row;
        DataView DV = new DataView();
        DV = filter.AsDataView();
        GridView1.DataSource = DV;
        GridView1.DataBind();
    }

Output
********************************
Image2


3. Filer based on user name
********************************
 protected void Button3_Click(object sender, EventArgs e)
    {
        string srt = @"select e.EmployeeId,d.Designation,u.username,u.userpwd from Employees e 
                    inner join DesignationMaster d on d.Designationid=e.DesignationID
                    inner join userdet u on u.EmpId=e.EmployeeId";
        SqlDataAdapter adt = new SqlDataAdapter(srt, con);
        DataTable dt = new DataTable();
        adt.Fill(dt);

        EnumerableRowCollection<DataRow> filter = from row in dt.AsEnumerable() where row.Field<string>("username").Contains(TextBox1.Text) select row;

        DataView dv = new DataView();
        dv = filter.AsDataView();
        GridView1.DataSource = dv;
        GridView1.DataBind();

    }

Output
********************************
Image3

Linq Simple Example with Array C#


  protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "";
        int[] a = { 1, 5, 4, 8, 9, 6 };

        Label1.Text = "Original Array";

        for (int i = 0; i < a.Length; i++)
        {
            Label1.Text += "<br>" + a[i].ToString();
        }

        Label1.Text = "<br>Linq";

        var elements =from element in a where element>5 select element;

        foreach (var x in elements)
        {
            Label1.Text += "<br>" + x;
        }

    }
Output
OutPut Image

Wednesday, September 3, 2014

C# code to convert Number to Words

public static string NumbersToWords(int inputNumber)
    {
        int inputNo = inputNumber;

        if (inputNo == 0)
            return "Zero";

        int[] numbers = new int[4];
        int first = 0;
        int u, h, t;
        System.Text.StringBuilder sb = new System.Text.StringBuilder();

        if (inputNo < 0)
        {
            sb.Append("Minus ");
            inputNo = -inputNo;
        }

        string[] words0 = {"" ,"One ", "Two ", "Three ", "Four ",
            "Five " ,"Six ", "Seven ", "Eight ", "Nine "};
        string[] words1 = {"Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ",
            "Fifteen ","Sixteen ","Seventeen ","Eighteen ", "Nineteen "};
        string[] words2 = {"Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ",
            "Seventy ","Eighty ", "Ninety "};
        string[] words3 = { "Thousand ", "Lakh ", "Crore " };

        numbers[0] = inputNo % 1000; // units
        numbers[1] = inputNo / 1000;
        numbers[2] = inputNo / 100000;
        numbers[1] = numbers[1] - 100 * numbers[2]; // thousands
        numbers[3] = inputNo / 10000000; // crores
        numbers[2] = numbers[2] - 100 * numbers[3]; // lakhs

        for (int i = 3; i > 0; i--)
        {
            if (numbers[i] != 0)
            {
                first = i;
                break;
            }
        }
        for (int i = first; i >= 0; i--)
        {
            if (numbers[i] == 0) continue;
            u = numbers[i] % 10; // ones
            t = numbers[i] / 10;
            h = numbers[i] / 100; // hundreds
            t = t - 10 * h; // tens
            if (h > 0) sb.Append(words0[h] + "Hundred ");
            if (u > 0 || t > 0)
            {
                if (h > 0 || i == 0) 
                    sb.Append("and ");
                if (t == 0)
                    sb.Append(words0[u]);
                else if (t == 1)
                    sb.Append(words1[u]);
                else
                    sb.Append(words2[t - 2] + words0[u]);
            }
            if (i != 0) sb.Append(words3[i - 1]);
        }

        return sb.ToString().TrimEnd();        
    }