Tuesday, August 26, 2014

Example of Events in C Sharp

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

namespace EventsExample
{
    public delegate void EventEx();
    public class EventExample
    {
        public  event EventEx events;
        public void Display()
        {
            for (int i = 1; i <= 100; i++)
            {
                if (i%25==0 && events != null)
                {
                 
                    events();
                }
             
            }
         
        }
     
    }
    public class AnotherExample
    {
        public int Count
        { get; private set; }
        public AnotherExample(EventExample ev)
        {
            Count = 0;
            ev.events += eventexecute;
                   

        }
        public void eventexecute()
        {
            Count++;
        }
     

    }
    class Program
    {
        static void Main(string[] args)
        {
            EventExample ev = new EventExample();
            AnotherExample an = new AnotherExample(ev);
         
            ev.Display();
            Console.WriteLine("{0} Quarters in 100 ",an.Count);
        }
    }
}


Thursday, August 7, 2014

Practical Difference between Const & ReadOnly

Wednesday, August 6, 2014

To Find the Perfect Number


/*a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself.
Read here: en.wikipedia.org/wiki/Perfect_number
*/

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

namespace ConsoleApplication2
{
    class Program
    {
        static void PerfectNumber(int a)
        {


            int sum = 0;
            for (int idx = 1; idx < a; idx++)
            {
                int id = a % idx;
                if (id == 0)
                {
                    sum = sum + idx;
                }
              
            }
            Console.WriteLine();
            Console.WriteLine("The sum of all it's proper divisors is: "+sum+'\n');
            if (sum == a)
            {
                Console.WriteLine("YES!  "+a+ " is a Perfect number");

            }
            else
                Console.WriteLine("No!  " + a + " is  NOT a Perfect number");

        }

        static void Main(string[] args)
        {
            Console.WriteLine("********************************************************");
            Console.WriteLine("To Check the whether entered number is Perfect Number !");
            Console.WriteLine();
            Console.Write("Enter the a desired Number:");
            int abc = int.Parse(Console.ReadLine());
            PerfectNumber(abc);

        }
    }
}

Wednesday, July 16, 2014

Create User Account as desired using C sharp - Console Application

To create User Account as desired using C sharp Console Application

Here is a code


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Diagnostics;



namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Do You want to create a user ID in your system?");
            string st = Console.ReadLine();
            if (st == "yes")
            {
                Console.WriteLine("Enter the name of the user to be create:");
                string user = Console.ReadLine();
                Console.WriteLine("Enter the password:");
                string password = Console.ReadLine();
                string userdetails = "@/C" + " " + "net user" + " " + user + " " + password + " " + "/add";
                Process.Start("cmd.exe", userdetails);
                Console.WriteLine("User has been created succesfully");
                Process.Start("cmd.exe", @"/C pause");
            }
            else
                           
               Console.WriteLine("Thank for you not interested in to create user accounts");
                      
           
        }
    }
}

--- If you want to find out whether user account has been created

Press Windows + R
=> cmd
=> net user
Here you will find list of user accounts created

--Happy Coding--

To find whether system is connected to Domain and find the system name.. !

Hi,

If you would like to find the whether system is connected to work group or domain and find the its name in the C sharp.

Here it is the solution....























(1) Create a Windows forms Application...
(2) Create a button by renaming its text as : GetSystemName
(3) Double click on the button, we will have to code behind to it to execute

Code is as follows:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string domainName = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName;
            string hostName = Dns.GetHostName();
            string SysName = "";
            if (!hostName.Contains(domainName))
                SysName = hostName + "." + domainName;
             
            else
                SysName = hostName;
            MessageBox.Show("Your System is not connected to Domain!");
            MessageBox.Show("Your Computer Name is "+SysName);

        }

     
    }
}




When you click on the button it will displays as follows,


"Your system is not connected to domain"

and

"Your computer Name is WINSERVER03"




Thursday, June 5, 2014

To Check whether Given IP Address is 'Pinging'

-- To Check To Check whether Given IP Address is 'Pinging'


drop table #TempTable
declare @IP as varchar(15)
declare @cmd as varchar(300)
set @IP = '192.168.50.200'  -- fill the ip address here
set @cmd = 'ping ' + @IP
--Create table
create table #TempTable (output varchar(150) default(''))
insert into #TempTable
exec xp_cmdshell @cmd

select * from #TempTable where output is not null


Use of function 'LEFT' in SQL 2008 R2

-- Use of LEFT function

select LEFT('WindowXP',1as LEFTOUT

-- In above function LEFT which removes the characters in a string variable from the starting point mentioned next to the string variable such as 1

Results is


How to find the IP Address of the client Machine in SQL Server 2008 R2


-- Check whether particular table exists in the database
if exists (select * from sys.objects where object_id = object_id(N'[#table]'))
begin
drop table #table
end
use tempdb
go
--Create the temp table #table
create table #table (IPValues varchar(200))
insert into #table exec master..xp_cmdshell 'ipconfig'
-- select the data from the table #table
select * from #table where IPValues is not null
-- You will get results like this



Thursday, May 8, 2014

How to find whether specific column exists in the given table.. in SQL 2008

-- Query to check the specific column is exists in the table in SQL 2008

if exists (select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='<Table_Name>' and COLUMN_NAME='<Column_Name>')
begin
print 'Column you have specified is exists'
end
else
begin
print 'Column does not exists'
end

Thursday, May 1, 2014

Difference between cast and convert in MS SQL



Difference between cast and convert


Syntax of cast and convert is as follows,

(    (1)   Select cast(<varchar-value> as int) from Table
(    (2)   Select Convert(int, <varchar-value>) from Table

There are two types of conversion of data format from one format to another, they are implicit and explicit,
Implicit way of conversion includes the server automatically converts the data without requiring the user to input any external data or coding.
Explicit way of conversion is where server accepts user input to covert the data to his desired format.


use temp1
create table dbo.custr (cuid int, custaddress varchar(50))
go
insert into dbo.custr values (1, '560056')


select CAST(custaddress as int)as value from dbo.custr

select CONVERT(int,custaddress) as value from dbo.custr



In above example column custaddress is having datatype of varchar which is of length of 50, we extracted the data in the format of int by using cast and covnert functions.

Datetime and Datetime2 in MS Sql Server 2008 R2



Datetime and Datetime2 in MS Sql Server 2008 R2
---------------------------------------------------------------------- 


Datetime2 is a datatype in MS SQL server 2008 to store the date and time values. Datetime is not ansi complaint


DateTime
DateTime2
Min. Value
1753-01-01 00:00:00
0001-00-00 00:00:00
Max Value
9999-12-31 23:59:59.997
9999-12-31 23:59:59.9999999
Storage Size
8 bits
6 to 8 bits
Declaration Method
Declare @now datetime
Declare @now datetime2(7)

(1)    To find the current system date by using datetime datatype:

Declare @now datetime
@now = getdate()

(2)    To find the current system date by using datetime2 datatype:

Declare @now datetime2(7)
@now = sysdatetime()
(3)    To find the length of the data type


declare @now datetime2(2)
set @now=sysdatetime()
select DATALENGTH(@now)


Use of Set ANSI_NULL ON / OFF in SQL Server



Use of Set ANSI_NULL ON / OFF in SQL Server


Hint: It is used while creating user defined functions, stored procedures and etc

Let we create table with name of customers,
CODE
Create table dbo.customer (CustId varchar(5), CustFirstName varchar(25), CustLastName varchar(25))
Go
Insert into dbo.customer (CustId, CustFirstName, CustLastName) values ('CU001', 'SURESH', 'BABU'), ('CU002', 'RAVI', 'KUMAR'), ('CU003', 'AVINASH', NULL), ('CU004', 'MAHESH', NULL)

Now run below SQL Query to extract the values in the table of dbo.customer
set ansi_nulls on
select * from dbo.customer where CustLastName = null

It will yields “0” results like
CustId
CustFirstName
CustLastName

And in the Message column it shows
(0 row(s) affected)

Again you run with another code but little modification
set ansi_nulls off
select * from dbo.customer where CustLastName = null

it will yields you following results,
CustId
CustFirstName
CustLastName
CU003
AVINASH
NULL
CU004
MAHESH
NULL

And in the message column,

(2 row(s) affected)
Hint: If it is ansi_nulls on
We need to use following code to extract records with null values
set ansi_nulls on
select * from dbo.customer where CustLastName is null

or

set ansi_nulls on
select * from dbo.customer where CustLastName is not null


Summary: When this setting value is ON (i.e. SET ANSI_NULLS ON) then comparison with NULL value using = and <> comparison operator will return false.