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