29 October 2013

Label vs Literal in asp.net

Label:
The Label web server control is used to label other controls on an aspx web page/UserControl. It is used to display text with the ability to change the style (appearing) of the displayed text.
Inheritance Hierarchy:
System.Object
 
 System.Web.UI.Control
   
 System.Web.UI.WebControls.WebControl
     
 System.Web.UI.WebControls.Label
<asp:Label ID="lblFirstName" runat="server" CssClass="myCssClass"Text="First Name" />
It will be rendered as:
<span id="ctl00_ContentPlaceHolder1_lblFirstName" class="myCssClass">First Name</span> 
(NB: the id of the Label is related to the structure of your asp page and if you are using MasterPage or not)

Literal:
Use the System.Web.UI.WebControls.Literal control to reserve a location on the Web page to display text. The Literal control is similar to the Label control, except the Literal control does not allow you to apply a style to the displayed text. You can programmatically control the text displayed in the control by setting the Text property.
Inheritance Hierarchy:
System.Object
 
 System.Web.UI.Control
   
 System.Web.UI.WebControls.Literal 

<asp:Literal id="litFirstName" runat="server" Text="First Name" />

Find Duplicate records from a Database Table and Delete them


To Find Out Duplicate records in a Table and Delete them:-
For this operation i am creating a temparory table and insert records in it, with some duplicate rows. First i will find the duplicate rows and insert it in a temparory table, then delete all records from Original table which are related to Duplicate rows.
Then Insert rows from Temparory table to the original table. Now you can find the unique records in your original table.
--Creating Table
Create TAble emp( id int, name Char(30) )

--Inserting Rows
insert into emp (id,name) values (1,'adi')
insert into emp (id,name) values (2,'jc.adi')
insert into emp (id,name) values (1,'adi')
insert into emp (id,name) values (1,'adi')
insert into emp (id,name) values (3,'adi')
insert into emp (id,name) values (2,'adi')

--Now check the table
SELECT * FROM emp

--Create Duplicate Table
Create TAble #DuplicateTable
( DuplicateId int, DuplicateName Char(30) )

--Getting Duplicate Records from Original table
insert into #DuplicateTable (DuplicateId,DuplicateName) (
SELECT id,name FROM emp GROUP BY id,name HAVING COUNT(id)>1)

--Again check Duplicate table
SELECT * FROM #DuplicateTable

--DELETE RECORDS FROM original TABLE
DELETE FROM emp
FROM emp
INNER JOIN #DuplicateTable Duplicate ON Duplicate.DuplicateId = emp.id

--Again check your original table
SELECT * FROM emp


--INSERT Records in Original table from Temparory table
INSERT INTO emp (id,name)
(SELECT DuplicateId,DuplicateName FROM #DuplicateTable)

--Now you can check that the Original table contains only unique records.
SELECT * FROM emp


(OR)


SET ROWCOUNT 1
DELETE emp
FROM emp a
WHERE (SELECT COUNT(*) FROM emp b WHERE b.id =
a.id AND b.name = a.name ) > 1
WHILE @@rowcount > 0
  DELETE emp
  FROM emp a
  WHERE (SELECT COUNT(*) FROM emp b WHERE b.id =
a.id AND b.name = a.name) > 1

SET ROWCOUNT 0

Static Method in C#

Introduction
The methods in C# may or may not take parameters and they may or may not return a value. Also, a custom method that is also known as a user defined method may be declared non-static (instance level) or static (class level). When a method is static then it can be invoked directly from the class level without creating an object. This is the reason for making a main() function/method static. Another example is the WriteLine() method that is called/used without creating any object. Let's explore further using an example.
    class Program
   
 {
       
 public static void withoutObj()
       
 {
           
 Console.WriteLine("Hello");
       
 }
 
        static void Main()
       
 {
           
 Program. withoutObj();
           
 Console.ReadKey();
       
 }
   
 }
In the above example, I will be calling a method using the class name itself without an object being used for the WriteLine().
Using Static Method
Usually we define a set of data members for a class and then every object of that class will have a separate copy of each of those data members. Let's have an example.
    class Program
   
 {
       
 public int myVar;  //a non-static field
 
        static void Main()
       
 {
           
 Program p1 = new Program();  //a object of class
           
 p1.myVar = 10;
           
 Console.WriteLine(p1.myVar);
           
 Console.ReadKey();
       
 }
   
 }
In the above example, myVar is a non-static field so to use this field we first need to create the object of that class. On the other hand, static data is shared among all the objects of that class. That is, for all the objects of a class, there will be only one copy of static data. Let's have an example.
    class Program
   
 {
       
 public static int myVar;  //a static field
 
        static void Main()
       
 {
           
 //Program p1 = new Program();  //a object of class
           
 myVar = 10;
           
 Console.WriteLine(myVar);
           
 Console.ReadKey();
       
 }
   
 }
In the above we don't have an object of the class to use that field since the field is static.
Notable Points here are:

1. A static method can be invoked directly from the class level
2. A static method not requires any class object
3. Any main() method is shared through entire class scope so it always appears with static keyword.
Thanks for reading.

Coding of Log Out Button in asp.net C#


protected void lnkbtnlogout_Click(object sender, EventArgs e)
{
Session.Abandon();
Session.Clear();
Response.Redirect("~/Default.aspx");

}

Partial Classes in C#

Introduction
A Partial class is one that can be split among multiple physical files. This feature was introduced with the release of C# version 2.0. With C#, we can split the source code for a class into separate files so that we can organize the definition of a large class into smaller, easier to manage pieces. When we split a class across multiple files, we define the parts of the class by using the partial keyword in each file. Each file contains a section of the class definition, and all parts are combined when the application is compiled. Look at the example below:

Original and Single Class File (Calculation.cs)

class ClassRoom
{
   
 private int boycount;   //field
    public ClassRoom()     //default constructor
   
 {
       
 boycount = 30;
   
 }
    public ClassRoom(int bcount)     //overloaded constructor
   
 {
       
 boycount = bcount;
   
 }
    public double Avg()     //method
   
 {
       
 //statements goes here
   
 }
}

Splitted Class Files into two parts


//Calculation1.cs
partial class ClassRoom
{
   
 private int boycount;   //field

   
 public ClassRoom()     //default constructor
   
 {
       
 boycount = 30;
   
 }
}
//Calculation2.cs
partial class ClassRoom
{
   
 public ClassRoom(int bcount)     //overloaded constructor
   
 {
       
 boycount = bcount;
   
 }
    public double Avg()     //method
   
 {
       
 //statements goes here
   
 }
}
Now, when we compile a class that has been split into separate files, we must provide all the files to the compiler.
Partial Classes Rules and Advantages
To work on large projects, spreading a class over separate files allows multiple programmers to work on it simultaneously.
When working with automatically generated source, code can be added to the class without having to recreate the source file.
To split a class definition, use the partial keyword modifier.
All the parts must have the same accessibility, such as public, private, and so on.
The partial modifier can only appear immediately before the keywords class, struct, or interface.
Benefit of partial classes:

1) More than one developer can simultaneously write the code for the class.

2) You can easily write your code (for extended functionality) for a VS.NET generated class. This will allow you to write the code of your own need without messing with the system generated code.


21 October 2013

stars pyramid in c#.net


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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int number_of_layer = 7, Space, Number;
            Console.WriteLine("Print paramid");
            for (int i = 1; i <= number_of_layer; i++) // Total number of layer for pramid
            {
                for (Space = 1; Space <= (number_of_layer - i); Space++)  // Loop For Space
                    Console.Write(" ");
                for (Number = 1; Number <= i; Number++) //increase the value
                    Console.Write("*");
                for (Number = (i - 1); Number >= 1; Number--)  //decrease the value
                    Console.Write("*");
                Console.WriteLine();
              
            }
            Console.ReadLine();
        }
    }

}

Palindrome Program in c#.net

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = string.Empty;
            Console.WriteLine("Enter a String");

            string s = Console.ReadLine();

            int i = s.Length;
            //we can get the Length of string

            for (int j = i - 1; j >= 0; j--)
            {
                str = str + s[j];
            }
            if (str == s)
            {

                Console.WriteLine(s + " is palindrome");

            }
            else
            {
                Console.WriteLine(s + " is not a palindeome");
            }

            Console.WriteLine(str);

            Console.ReadLine();
        }
    }
}