Tuesday, September 2, 2014

Access Specifier and get set accessor.

using System;

namespace Example1
{
    class Program
    {
        private void add()
        {
            int num1, num2, result;
            Console.Write("Enter a number:\t");
            num1 = Convert.ToInt32(Console.ReadLine());

            Console.Write("\nEnter second number:\t");
            num2 = Convert.ToInt32(Console.ReadLine());

            result = num1 + num2;
            Console.WriteLine("{0} + {1} = {2}", num1, num2, result);
        }
        static void Main(string[] args)
        {
            Program p = new Program();
            p.add(); //It is valid, because private add() is           in same class
            Console.ReadLine();
        }
    }
}


using System;

namespace Example2
{
    class input
    {
        private static int num1, num2, result;
        public void add()
        {
            result = num1 + num2;
            Console.WriteLine("\n\nAdd = {0}", result);
            Console.ReadLine();
        }

        // Creating property for storing value in num1
        public int Number1
        {
            get
            {
                return num1;
            }
            set
            {
                num1 = value;
            }
        }

        // Creating property for storing value in num2
        public int Number2
        {
            get
            {
                return num2;
            }
            set
            {
                num2 = value;
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            input inp = new input();
            Console.Write("Enter number 1st:\t");
            inp.Number1 = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter number 2nd:\t");
            inp.Number2 = Convert.ToInt32(Console.ReadLine());

            inp.add();
        }
    }

}

C# Programming Examples of Variables and Data types




using System;
 
namespace Examples
{
  class Program
   {
     static void Main(string[] args)
      {
        string name;
        string city;
        sbyte age;
        int pin;
 
        // \n is used for line-break
        Console.WriteLine("Enter your name\n");
        name = Console.ReadLine();
 
        Console.WriteLine("Enter Your City\n");
        city = Console.ReadLine();
 
        Console.WriteLine("Enter your age\n");
        age = sbyte.Parse(Console.ReadLine());
 
        Console.WriteLine("Enter your pin\n");
        pin = Int32.Parse(Console.ReadLine());
 
        // Printing message to console
        //formatting output
        Console.WriteLine("=============="); 
        Console.WriteLine("Your Complete Address:");
        Console.WriteLine("============\n");
 
        Console.WriteLine("Name = {0}", name);
        Console.WriteLine("City = {0}", city);
        Console.WriteLine("Age = {0}", age);
        Console.WriteLine("Pin = {0}", pin);
        
Console.WriteLine("===============");
 
        Console.ReadLine();
      }
   }
}
O/P:

Enter your Name :Naresh
Enter Your City :Wgl
Enter Your age  :23
Enter Your Pin  :506164

=============================
Your Complete Address:
=============================
your Name :Naresh
Your City :Wgl
Your age  :23
Your Pin  :506164

How to debug or execute C# program?

Step 1: Open Notepad and write the following c# code carefully.


using System;
 
namespace Chapter1
{
  class Program
   {
     static void Main(string[] args)
      {
        Console.WriteLine("This is my First C# Program");
      }
   }
}


Step 2: Now save this program as Chapter1.cs. You must choose All Files in save as type box.
Step 3: Go to Start > All Programs > Microsoft Visual Studio 2005 or 2008 > Visual Studio Tools > Visual Studio 2005 or 2008 Command Prompt.
Step 4: Set Path in command prompt where your Chapter1.cs file is saved. Here we are using D:/Csharp>
Step 5: Now compile your program by following command:
  csc Chapter1.cs


You will see some information like compiler version number and framework information. There is no error, so you won’t get any error.
Step 6: Now, it’s turn to C sharp program. Just write your file name without extension and hit enter.
  Chapter1
Step 7: You will get the This is my First C# Program as output.

Output 

How to debug or execute C# program?

Step 1: Open Notepad and write the following c# code carefully.


using System;
 
namespace Chapter1
{
  class Program
   {
     static void Main(string[] args)
      {
        Console.WriteLine("This is my First C# Program");
      }
   }
}


Step 2: Now save this program as Chapter1.cs. You must choose All Files in save as type box.
Step 3: Go to Start > All Programs > Microsoft Visual Studio 2005 or 2008 > Visual Studio Tools > Visual Studio 2005 or 2008 Command Prompt.
Step 4: Set Path in command prompt where your Chapter1.cs file is saved. Here we are using D:/Csharp>
Step 5: Now compile your program by following command:
  csc Chapter1.cs


You will see some information like compiler version number and framework information. There is no error, so you won’t get any error.
Step 6: Now, it’s turn to C sharp program. Just write your file name without extension and hit enter.
  Chapter1
Step 7: You will get the This is my First C# Program as output.

Output 

program to print following output using for loop.

using System;
namespace Example2
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, j;
            i = 0;
            j = 0;

            for (i = 1; i <= 5; i++)
            {
                for (j = 1; j <= i; j++)
                {
                    Console.Write(i);
                }
                Console.Write("\n");
            }
            Console.ReadLine();
        }
    }
}


O/P:

1
22
333
4444
55555

program to display table of given number.


using System;

namespace Examples1
{
    class Program
    {
        static void Main(string[] args)
        {
            int num, i, result;
            Console.Write("Enter a number\t");
            num = Convert.ToInt32(Console.ReadLine());

            for (i = 1; i <= 10; i++)
            {
                result = num * i;
                Console.WriteLine("{0} x {1} = {2}", num, i, result);
            }
            Console.ReadLine();
        }
    }
}

 O/P: 8 *1 =8
          - - - -
 --- -
 --- 
8 * 10=80

Wednesday, August 27, 2014

File Upload

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <center>
            <fieldset style="width: 25%">
                <legend>Uplaod and Preview the Image</legend>
                <table>
                    <tr>
                        <td>
                            Upload Image:
                        </td>
                        <td>
                            <asp:FileUpload ID="FileUpload1" runat="server" /><br />
                            <asp:Label ID="lblmessage" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td>
                        </td>
                        <td>
                            <asp:Button ID="btnupload" runat="server" Text="Upload Image" OnClick="btnupload_Click" />
                        </td>
                    </tr>
                </table>
                <asp:Image ID="imgpreview" runat="server" Width="350px" Height="300px" />
            </fieldset>
        </center>
    </div>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnupload_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            string path = Server.MapPath("~/images/") + FileUpload1.FileName;
            FileUpload1.SaveAs(path);
            imgpreview.ImageUrl = "~/images/" + FileUpload1.FileName;


        }
        else
        {
            lblmessage.Text = "Upload images";
            imgpreview.ImageUrl = "";

        }
    }
}