Friday, 26 January 2018

ArrayList in C# and List in C#

Major difference between ArrayList and List

ArrayList -
1) Namespace System.Collections contain ArrayList ( This namespace is added by default when we creat any aspx page in C#)
2) Create ArrayList :
ArrayList stringArrayList = new ArrayList();
Here we don’t need to specify object type arraylist going to contain,store different type of objects/items/
3) ArrayList stores everything as an object, and when you retrieve the value you have to cast it back into the actual object type if you want to use any of the objects methods, etc.
4) It is like Array of Objects.

List<T> -
1) Namespace System.Collections.Generic List<T> (we need to add namespace if we want to use Generic Types)
2) Create List<T>:
List<string> stringList = new List<string>();
i.e.
List<type> nameOfList = new List<type>(); & type means object type which List<T> going to hold.
3) List<T> is a generic container type that you specify what type it can hold when you instantiate the List<T>. You don't have to cast the object back into the type.
4) It is newly added in .Net 2.0 & onwards, fast processing no need of casting explicitly.


ArrayList  in C#
The Add method on ArrayList is used in almost every program. It appends a new element object to the very end of the ArrayList. You can keep adding elements to your collection until memory runs out. The objects are stored in the managed heap.

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

namespace AbstractCLass
{
    class Class_Main
    {
        static void Main(string[] args)
        {
            ArrayList objArrayList = new ArrayList();

            objArrayList.Add("C#");
            objArrayList.Add(".Net");
objArrayList.Add("By Sameer");
            ExampleOfArrayList(objArrayList);

        }

        static void ExampleOfArrayList(ArrayList objArrayList)
        {
            foreach (string List in objArrayList)
            {
                int i = objArrayList.Count;
                Console.WriteLine("Elements in Array List are"+List);
            }
        }
    }
}
------------------------------------------------------------------------------------------------
List  in C#

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

namespace AbstractCLass
{
    class Class_Main
    {
        static void Main(string[] args)
        {
            List<string> Employee = new List<string>();
            Employee.Add("C#");
            Employee.Add("By");
            Employee.Add("SameeR");
            ExampleOfArrayList(Employee);
            Console.ReadLine();
        }
        static void ExampleOfArrayList(List<string> objList)
        {
            foreach (string List in objList)
            {
                int cnt = objList.Count;
                Console.WriteLine("Elements in List are" + List);
            }
        }
    }
}

Friday, 19 January 2018

Like operator

Like operator

Like operator is used in where clause to search for a specified pattern in a column.
Gets data from table where firstname start with “n”
select * from Employees
where FirstName like 'n%'

Gets data from table where firstname end with “e”
select * from Employees
where FirstName like '%e'

Gets data from the table where firstname containg the whole word “au”
select * from Employees
where FirstName like '%au%'

retrieve data like “a” is at 2nd posotion at starting.
select * from Employees
where

FirstName like '_a%'

Monday, 15 January 2018

Add TextBox value to GridView Without using Database

GridView :

Dear All,
This Demo shows you how to bind TextBox value to Grid without using Database
Eg.
Ø  Suppose that u have 3 TextBox and one Button Control and Grid
Ø  TextBox1 for FirstName, TextBox2 for LastName and TextBox3 for Id
Ø   Once u click on button the data get added to Grid Row
Ø  Then on next button click second new row get added to row
Ø  And so on…..
C# Code:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

public partial class TxtBoxDemo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            DataColumn dc = new DataColumn("First Name");
            DataColumn dc1 = new DataColumn("Last Name");
            DataColumn dc2 = new DataColumn("Id");

            dt.Columns.Add(dc);
            dt.Columns.Add(dc1);
            dt.Columns.Add(dc2);
            ds.Tables.Add(dt);
            Session["data"] = ds;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DataSet ds = (DataSet)Session["data"];
        DataRow dr = ds.Tables[0].NewRow();
        dr[0] = txtFirstName.Text.Trim();
        dr[1] = txtLastName.Text.Trim();
        dr[2] = txtId.Text.Trim();
        ds.Tables[0].Rows.Add(dr);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
}

Wednesday, 10 January 2018

AutoEventWireup attribute in ASP.NET<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
The ASP.NET page framework supports an automatic way to associate page events and methods. If the AutoEventWireup attribute of the Page directive is set to true, the page framework calls page events automatically, specifically the Page_Init and Page_Load methods. In that case, no explicit Handles clause or delegate is needed.
  • AutoEventWireup is an attribute in Page directive. 
  • AutoEventWireup is a Boolean attribute that indicates whether the ASP.NET pages events are auto-wired.
  • AutoEventWireup will have a value true or false. By default it is true.
There is no event or method associated with Page_Load. Those events whose inline event is not there but that should be executed, for that purposed AutoEventWireup="true".

Disadvantages of AutoEventWireup attribute
  • AutoEventWireup uses fixed naming convention for the events. Page events handlers have specific predictable names. This limits your flexibility in how you name event handlers.
  • If you do set AutoEventWireup to true, Visual Studio will generate code to bind the events and the page framework will automatically call events based on their names. This can result in the same event code being called twice when the page runs. As a consequence, you should always leave AutoEventWireup set to false when working in Visual Studio.
  • Another disadvantage is that performance is adversely affected, because ASP.NET searches for methods at run-time. For a Web site with high traffic volumes, the impact on performance could be significant.
AutoEventWireup="true" target is for page events only. In case of AutoEventWireup method are not case sensitive. (Page_Load or page_load both will work).

If AutoEventWireup="false" but still you want to executed Page event (Page_Load). In this you have to explicitly code for it.

<form id="form1" runat="server" onload="Page_Load">

Monday, 1 January 2018

Find Second Max value in SQL


There are different ways where we can write query to find second max value in column
 We can write a sub-query to achieve the result
SELECT MAX (SALARY) FROM EMPLOYEE WHERE SALARY NOT IN (SELECT MAX (SALARY) FROM EMPLOYEE)
The first sub-query in the WHERE clause will return the MAX SALARY in the table, the main query SELECT’s the MAX SALARY from the results which doesn’t have the highest SALARY.


Second highest salary from emp table for each department

SELECT Max (e1.salary) from tbl_emp e1 where e1.Salary NOT IN (select Max (e2.salary) from tbl_emp e2 group by e2.departId) group by e1.departId


To find 2nd highest salary

SELECT * FROM Employee E1 WHERE 1 =
(SELECT COUNT (DISTINCT Salary) FROM Employee E2 WHERE E1.Salary < E2.Salary)

Select max(salary) from employee where salary in (select distinct Top 2 salary from employee order by salary desc)



always use indexing to get highest values
best practice to get highest salary or any value is based on Row_number, Dense_rank, rank

WITH T AS
(
SELECT *,
       DENSE_RANK() OVER (ORDER BY ID Desc) AS Rnk
FROM tblUserRelationship
)
SELECT ID
FROM T
WHERE Rnk=3;

I will post the same very soon in brief.
please check other post for the ranking operation