Wednesday, April 27, 2011

Property in Tutorial in C sharp

Property is Smart Method.
below given property example.

Tuesday, April 26, 2011

Indexer in Csharp Example

The Indexer is also known as Smart Array.
Benefit with Indexer is that we not need to define Limit Value like Array.
Example Of Indexer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Indexer
{
class Tutorial
{
string [] name=new string[15];
public string this[int range]
{
set { name[range] = value; }
get { return name[range]; }
}
static void Main(string[] args)
{
Tutorial p = new Tutorial();
p[0] = "Mike";
p[1] = "Milan";
p[2] = "Vihar";
p[3] = "Vivek";
p[4] = "Sharad";
Console.Write(" {0} \n {1} \n {2} \n {3} \n {4}",p[0],p[1],p[2],p[3],p[4]);


Console.ReadLine();

}
}

Monday, April 25, 2011

Exception handling Divide By Zero Exception

DivideByZeroException Exception Handling
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Exception handling 
{
    class Program
    {
        
        
        static void Main(string[] args)
        {
            int i;
            int j;
            int k;
            Console.WriteLine("Enter 1st No: ");
            i = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter 2nd No: ");
            j = int.Parse(Console.ReadLine());
            try
            {
                if (j == 0)
                {
                    throw new DivideByZeroException();
                }
                k = i / j;
                Console.WriteLine("Ans is " + k);
            }
            catch (DivideByZeroException)
            {
                    Console.WriteLine("Divide By Zero Not Possible..");
            
            }

            finally
            {
                Console.ReadLine();
            }
        }
    }
}
 

Sunday, April 24, 2011

GridView Data Control in ASP.net Example

For ASP.net Beginner
ToolBox in that Data in that GridView Pick up and Drag that Control to ASPX page.
Which is Look Like This Image

Saturday, April 23, 2011

Exception Handling IndexOutOfRangeException

Exception handling for index outof range exception in c sharp.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ass_45
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] name = new int[3];
            Console.WriteLine("Enter Array Value:");
            try
            {
                for (int i = 0; i <= 3; i++)
                {

                    name[i] = Convert.ToInt32(Console.ReadLine());

                }
                for (int i = 0; i <= 3; i++)
                {
                    Console.WriteLine("Array" + name[i]);
                }
            }
            catch (IndexOutOfRangeException)
            {
                Console.WriteLine("Array Out of Index Ha Ha Ha");
            }
            finally
            {
                Console.ReadLine();
            }
        }
    }
}

Generics example in c sharp

Generics is belongs from System.Collections.Generic class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Generics
{
    public class Test
    {
        public T EmpID
        {
            set;
            get;
        }

        public void show(T t,N n)
        {
            if (typeof(T) == typeof(int) && typeof(N) == typeof(int))
            {
                int x = Convert.ToInt32(t);
                int y = Convert.ToInt32(n);
                Console.WriteLine(x + y);
            }
            else if (typeof(T) == typeof(string) && typeof(N) == typeof(int))
            {
                Console.WriteLine("The " + t + "'s Age is " + n);
            }

        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Test ob = new Test();
            ob.EmpID = 10;
            ob.show(10, 20);

            Test ob1 = new Test();                        
            ob1.show("Rajesh",20);
        }
    }
}

Thursday, April 21, 2011

Save Successfully confirm message using store procedure(sp) Input OutPut

Insert,Update,Delete Store Procedure(SP) execution Successfully done or any Error that Can using output variable in sp.
Here I have Given Example For Insert Sp in Three Tier Architecture.


Insert Store Procedure
ALTER PROCEDURE dbo.sp_InserttblEmp @Name varchar(50), @depid int, @Salary int, @status int output AS insert into tblemp VALUES(@Name,@depid,@Salary); select @status=@@ERROR RETURN @status
LogicLayer.cs
public Boolean insert_tblemp(LogicLayer objLogicLayer)
    {
        int status;
        string Sp = "sp_InserttblEmp";
        objCmd = new SqlCommand(Sp,objCn);
        objCmd.CommandType = CommandType.StoredProcedure;
        objCmd.Parameters.Add("@Name",objLogicLayer.Name);
        objCmd.Parameters.Add("@depid", objLogicLayer.Sid);
        objCmd.Parameters.Add("@Salary", objLogicLayer.Salary);
        objCmd.Parameters.Add("@status", SqlDbType.Int).Value=1;
        objCmd.Parameters["@status"].Direction = ParameterDirection.InputOutput;
        objCn.Open();
        objCmd.ExecuteNonQuery();
        status = (int)objCmd.Parameters["@status"].Value;
        objCn.Close();
        if (status == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
DataLayer.cs
public class public class LogicLayer
{
{
DataLayer objLogicLayer=new LogicLayer ();
public int Sid { set; get; }
public string Name { set; get; }
public int Salary { set; get; }
public Boolean insert_tblemp(DataLayer objDataLayer)
{
return objLogicLayer.insert_tblemp(objDataLayer);
}
}
InsertForm.ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="insert.aspx.cs" Inherits="insert" %>

<!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">
    
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
    <tr>
    <td>
    Name
    </td>
    <td>
        <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>
    depid
    </td>
    <td>
        <asp:TextBox ID="txtdepid" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>
    Salary
    </td>
    <td>
        <asp:TextBox ID="txtSalary" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>
   &nbsp;
        <asp:Label ID="lblMsg" runat="server" ForeColor="Red" Text="Label"></asp:Label>
    </td>
    <td>
        <asp:Button ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click" />
    </td>
    </tr>
    </table>
    </div>
    </form>
</body>
</html>
InsertForm.ASPX.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class insert : System.Web.UI.Page
{
    LogicLayer objLogicLayer = new LogicLayer();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        { 
        
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        bool status;
        objLogicLayer.Sid = Convert.ToInt32(txtdepid.Text);
        objLogicLayer.Name = txtName.Text;
        objLogicLayer.Salary = Convert.ToInt32(txtSalary.Text);
        status = objLogicLayer.insert_tblemp(objLogicLayer);
        if (status == true)
        {
            lblMsg.Text = "Save Successfully";
        }
        else
        {
            lblMsg.Text = "Error";
        }
    }
}

Thursday, April 14, 2011

Date Validation in Formate dd/mm/yyyy in asp.net

DateValidation.aspx


<asp:textbox id="txtdate" runat="server"></asp:textbox>


<asp:regularexpressionvalidator controltovalidate="txtdate" errormessage="RegularExpressionValidator" 
id="RegularExpressionValidator1" runat="server"
validationexpression=
"(0[1-9]|[1 2][0-9]|3[0 1]|)[-/.](0[1-9]|1[0 1 2])[-/.](19/20)\d\d ">
</asp:regularexpressionvalidator>

Tuesday, April 5, 2011

SQL Update Query With inner Join and case

tblEMp

EmpID

Name

Salary

DepID

1

Sachin

1000

1

2

Mahora

2000

2

3

Afridi

3000

1

tblDep

DepID

DepName

1

D1

2

D2
Now

Now Question is Update Salary with 10% where DepName is D1.and 20% where DepName is D2. Use Join and Single Query Only.
Ans:-  update tblemp set Salary = Case tbldep.lname
           when 'D1' then Salary +(Salary*0.10)
          when 'D2' then Salary +(Salary*0.20)
          end
           from tblemp join tbldep on tblemp.depid=tbldep.depid
ASPdotNET-Example