31 July 2011

Reverse of a string or number in console apps


class Reverseofno
    {
        public static void Main()
        {
            Console.WriteLine("enter a no/string: ");
            string a = Console.ReadLine();
            for (int x = a.Length - 1; x >= 0; x--)
            {
                Console.Write(a[x]);
            }

            Console.WriteLine();
           
            string bb = string.Empty;
            Console.WriteLine("enter a no/string: ");
            string b = Console.ReadLine();
            for (int x = b.Length - 1; x >= 0; x--)
            {
                bb += b[x].ToString();
            }
            string[] bbb = new string[bb.Split(' ').Length];
            for (int y = 0; y <= bb.Split(' ').Length-1; y++)
            {
                bbb[y] = bb.Split(' ')[y]+" ";
            }
            for (int z = bbb.Length - 1; z >= 0; z--)
            {
                Console.Write(bbb[z]);
            }
           
            Console.WriteLine();
           
            Console.WriteLine("enter a name: ");
            string n= Console.ReadLine();
            if(bbb.Contains(n+" "))
                Console.WriteLine("found");
            else
                Console.WriteLine("not found");
            Console.ReadLine();
        }
    }

Interface overriding in console apps


interface Interface
{
      void add(int x, int y);
}
class overrideinterface : Interface
{
      public void add(int a, int b)
      {
            Console.WriteLine("add is: " + (a + b));
      }
      public static void Main()
      {
            overrideinterface ovri = new overrideinterface();
            ovri.add(5, 5);
            Console.ReadLine();
      }
}

Note: A class can inherit any no.of interfaces separated by comma(,)

Virtual class overriding


class Virtual
{
        public virtual void add(int a, int b)
        {
            Console.WriteLine(a + b);
        }
}
class overridevirtual:Virtual
{
        public override void add(int a, int b)
        {
            Console.WriteLine(a);
        }
        public static void Main()
        {
            Virtual vir = new Virtual();
            vir.add(1,2);
            Console.ReadLine();
        }
}

Abstract Overriding in console apps


abstract class Abstract
{
      public abstract void mul(int x);
      public void add(int x, int y)
      {
          Console.WriteLine("add is: "+ (x + y));
      }
}
class overrideabstract : Abstract
{
      public override void mul(int a)
      {
          Console.WriteLine("mul is: "+ a*a);
      }
      public static void Main()
      {
          overrideabstract ovra = new overrideabstract();
          ovra.add(4, 5);
          ovra.mul(3);
          Console.ReadLine();
      }
}

Prime No. in console apps


class Prime
    {
        static void Main(string[] args)
        {
            int j = 0;
            int count = 0;
            Console.WriteLine("Plz enter a number: ");
            int n =int.Parse( Console.ReadLine());

            Console.WriteLine("The Prime no's are:");
            for (int i = 2; i <= n; i++)
            {
                for (j = 2; j <= i; j++)
                {
                    if (i % j == 0)
                    {
                        break;
                    }
                }
                if (i == j)
                {
                    Console.WriteLine(i);
                    count++;
                }
            }
            Console.WriteLine();
            Console.Write("Total Prime No's: "+count);
            Console.ReadLine();
        }

Constructor OverLoading in Console apps

class CACOverLoading
{
        int x;
        public CACOverLoading()
        {
            Console.WriteLine("Default constructor");
            x = 123;
        }
        public CACOverLoading(int x)
        {
            Console.WriteLine("Parameterized constructor");
            Console.WriteLine(x);
            this.x = 789;
        }
        public void display()
        {
            Console.WriteLine(x);
        }
        public static void Main()
        {
            CACOverLoading col = new CACOverLoading();
            col.display();
            CACOverLoading col1 = new CACOverLoading(456);
            col1.display();
            Console.ReadLine();
        }
}

Method OverLoading in console apps


class mOverLoading
    {
        int x=10;
        public void show()
        {
            Console.WriteLine("default constructor");
            Console.WriteLine(x + this.x);
        }
        public void show(int x)
        {
            Console.WriteLine("Parameterized constructors");
            Console.WriteLine(x+" "+this.x);
        }
        public void show(string s, int x)
        {
            Console.WriteLine("Parameterized constructor");
            Console.WriteLine(s + " " + x);
        }
        public static void Main()
        {
            mOverLoading mol = new mOverLoading();
            mol.show();
            mol.show(123);
            mol.show("satya", 456);
            Console.ReadLine();
        }
    }

Operator OverLoading in console apps


class ClsComplex
{
        int R, I;
        public ClsComplex(int R, int I)
        {
            this.R = R;
            this.I = I;
            Console.WriteLine("{0} + {1}i", R, I);
        }
        public static ClsComplex operator +(ClsComplex Num1, ClsComplex Num2)
        {
            ClsComplex Num3 = new ClsComplex(Num1.R + Num2.R, Num1.I + Num2.I);
            return Num3;
        }
}
class ClsOpOverLoad
{
        static void Main(string[] args)
        {
            Console.Write("C1= ");
            ClsComplex C1 = new ClsComplex(4, 5);
            Console.Write("C2= ");
            ClsComplex C2 = new ClsComplex(8, 2);
            Console.Write("C3= ");
            ClsComplex C3 = C1 + C2;
            Console.Read();
        }
}

30 July 2011

Reverse of a string or number in console apps


class Reverseofno
    {
        public static void Main()
        {
            Console.WriteLine("enter a no/string: ");
            string a = Console.ReadLine();
            for (int x = a.Length - 1; x >= 0; x--)
            {
                Console.Write(a[x]);
            }

            Console.WriteLine();
           
            string bb = string.Empty;
            Console.WriteLine("enter a no/string: ");
            string b = Console.ReadLine();
            for (int x = b.Length - 1; x >= 0; x--)
            {
                bb += b[x].ToString();
            }
            string[] bbb = new string[bb.Split(' ').Length];
            for (int y = 0; y <= bb.Split(' ').Length-1; y++)
            {
                bbb[y] = bb.Split(' ')[y]+" ";
            }
            for (int z = bbb.Length - 1; z >= 0; z--)
            {
                Console.Write(bbb[z]);
            }
           
            Console.WriteLine();
           
            Console.WriteLine("enter a name: ");
            string n= Console.ReadLine();
            if(bbb.Contains(n+" "))
                Console.WriteLine("found");
            else
                Console.WriteLine("not found");
            Console.ReadLine();
        }
    }

Interface overriding in console apps


interface Interface
    {
        void add(int x, int y);
    }
    class overrideinterface : Interface
    {
        public void add(int a, int b)
        {
            Console.WriteLine("add is: " + (a + b));
        }
        public static void Main()
        {
            overrideinterface ovri = new overrideinterface();
            ovri.add(5, 5);
            Console.ReadLine();
        }
    }

Abstract Overriding in console apps


abstract class Abstract
    {
        public abstract void mul(int x);
        public void add(int x, int y)
        {
            Console.WriteLine("add is: "+(x + y));
        }
    }
    class overrideabstract : Abstract
    {
        public override void mul(int a)
        {
            Console.WriteLine("mul is: "+a*a);
        }
        public static void Main()
        {
            overrideabstract ovra = new overrideabstract();
            ovra.add(4, 5);
            ovra.mul(3);
            Console.ReadLine();
        }
    }

Virtual class overriding


class Virtual
    {
        public virtual void add(int a, int b)
        {
            Console.WriteLine(a + b);
        }
    }
    class overridevirtual:Virtual
    {
        public override void add(int a, int b)
        {
            Console.WriteLine(a);
        }
        public static void Main()
        {
            Virtual vir = new Virtual();
            vir.add(1,2);
            Console.ReadLine();
        }
    }

Prime No. in console apps


class Prime
    {
        static void Main(string[] args)
        {
            int j = 0;
            int count = 0;
            Console.WriteLine("Plz enter a number: ");
            int n =int.Parse( Console.ReadLine());

            Console.WriteLine("The Prime no's are:");
            for (int i = 2; i <= n; i++)
            {
                for (j = 2; j <= i; j++)
                {
                    if (i % j == 0)
                    {
                        break;
                    }
                }
                if (i == j)
                {
                    Console.WriteLine(i);
                    count++;
                }
            }
            Console.WriteLine();
            Console.Write("Total Prime No's: "+count);
            Console.ReadLine();
        }

Constructor OverLoading in Console apps


class CACOverLoading
    {
        int x;
        public CACOverLoading()
        {
            Console.WriteLine("Default constructor");
            x = 123;
        }
        public CACOverLoading(int x)
        {
            Console.WriteLine("Parameterized constructor");
            Console.WriteLine(x);
            this.x = 789;
        }
        public void display()
        {
            Console.WriteLine(x);
        }
        public static void Main()
        {
            CACOverLoading col = new CACOverLoading();
            col.display();
            CACOverLoading col1 = new CACOverLoading(456);
            col1.display();
            Console.ReadLine();
        }
    }

Method OverLoading in console apps


class mOverLoading
    {
        int x=10;
        public void show()
        {
            Console.WriteLine("default constructor");
            Console.WriteLine(x + this.x);
        }
        public void show(int x)
        {
            Console.WriteLine("Parameterized constructors");
            Console.WriteLine(x+" "+this.x);
        }
        public void show(string s, int x)
        {
            Console.WriteLine("Parameterized constructor");
            Console.WriteLine(s + " " + x);
        }
        public static void Main()
        {
            mOverLoading mol = new mOverLoading();
            mol.show();
            mol.show(123);
            mol.show("satya", 456);
            Console.ReadLine();
        }
    }

Operator OverLoading in console apps


class ClsComplex
    {
        int R, I;
        public ClsComplex(int R, int I)
        {
            this.R = R;
            this.I = I;
            Console.WriteLine("{0} + {1}i", R, I);
        }
        public static ClsComplex operator +(ClsComplex Num1, ClsComplex Num2)
        {
            ClsComplex Num3 = new ClsComplex(Num1.R + Num2.R, Num1.I + Num2.I);
            return Num3;
        }
    }
    class ClsOpOverLoad
    {
        static void Main(string[] args)
        {
            Console.Write("C1= ");
            ClsComplex C1 = new ClsComplex(4, 5);
            Console.Write("C2= ");
            ClsComplex C2 = new ClsComplex(8, 2);
            Console.Write("C3= ");
            ClsComplex C3 = C1 + C2;
            Console.Read();
        }
    }

28 July 2011

how to find out highest salaried employee details of a table in sqlserver



select * from tblEmp tbl1 where 0=(select count(distinct salary) from tblEmp tbl2 where tbl1.salary<tbl2.salary)

with s as (select *, row_number() over(order by sal desc) as rn from sam) select * from s where s.rn=1

max possible columns per a table in sqlserver is 1024

copy one column data into other column of a same table using query in sqlserver

update tblEmp set address1=address2

get the list of tables that r available in a particular database in sqlserver

select * from information_schema.tables

or

select * from sys.tables

get the list of columns that r available in a particular table in sqlserver

select column_name from information_schema.columns where table_name='tblEmp'

or

select name from sys.columns where object_id=object_id('tblEmp')

get the serial no. for records of table in sqlserver

select empname, row_number() over(order by empid)as 's.no' from tblEmp

27 July 2011

how to find out rank of employees based on their salaries

select empname,dense_rank()over(order by salary)as rank from tblEmp

how to find servername from sqlserver name using query

select host_name()


For app name:

select app_name()

how to find out highest salaried employee details of a table in sqlserver

select * from tblEmp tbl1 where 0=(select count(distinct salary) from tblEmp tbl2 where tbl1.salary<tbl2.salary)



max possible columns per a table in sqlserver is 1024 

copy one column data into other column of a same table using query in sqlserver

update tblEmp set address1=address2

get the list of tables that r available in a particular database in sqlserver

select * from information_schema.tables

or

select * from sys.tables

get the list of columns that r available in a particular table in sqlserver

select column_name from information_schema.columns where table_name='tblEmp'

or

select name from sys.columns where object_id=object_id('tblEmp')

get the serial no. for records of table in sqlserver

select empname, row_number() over(order by empid)as 's.no' from tblEmp

how to find out rank of employees based on their salaries

select empname,dense_rank()over(order by salary)as rank from tblEmp

how to find servername from sqlserver name using query

select host_name()


For app name:

select app_name()

26 July 2011

palindrome in console applications


string str = string.Empty;
            Console.WriteLine("Enter a String");
            string s = Console.ReadLine();
            int i = s.Length;
            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 palindrome");
            }

factorial of number in console applications


            int count = 1;
            Console.Write("Enter Number for factorial: ");
            int number = Convert.ToInt32(Console.ReadLine());
            for (int i = 1; i <= number; i++)
            {
                count = count * i;
            }
            Console.Write(count);
            Console.Read();

or
          public static long numfact(long n)
          {
           if (n <= 1)
               return 1;
           else
             return n * numfact(n - 1);
           }
           Console.Write(numfact(5));

palindrome in console applications


string str = string.Empty;
            Console.WriteLine("Enter a String");
            string s = Console.ReadLine();
            int i = s.Length;
            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 palindrome");
            }

factorial of number in console applications


            int count = 1;
            Console.Write("Enter Number for factorial: ");
            int number = Convert.ToInt32(Console.ReadLine());
            for (int i = 1; i <= number; i++)
            {
                count = count * i;
            }
            Console.Write(count);
            Console.Read();

or
          public static long numfact(long n)
          {
           if (n <= 1)
               return 1;
           else
             return n * numfact(n - 1);
           }
           Console.Write(numfact(5));

21 July 2011

Some Coding Standards in .net

1.    Use Meaningful, descriptive words to name variables. Do not use abbreviations.
    Good:
    string address
    int salary
     Not Good:
           string name
       string addr
      int sal

2. Do not use single character variable names like i, n, s etc. Use names like index, temp
     variable 'i' is used for for iterations in loops
        Ex:  for ( int i = 0; i < count; i++ ){....}

3.  Do not use underscores (_) for local variable names. But member variables must be prefixed with underscore (_)

4.  Prefix boolean variables, properties and methods with “is” or similar prefixes.
      Ex: private bool _isFinished

5.  File name should match with class name.
       Ex:  for the class HelloWorld, the file name should be helloworld.cs (or, helloworld.vb) 

6.  Keep private member variables, properties and methods in the top of the file and public members in the bottom.  

7. Use String.Empty instead of “”
      Ex: txtMobile= String.Empty;

8.   8. Avoid having very large files. If a single file has more than 1000 lines of code, it is a good candidate for refactoring. Split them logically into two or more classes.

9. Use StringBuilder class instead of String when you have to manipulate string objects in a loop. The String object works in weird way in .NET. Each time you append a string, it is actually discarding the old string object and recreating a new object, which is a relatively expensive operations.

20 July 2011

Some Coding Standards in .net

1.    Use Meaningful, descriptive words to name variables. Do not use abbreviations.
    Good:
    string address
    int salary
     Not Good:
           string name
       string addr
      int sal

2. Do not use single character variable names like i, n, s etc. Use names like index, temp
     variable 'i' is used for for iterations in loops
        Ex:  for ( int i = 0; i < count; i++ ){....}

3.  Do not use underscores (_) for local variable names. But member variables must be prefixed with underscore (_)

4.  Prefix boolean variables, properties and methods with “is” or similar prefixes.
      Ex: private bool _isFinished

5.  File name should match with class name.
       Ex:  for the class HelloWorld, the file name should be helloworld.cs (or, helloworld.vb) 

6.  Keep private member variables, properties and methods in the top of the file and public members in the bottom.  

7. Use String.Empty instead of “”
      Ex: txtMobile= String.Empty;

8.   8. Avoid having very large files. If a single file has more than 1000 lines of code, it is a good candidate for refactoring. Split them logically into two or more classes.

9. Use StringBuilder class instead of String when you have to manipulate string objects in a loop. The String object works in weird way in .NET. Each time you append a string, it is actually discarding the old string object and recreating a new object, which is a relatively expensive operations.

Naming conventions in .net

Pascal case
The first letter in the identifier and the first letter of each subsequent concatenated word are capitalized.
Example:
BackColor, DataSet
Camel case
The first letter of an identifier is lowercase and the first letter of each subsequent concatenated word is capitalized.
Example:
numberOfDays, isValid
Uppercase
All letters in the identifier are capitalized.
Example:
ID, PI
1).  Private Variables: _strFirstName, _dsetEmployees
2).  Local Variables: strFirstName, dsetEmployees
3).  Namespace:(Pascal) System.Web.UI, System.Windows.Forms
4).  Class Naming:(Pascal) FileStream, Button
5).  Interface Naming:(Pascal) IServiceProvider, IFormatable
6).  Parameter Naming:(Camel) pTypeName, pNumberOfItems
7).  Method Naming:(Pascal) RemoveAll(), GetCharAt()
7).  Method Parameters:(Camel) void SayHello(string name)....
8).  Property / Enumerations Naming:(Pascal) BackColor, NumberOfItems
9).  Event Naming:(Pascal) public delegate void MouseEventHandler(object sender, MouseEventArgs e);
10).  Exception Naming: catch (Exception ex){ }
11).  Constant Naming: AP_WIN, CONST

19 July 2011

Naming conventions in .net

Pascal case
The first letter in the identifier and the first letter of each subsequent concatenated word are capitalized.
Example:
BackColor, DataSet
Camel case
The first letter of an identifier is lowercase and the first letter of each subsequent concatenated word is capitalized.
Example:
numberOfDays, isValid
Uppercase
All letters in the identifier are capitalized.
Example:
ID, PI
1).  Private Variables: _strFirstName, _dsetEmployees
2).  Local Variables: strFirstName, dsetEmployees
3).  Namespace:(Pascal) System.Web.UI, System.Windows.Forms
4).  Class Naming:(Pascal) FileStream, Button
5).  Interface Naming:(Pascal) IServiceProvider, IFormatable
6).  Parameter Naming:(Camel) pTypeName, pNumberOfItems
7).  Method Naming:(Pascal) RemoveAll(), GetCharAt()
7).  Method Parameters:(Camel) void SayHello(string name)....
8).  Property / Enumerations Naming:(Pascal) BackColor, NumberOfItems
9).  Event Naming:(Pascal) public delegate void MouseEventHandler(object sender, MouseEventArgs e);
10).  Exception Naming: catch (Exception ex){ }
11).  Constant Naming: AP_WIN, CONST

keyboard key ascii values


<script type="text/javascript">
document.onkeyup = KeyCheck;

function KeyCheck()
{
var KeyID = event.keyCode;
switch(KeyID)
{
case 16:
document.Form1.KeyName.value = "Shift";
break;
case 17:
document.Form1.KeyName.value = "Ctrl";
break;
case 18:
document.Form1.KeyName.value = "Alt";
break;
case 19:
document.Form1.KeyName.value = "Pause";
break;
case 37:
document.Form1.KeyName.value = "Arrow Left";
break;
case 38:
document.Form1.KeyName.value = "Arrow Up";
break;
case 39:
document.Form1.KeyName.value = "Arrow Right";
break;
case 40:
document.Form1.KeyName.value = "Arrow Down";
break;
}
}

how to make nvarchar id auto incerement (1) function in sqlserver

create function idincr() returns nvarchar(50)
as begin
declare @id int, @cid nvarchar(50), @did int
set @id=(select count(*) from tblcust)
if(@id>0)
begin
set @did= (select max(cast(substring(custid,2,len(custid))as int))from tblcust)+1;
set @cid='A'+cast(@did as nvarchar(50))
end
else 
set @cid='A1'
return @cid
end

Go

select dbo.idincr()

Go

how to change the database name in sqlserver using query

ALTER DATABASE databasename MODIFY NAME = newdatabasename

how to Insert data of one database table to another database table

INSERT INTO Database2..Table1 SELECT * FROM Database1..Table1

How to restore and backup database in sqlserver using query

For BackUp:
backup database mydb to disk = 'c:\mydb.bak'


For Restore:
restore database mydb from disk = 'c:\mydb.bak'

keyboard key ascii values


<script type="text/javascript">
document.onkeyup = KeyCheck;

function KeyCheck()
{
   var KeyID = event.keyCode;
   switch(KeyID)
   {
      case 16:
      document.Form1.KeyName.value = "Shift";
      break; 
      case 17:
      document.Form1.KeyName.value = "Ctrl";
      break;
      case 18:
      document.Form1.KeyName.value = "Alt";
      break;
      case 19:
      document.Form1.KeyName.value = "Pause";
      break;
      case 37:
      document.Form1.KeyName.value = "Arrow Left";
      break;
      case 38:
      document.Form1.KeyName.value = "Arrow Up";
      break;
      case 39:
      document.Form1.KeyName.value = "Arrow Right";
      break;
      case 40:
      document.Form1.KeyName.value = "Arrow Down";
      break;
   }
}</script>

how to make nvarchar id auto incerement (1) function in sqlserver

create function idincr() returns nvarchar(50)
as begin
declare @id int, @cid nvarchar(50), @did int
set @id=(select count(*) from tblcust)
if(@id>0)
begin
set @did= (select max(cast(substring(custid,2,len(custid))as int))from tblcust)+1;
set @cid='A'+cast(@did as nvarchar(50))
end
else 
set @cid='A1'
return @cid
end

Go

select dbo.idincr()

Go