Saturday 22 October 2011

Web config file in ASP .net

Description of web config file :
Web.config file  is a configuration file for the Asp .net web application. An Asp .net application has one or more web.config file which keeps the configurations required for the corresponding application. 
Web.config file is written in XML with specific tags having specific meanings.
Some point in Web Config File:
A web Application can contain more than one web config file .and it reside  different sub-directories of your web application. Web config file in sub-directory take precedence over the setting that are specified in the  parent directory .
2. web config file protected by the IIS. so clients can not get to them. if u try to retrive an existing http:hellohome.com/webconfig then u will get Access denied or error .
3.When you modify the settings in the Web.Config file, you do not need to restart the Web service for the modifications to take effect.. By default, the Web.Config file applies to all the pages in the current directory and its subdirectories.
-----------------------------------------------------------------------------------------
The web config is XML file it can consist of any valid xml tags but the root element always be <configuration>

some tag description

1.The <configuration> tag has child tag, which we call section group, the <system.web> tag. A section group typically contains the setting sections, such as: compilation, customErrors, authentication, authorization, etc. The way this works is pretty straightforward: you simply include your settings in the appropriate setting sections. You want to use a different authentication mode for your Web application, you'd change that setting in the authentication section.

2.<authentication>

The authentication section controls the type of authentication used within your Web application, as contained in the attribute mode. You'll enter the value "None" if anyone may access your application. If authentication is required, you'll use "Windows", "Forms" or "Passport" to define the type of authentication. For example:

<authentication mode="Windows" />

3.<authorization>

To allow or deny access to your web application to certain users or roles, use <allow> or <deny> child tags.

<authorization>
<allow roles="Administrators,Users" />
<deny users="*" />
</authorization>

It's important to understand that ASP.NET's authorization module iterates through the sections, applying the first rule that corresponds to the current user. In this example, users carrying the role Administrators or Users will be allowed access, while all others (indicated by the * wildcard) will encounter the second rule and will subsequently be denied access.

4.<compilation>

Here, you can configure the compiler settings for ASP.NET. You can use loads of attributes here, of which the most common are debug and defaultLanguage. Set debug to "true" only if you want the browser to display debugging information. Since turning on this option reduces performance, you'd normally want to set it to "false". The defaultLanguage attribute tells ASP.NET which language compiler to use, since you could use either Visual Basic .NET or C# for instance. It has value vb by default.

5.<customErrors>

To provide your end users with custom, user-friendly error messages, you can set the mode attribute of this section to On. If you set it to RemoteOnly, custom errors will be shown only to remote clients, while local host users will see the ugly but useful ASP.NET errors -- clearly, this is helpful when debugging. Setting the mode attribute to Off will show ASP.NET errors to all users.

If you supply a relative (for instance, /error404.html) or absolute address (http://yourdomain.com/error404.html) in the defaultRedirect attribute, the application will be automatically redirected to this address in case of an error. Note that the relative address is relative to the location of the Web.config file, not the page in which the error takes place. In addition you can use <error> tags to provide a statusCode and a redirect attribute:

<customErrors mode="RemoteOnly" defaultRedirect="/error.html">
<error statusCode="403" redirect="/accessdenied.html" />
<error statusCode="404" redirect="/pagenotfound.html" />
</customErrors>

Friday 21 October 2011

Print triangle without for loop

  class Program
    {
        static void Main(string[] args)
        {

            int i = 1;
        A: if (i < 7)
            {
                pro(i);
                i++;
                goto A;
            }
            int l = 6;
        D: if (l > 0)
            {
                pro1(l);
                l--;
                goto D;

            }

            Console.Read();
        }

        protected static void pro(int j)
        {
            int k = 1;
        B:
            if (k < j)
            {
                Console.Write(k);

                k++;
                goto B;
            }
            Console.WriteLine();

        }

        protected static void pro1(int j)
        {
            int k = 1;
        C:
            if (k < j)
            {
                Console.Write(k);

                k++;
                goto C;
            }
            Console.WriteLine();

        }
    }

out put is
1
12
123
1234
12345
12345
1234
123
12
1

Thursday 20 October 2011

What is the Partial Class in C#

Partial Class:

How to find nth highest salary

Question : normally this question asked that write the query for nth highest sal ?
Answer: u use any one of them
1.    select distinct a.salary from Salary a where 5=( select count(distinct b.salary) from Salary b where     a.salary<=b.salary)
------------------------------------------------------
2. Select top 1 salary from (
  
Select Distinct top n salary
    from
employee
    order
by  salary desc) a
    order by
salary

---------------------------------------
3.SELECT distinct salary FROM
(
    SELECT DENSE_RANK() OVER (ORDER BY salary DESC) AS rank, salary
    FROM property_R
) T2
WHERE rank=1 

Wednesday 19 October 2011

Convert String

my data return string (always 9 character) as following,
000005330
000004110
000041660
how my GridView display above value into decimal. the expected result as following,
53.30
41.10
416.60
------------------------
Sample solution
  string a = "000005330";

            a = a.Insert(a.Length - 2, ".");

            float num = float.Parse(a);

Friday 14 October 2011

"Diffence between Delete And truncate in SQL Server"

Delete :
1. Delete is DML command  .
2. We can use where condition in Delete.
3. Delete retain the identity.
4. Slower then truncate because it keep logs
5.Triggers get fired in DELETE command
6. DELETE you can rollback data .
7. It deletes specified data if where condition exists
----------------------------------------------------------------------------
Truncatre:
1. Delete is DDL Command.
2. We can not use where condition in truncate .
3. If the table contains an identity column, the counter for that column is reset to the seed value that is defined for the column
4. Faster in performance wise, 
5. Triggers not fired in truncate command
6. It cannot rollback data(but it is possible).
7. It delete all data.
----------------------------------------------------------------------------
Question :TRUNCATE is much faster than DELETE.
Reason:When you type DELETE.all the data get copied into the Rollback Tablespace first.then delete operation get performed.Thatswhy when you type ROLLBACK after deleting a table ,you can get back the data(The system get it for you from the Rollback Tablespace).All this process take time.But when you type TRUNCATE,it removes data directly without copying it into the Rollback Tablespace.Thatswhy TRUNCATE is faster.Once you Truncate you cann't get back the data.
----------------------------------------------------------------------------------------------
Question : How to rollback the data when delete command execute?

Answer:
CREATE PROCEDURE [usp_delete_Pataint_master]
 (@PataintID [int],
 @BranchID [int]

)
AS
 BEGIN
 SET NOCOUNT ON
  BEGIN TRAN

  DELETE [dbo].[Pataint_master]
  WHERE ([BranchID] = @BranchID
  AND [PataintID]  = @PataintID)
  IF @@ERROR = 0
   COMMIT TRAN
  ELSE
   ROLLBACK TRAN
  RETURN (@@ERROR)
 END

GO

Saturday 8 October 2011

When use Array and when use ArrayList


Arrays are strongly typed, and work well as parameters. If you know the length of your collection and it is fixed, you should use an array.
ArrayLists are not strongly typed, every Insertion or Retrial will need a cast to get back to your original type. If you need a method to take a list of a specific type, ArrayLists fall short because you could pass in an ArrayList containing any type. ArrayLists use a dynamically expanding array internally, so there is also a hit to expand the size of the internal array when it hits its capacity.

Hope this will help you
Thanks
Rohit Srivastava

How to convert ArrayList to Array


Question arises that how to convert array list to array . below are simple code to convert Arraylist to array
Simple Answer are given below
using System;
using System.Collections;


class Program
{
    static void Main()
    {
        ArrayList arrList = new ArrayList();


        int[] arrToConvert = new int[] {};


        //Add some value to ArrayList of type integer


        arrList.Add(1);


        arrList.Add(2);


        arrList.Add(3);


        arrList.Add(4);


        //Now convert ArrayList to array


        arrToConvert = (int[])arrList.ToArray(typeof(Int32));


        foreach(int i in arrToConvert )
        {
            Console.WriteLine(i);
            
        }
     
            Console.ReadLine();
    }
}


Array List

Question: Describe the Array List ?
Question : What is difference between Array and Arraylist.
Ans: Below full Description of Array list
Firstly i want to describe why use array list

 The main problem of simple array is that their size is fixed by the number you specify when declaring the array variable: you cannot add items beyond the specified dimension. Another limitation is that you cannot insert an item inside the list. To overcome this, you can create a linked list. Instead of working from this, the .NET Framework provides the ArrayList class. With the ArrayList class, you can add new items to a list, insert items inside a list, arrange items of a list, check the existence of an item in a list, remove an item from the list, inquire about the list, or destroy the list. These operations are possible through various properties and methods.
The ArrayList class is defined in the System.Collections namespace. To use it, first declare a pointer to ArrayList
Simple definition of Array list is 
An ArrayList is an array that can dynamically grow and shrink.
Method In Array list 
Add Method :
using System.Collections;
class Program
{
    static void Main()
    {
 // Create an ArrayList and add four element elements.
 ArrayList list = new ArrayList();
 list.Add("One");
 list.Add("Two");
 list.Add("Three");
list.Add("Four")
}
}
these program add the four elemnet in Arraylist 
Ex:Call the method in which parameter of this method is Array list
using System;
using System.Collections;
class Program
{
    static void Main()
    {
 // Create an ArrayList and add three method 
 ArrayList list = new ArrayList();
 list.Add(5);
 list.Add(7);
list.Add(11);
 // Use ArrayList with method.
 Example(list);
    }
    static void Example(ArrayList list)
    {
 foreach (int i in list)
 {
     Console.WriteLine(i);
 }
    }
}
=========

Output
5
7
11
Add one ArrayList to second one
using System;
using System.Collections;
class Program
{
    static void Main()
    {
 // Create an ArrayList with two values.
 ArrayList list = new ArrayList();
 list.Add(5);
 list.Add(7);
 // Second ArrayList.
 ArrayList list2 = new ArrayList();
 list2.Add(10);
 list2.Add(13);
 // Add second ArrayList to first.
 list.AddRange(list2);
 // Display the values.
 foreach (int i in list)
 {
     Console.WriteLine(i);
 }
    }
}
Output
5
7
10
13
Description of the two ArrayLists. The first ArrayList has two elements added
to it.Next,the second ArrayList has two elements added to it. The second ArrayList is then appended to the first using the AddRange method. The example finally show
the output.

Count and Clear

The ArrayList class provides the Count property, which is a virtual property. When you use Count, no counting is actually done; This means that Count is fairly fast. The example also shows Clear.
using System;
using System.Collections;
class Program
{
    static void Main()
    {
 // Create an ArrayList with three values.
 ArrayList list = new ArrayList();
 list.Add(9);
 list.Add(10);
list.Add(20);
 // Show number of elements in ArrayList.
 Console.WriteLine(list.Count);
 // Clear the ArrayList.
 list.Clear();
 // Show count again.
 Console.WriteLine(list.Count);
    }
}
Output
3
0
Description:
Count. The Count property returns an int, and you can say that it will always be a positive value.
Clear. You can call the method Clear on your ArrayList. Internally, this calls the Array.Clear method.

ArrayList Sort and Reverse

using System;
using System.Collections;
class Program
{
    static void Main()
    {
 // Create an ArrayList with five strings.
 ArrayList list = new ArrayList();
 list.Add("Cat");
 list.Add("Zebra");
 list.Add("Dog");
 list.Add("Cow");
list.Add("lion")
 // Sort the ArrayList.
 list.Sort();
 // Display the ArrayList elements.
 foreach (string value in list)
 {
     Console.WriteLine(value);
 }
 // Reverse the ArrayList.
  list.Reverse();
  // Display the ArrayList elements again.
  foreach (string value in list)
 {
     Console.WriteLine(value);
 }
    }
}

Output
Cat
Cow
Dog
Lion
Zebra

Zebra
Lion
Dog
Cow
Cat
Description:

Implementation:

Inside the base class libraries, you can see that the Sort method here always

ends up in an internal TrySZSort or QuickSort method when it doesn't throw an exception. The TrySZSort internal method is optimized for one-dimensional arrays, also known as "Zero" arrays or vectors.
Performance of Sort. Because the TrySZSort method used in the base class libraries is implemented in native code, it has been heavily optimized. Therefore, this method is likely faster than any solution written in the C# language. In other words: using Sort on ArrayList or on Array is faster than most custom implementations.

Insert and remove elements

using System;
using System.Collections;

class Program
{
    static void Main()
    {
 // Create an ArrayList with four strings.
 ArrayList list = new ArrayList();
 list.Add("Dot");
 list.Add("Net");
 list.Add("Perls");
list.Add("VB.Net");
        // Remove middle element in ArrayList.
 list.RemoveAt(1); // It becomes [Dot, Perls,Vb.Net]
 
 // Insert word at beginning of ArrayList.
 list.Insert(0, "Carrot"); // It becomes [Carrot, Dot, Perls,VB.Net]
 // Remove first two words from ArrayList.
 list.RemoveRange(0, 2);
 
 // Display the result ArrayList.
 foreach (string value in list)
 {
     Console.WriteLine(value); // <-- "Perls"
 }
    }
}

Output
Perls
Vb.Net

Loop with for

using System;
using System.Collections;
class Program
{
    static void Main()
    {
 // Create an ArrayList with four strings.
 ArrayList list = new ArrayList();
 list.Add("man");
 list.Add("woman");
 list.Add("plant");
list.Add("animals")
 // Loop over ArrayList with for.

 for (int i = 0; i < list.Count; i++)
 {
     string value = list[i] as string;
     Console.WriteLine(value);
 }
    }
}

Output

man
woman
plant
animal
Using "as" cast with ArrayList elements. The "as" cast in C# is probably the best way to cast reference types such as string. After you cast, you can check the result for null before using the variable, to see if the cast succeeded. This is not always needed.

Get range of values

using System;
using System.Collections;

class Program
{
    static void Main()
    {
 // Create an ArrayList with 4 strings.
 ArrayList list = new ArrayList();
 list.Add("fish");
 list.Add("amphibian");
 list.Add("bird");
 list.Add("plant");
 // Get last two elements in ArrayList.
 ArrayList range = list.GetRange(2, 2);
// Display the elements.
  foreach (string value in range)
 {
     Console.WriteLine(value); // bird, plant
 }
    }
}

Output

bird
plant
Hope This will help you 
Difference Between Array and ArrayList
  • Array is: a datatype, thatcan be used by calling indexes. during runtime, one cannot really change the size of the array, unless you use the method of copying the array and getting rid of the old one. In .NET, the Visual Studio makes use of a special class to store the data. Because of this, the performance is actually quite fast. This is also because in an array, you need to specify the size and thus, the data is stored one after the other.
  • Examples:  
      • int[ ] myNumbers= new int[5];
    • myNumbers[0] = 16;
  • ArrayList is: a datatype collection. In order to fill an ArrayList, one can use the .Add property. ArrayLists are very dynamic in the sense that when you add and/or remove items from it, the performace stays the same. The internal structure of an ArrayList is an array.
  • Examples:
    • ArrayList myArray = new ArrayList();
    • myArray .Add(“Steph”);
    • string str = myArray [0];
Most of the time, we tend to choose array lists rather than arrays since we have no idea how big it is going to turn out. Arrays are ideal when you know how many items you are going to put in it. Whenever possible, it is recommended to use arrays as this drastically improves the performance.
ARRAY
ARRAYLIST
1. Char[] vowel=new Char[]; 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
4. An Array is a collection of similar items ArrayList can hold item of different types
5. An Array can have multiple dimensions  ArrayList always has exactly one dimension
Thanks 
Rohit Srivastava


Friday 7 October 2011

How to reset the identity column

Generally when we create a table and set identity and insert the data for testing purpose  but when we delete the data and feed original data then identity start with last not start with 1.
Here is solution
lets we create the table

create table ABC
(
id int identity(1,1),
name varchar(30)
)

and insert some data

insert into abc values ('ROHIT')
insert into abc values ('Riya')
insert into abc values ('Ankita')
insert into abc values('Kumar')
insert into abc values('baby')

and then select the data then output is like below


Select * from abc


1 ROHIT
2 Riya
3 Priya
4 Kumar
5 baby

but when we delete data from table abc and after that insert some data then it started with 6 id not 1
so to achieve this we use following query
after the delete command
delete from abc
we use the below query
DBCC CHECKIDENT (abc, RESEED, 0)
note : where abc is the table name
after this when we start enter the data then id start with 1 not 6

Hope this will help you if any problem then please let me know

Thanks
ROHIT SRIVASTAVA