Monthly Archives: July 2010

Array vs ArrayList

In yet another round of fight, today we have contenders Array and ArrayList:

ARRAY ARRAYLIST
1. Array can store single data like Char[] vowel=new Char[]; Array list you can store multiple datatypes and inserting and removing element is easy.

ArrayList a_list=new ArrayList();

2. Array is in the System namespace ArrayList is in the System.Collections namespace.
3. The capacity of an Array is fixed. ArrayList can increase and decrease size dynamically

Ex:

ArrayList list = new ArrayList();

list.Add(“One”);

list.Add(“Two”);

list.Add(“Three”);

ARRAY

ARRAYLIST

1.

Array can store single data like Char[] vowel=new Char[];

Array list you can store multiple datatypes and inserting and removing element is easy.

ArrayList a_list=new ArrayList();

2.

Array is in the System namespace

ArrayList is in the System.Collections namespace.

3.

The capacity of an Array is fixed.

ArrayList can increase and decrease size dynamically

Ex:

        ArrayList list = new ArrayList();
        list.Add("One");
        list.Add("Two");
        list.Add("Three");

Leave a comment

Filed under C#

Compile Code into Assemblies

To service user request thru application code, it need to be compiled to one or more assemblies.

Assembly files have extension .dll. We can write ASP.net code in many languages like C#, VB.net, J# & others but when compiled, but when compiled, it will be translated into language-independent or CPU-independent language i.e. MS Intermediate Language(MSIL). At run-time in the context of .Net framework, the MSIL gets translated into CPU-specific instructions.

 Benefits of compiling the application code to Assemblies:

Performance:- Compiled code is much faster then scripting language because it’s representation is closer to machine code & doesn’t require additional parsing.

Security:- Compiled code is difficult to the reverse engineer as it lacks the readability & high level of abstraction.

Stability:- Code is checked at compile time for syntax error, type safety, & other problems. By caching these errors at build-time we can eliminate many errors in our code.

Inter-operability:- As MSIL code support all .Net languages so if you are writing an ASP.Net web page in C# then you can use reference of assemblies written in VB.net too.

Leave a comment

Filed under .Net

Factorial

There are actually two ways to get factorial i.e. using recursion or without recursion:

With Recursion

protected int Factorial(int num)

{

              if (num == 1)

                      return num;

              return (num * Factorial(num – 1));

}

Without Recursion

protected int FactorialNoRecursion(int num)

{

             int result=1;

             if (num == 1)

                      return num;

              for (int i = 2; i <= num; i++)

                     result *= i;

             return result;

}

Leave a comment

Filed under C#

AutoCompleteExtender

AutoCompleteExtender control pulls words that start with the prefix typed into the textbox and display in a popup panel. It comes under AJAX control toolkit.

To implement the same we need to call webservice (WebMethod) thru Ajax & First thing you have to do is to add the ScriptService reference to the webserive as follows. 

[System.Web.Script.Services.ScriptService]:

 

 [WebService(Namespace = “http://tempuri.org/&#8221;)]

 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

 [System.Web.Script.Services.ScriptService]

 public class SampleWebService : System.Web.Services.WebService

 {

      [WebMethod]

      public string[] GetName(string prefix)

      {

            List<string> names = null;

            string query = string.Empty;

            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings[“ConnectionString”]);

             query = “SELECT Name FROM Student WHERE Name = ” + prefix + “%”;

            SqlCommand com = new SqlCommand(query);

            con.Open();

            SqlDataReader dr = com.ExecuteReader();

            if (dr.HasRows)

            {

                names = new List<string>();

                while (dr.Read())

                {

                    names.Add(dr.GetString(1));

                }

            }

           

            return names.ToArray();

        }

    }

 Now at the design level, apart from specifying ScriptManager, we need to provide a <Services> tag as shown below and use Ajax AutocompleteExtender with ServicePath & ServiceMethod:

 <body>

    <form id=”form1″ runat=”server”>

        <asp:ScriptManager ID=”ScriptManager1″ runat=”server”>

            <services>

                <asp:ServiceReference Path=”AutoComplete.asmx” />

            </services>

        </asp:ScriptManager>

        <div>

            <asp:TextBox ID=”txtCountry” runat=”server”></asp:TextBox>

<ajaxToolkit:AutoCompleteExtender runat=”server” ID=”autoComplete1″ TargetControlID=”txtCountry”

ServicePath=”AutoComplete.asmx” ServiceMethod=”GetCountriesList” MinimumPrefixLength=”1″

            EnableCaching=”true” />

        </div>

    </form>

</body>

 

Leave a comment

Filed under Ajax

UpdatePanel

UpdatePanel

The UpdatePanel control is a server control which used to achieve asynchronously postback of limited to regions of the page.
This process is coordinated by the ScriptManager server control.

By default, any postback control inside an UpdatePanel control causes an asynchronous postback and refreshes the panel’s content. However, you can also configure other controls on the page to refresh an UpdatePanel control. You do this by defining a trigger for the UpdatePanel control.
eg:
<form runat=”server”>
    <div>
    <asp:Button
                Text=”Refresh Panel”
                runat=”server” />
    <asp:ScriptManager
                       runat=”server” />
    <asp:UpdatePanel
                     UpdateMode=”Conditional”
                     runat=”server”>
                     <Triggers>
                       <asp:AsyncPostBackTrigger ControlID=”Button1″ />
                     </Triggers>
                     <ContentTemplate>
                     <fieldset>
                     <legend>UpdatePanel content</legend>
                     <%=DateTime.Now.ToString() %>
                     </fieldset>
                     </ContentTemplate>
    </asp:UpdatePanel>

A trigger’s control event is optional. If you do not specify an event, the trigger event is the default event of the control.

UpdateMode (Always, Conditional)
If the UpdateMode property is set to Always, the UpdatePanel control’s content is updated on every postback that originates from anywhere on the page. This includes asynchronous postbacks from controls that are inside other UpdatePanel controls, and postbacks from controls that are not inside UpdatePanel controls.

If the UpdateMode property is set to Conditional, the UpdatePanel control’s content is updated when one of the following is true:

  • When the postback is caused by a trigger for that UpdatePanel control.
  • When you explicitly call the UpdatePanel control’s Update() method.
  • When the UpdatePanel control is nested inside another UpdatePanel control and the parent panel is updated. 

To use an UpdatePanel control in a master page, you must decide how to include the ScriptManager control. If you include the ScriptManager control on the master page, it can act as the ScriptManager control for all content pages.

UpdatePanel controls can be nested. If the parent panel is refreshed, all nested panels are refreshed also.

Leave a comment

Filed under Ajax

Abstract Class FACTS

Abstract Class FACTS

1) We can inherit abstract class from another abstract class also.

Eg:

public abstract class AbstractClass1

{
public abstract string TestMe();       

}

 It is inherited by AbstractClass2 

public abstract class AbstractClass2 : AbstractClass1

{
public abstract string DoSome();

public override string TestMe()

{

return “This is test”;

}

}

2) Abstract class can inherit Interface also.

Leave a comment

Filed under .Net, C#

GROUP BY, Where vs Having

GROUP BY is actually used to group the result-set by using aggregate functions.

Let’s say we have a table Student with columns:

Name Subject Score
Mahesh Math 80
Mahesh Eng 70
Ramesh Eng 70
Somesh Math 65
Ramesh Math 75
Somesh Eng 65
Mahesh Hindi 60
Ramesh Hindi 75

Now to get each student total marks:

SELECT Name, Sum(Score)

FROM Student

GROUP BY Name

Name Score
Mahesh 210
Ramesh 220
Somesh 130

Without GROUP BY,

SELECT Name, Sum(Score)

FROM Student

Name Score
Mahesh 560
Mahesh 560
Ramesh 560
Somesh 560
Ramesh 560
Somesh 560
Mahesh 560
Ramesh 560

WHERE vs HAVING

WHERE HAVING
WHERE is basically used to filter before the Group BY. HAVING is used to filter on GROUP BY result-set.
WHERE can’t be used with aggregate functions. Having is applied with aggregate functions only.
WHERE doesn’t need Group By. HAVING is used with Group By only.

Now for Query to get all students who have appeared in three subject exam:

SELECT Name, Count (Name)

FROM Student

GROUP BY Name

HAVING Count (Name) =3

To get all students who have scored more then 70 & appeared in three subject exam:

SELECT Name, Count(Name)

FROM Student

Where Score>70

GROUP BY Name

HAVING Count(Name)=3

 References:

http://www.programmerinterview.com/index.php/database-sql/having-vs-where-clause/

Leave a comment

Filed under SQL