Search This Blog

Tuesday, November 8, 2011

What is Difference between Master Page and Child Page???

Master Page 
                    :- Master Page is the Page of the website which contains the core layout of all the pages of Your Website which follows that master Page.

@master directive is there in the master page.

It contains the part of page which are common for some of the pages in the website.

Example Like Menu of the website which will be displayed in some of the pages of the website We can put that menu in the master page and follow all the pages which requires to display that menu.

Child Page :-
                    The Child Page will contains the core layout of the master page and also the content place holder on which we can make or display content of this page on the selected area of content place holder.

@page Directive is contain by the child page.



Saturday, November 5, 2011

Grid View .aspx.cs Page


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

namespace demo_databaseconnection
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        SqlConnection con1 = new SqlConnection("Data Source=TOPS17;Initial Catalog=TestGridview;Integrated Security=True");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                fill_grid();
            }
           
        }
        void fill_grid()
        {
            SqlDataAdapter da = new SqlDataAdapter("select * from tbl_reg",con1);
            DataSet ds = new DataSet();
            da.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }

        protected void btn_insert_Click(object sender, EventArgs e)
        {
            SqlCommand cmd1 = new SqlCommand("insert into tbl_reg values(@user,@pass)",con1);
            cmd1.Parameters.AddWithValue("@user",txtusername.Text);
            cmd1.Parameters.AddWithValue("@pass",txtpassword.Text);
            con1.Open();
            cmd1.ExecuteNonQuery();
            con1.Close();
            lblmessage.Text = "Record inserted successfully !!!";
            txtusername.Text = "";
            txtpassword.Text = "";
            fill_grid();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            fill_grid();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            fill_grid();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id1=int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
            SqlCommand cmd1 = new SqlCommand("delete from tbl_reg where id = @id1",con1);
            cmd1.Parameters.AddWithValue("@id1",id1);          
            con1.Open();
            cmd1.ExecuteNonQuery();
            con1.Close();
            fill_grid();
            lblmessage.Text = "The record successfully deleted from Database.";
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox t_username = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_username");
            TextBox t_password = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_password");
            int id1 = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
            SqlCommand cmd1 = new SqlCommand("update tbl_reg set                                  username=@username,password=@password where id=@id1",con1);
            cmd1.Parameters.AddWithValue("@username",t_username.Text);
            cmd1.Parameters.AddWithValue("@password",t_password.Text);
            cmd1.Parameters.AddWithValue("@id1",id1);
            con1.Open();
            cmd1.ExecuteNonQuery();
            con1.Close();
            GridView1.EditIndex = -1;
            fill_grid();
            lblmessage.Text = "The record successfully Updated in the Database.";

        }

        protected void insert_Click(object sender, EventArgs e)
        {
            TextBox t_user = (TextBox)GridView1.HeaderRow.FindControl("txt_user");
            TextBox t_pass = (TextBox)GridView1.HeaderRow.FindControl("txt_pass");
            SqlCommand cmd1 = new SqlCommand("insert into tbl_reg values(@username,@password)",con1);
            cmd1.Parameters.AddWithValue("@username",t_user.Text);
            cmd1.Parameters.AddWithValue("@password",t_pass.Text);
            con1.Open();
            cmd1.ExecuteNonQuery();
            con1.Close();
            fill_grid();
            lblmessage.Text = "Record inserted successfully";
        }

        protected void update_Click(object sender, EventArgs e)
        {
            TextBox t_username = (TextBox)GridView1.FooterRow.FindControl("t_user");
            TextBox t_password = (TextBox)GridView1.FooterRow.FindControl("t_pass");
            SqlCommand cmd1 = new SqlCommand("update tbl_reg set username=@username,password=@password where id=@id1");
           
        }
    }
}

What is Namespace in asp.net ?

Namespace :-


                          The Namespace is the container of all the classes in c# language same as package in java.


Namespaces are helps us to organize our code in the systematic manner. A namespace is a logical naming scheme for types in which a simple type name, such as MyType, is preceded with a dot-separated hierarchical name.


System is the basic namespace used by every .NET code. If we can explore the System namespace little bit, we can see it has lot of namespace user the system namespace. For example, System.Io, System.Net, System.Collections, System.Threading, etc.




A namespace can be created via the Namespace keyword. Here is an example to create "Books" namespace in VB.NET and C#.
VB.NET Code:

Namespace BooksClass Authors'Do somethingEnd ClassEnd Namespace

C# Code:
namespace Books{class Authors{//Do something}}

This is simple namespace example. We can also build hierarchy of namespace. Here is an example for this.
VB.NET Code:
Namespace BooksNamespace InventoryImports SystemClass AddInventoryPublic Function MyMethod()Console.WriteLine("Adding Inventory via MyMethod!")End FunctionEnd ClassEnd NamespaceEnd Namespace
C# Code:
namespace Books{namespace Inventory{using System;class AddInventory{public void MyMethod(){Console.WriteLine("Adding Inventory via MyMethod!");}}}}
That's all it takes to create namespace. Let's look how we can use the namespaces in our code. I'm going to create a standalone program to use the namespaces.
VB.NET Code:
Imports SystemClass HelloWorldPublic Sub Main()Dim AddInv As Inventory.AddInventory = New AddInventoryAddInv.MyMethod()End SubEnd Class
OR
Imports System.InventoryClass HelloWorldPublic Sub Main()Dim AddInv As AddInventory = New AddInventoryAddInv.MyMethod()End SubEnd Class
C# Code:
using Books;class HelloWorld{public static void Main(){Inventory.AddInventory AddInv = new AddInventory();AddInv.MyMethod();}}
OR
using Books.Inventory;class HelloWorld{public static void Main(){AddInventory AddInv = new AddInventory();AddInv.MyMethod();}}
Note: When using Imports statement or Using statement we can use only the namespace names, we can't use the class names. For example, the following statements are invalid.
Imports Books.Inventory.AddInventory
using Books.Inventory.AddInventory;

Friday, November 4, 2011

What is Exception Handling in c sharp ??????????

Exception :- 
                    The Exception is one type of error which comes from the system or currently executed application throws that which normally contains the information about the error .

Exception Handling is the mechanism provided by the c sharp language to handle that type of exception .

See The Below Example Which Contains the Try,Catch,Throw and finally blocks.


 class Program
    {
        static void Main(string[] args)
        {
            double a=100,result = 0;
            double b = 0;
           // double b = 10; //Please check it by removing comment and Give comment to above statement
            try
            {
                result = div(a, b);
                Console.WriteLine("The division of {0} and {1} is {2}", a, b, result);
            }
            catch (Exception e)
            {
                //Console.WriteLine(e.ToString());
                Console.WriteLine("Exception Divide by Zero is Handled");
            }
            finally
            {
                Console.WriteLine("This is finally block statement");
            }
            Console.ReadLine();
        }
        public static double div(double a1, double b1)
        {
            if (b1 == 0)
            {
                throw new System.DivideByZeroException();
            }
            else
            {
                return a1 / b1;
            }
           
        }
    }


WHAT is Generics in c sharp And Why we are using it.????????

Generics:-
                 The Generic is the concept by using which we can provide the type of parameter at the run time.

And also we can provide reuse-ability to the code written in the c# language,Just Example that we can take two data members of the class but at the declaration of the class we have to write or specify that the data type will be  temperary any name will be given to that at the declaration of the class .

But when we are creating object of that class to access it's properties at that time we are providing the type of the parameters which are used by that class.

Here in below example the Type of data is Integer ,String and Example class .

Example:-

public class GenericList<T>
{
    void Add(T input) { }
}
class Program
{
    private class ExampleClass { }
    static void Main()
    {
        // Declare a list of type int
        GenericList<int> list1 = new GenericList<int>();

        // Declare a list of type string
        GenericList<string> list2 = new GenericList<string>();

        // Declare a list of type ExampleClass
        GenericList<ExampleClass> list3 = new GenericList<ExampleClass>();
    }
}

Thursday, November 3, 2011

what is difference between 1 tier , 2 tier and 3 tier Architecture ????

1 tier Architecture in asp.net :-
                                                  In 1 Tier Architecture the code behind file  aspx.cs page is contains the code and data to access the database in the application.

2 Tier Architecture in asp.net :-
                                                 In 2 Tier Architecture the code behind file will contain the data and bal  Business Access Layer file is contain the code to access the database .
Means the code of database access is separated from simple data and .aspx.cs file.

3 Tier Architecture in asp.net :-
                                                  In 3 Tier Architecture the code and data will be separated from .aspx.cs file.
BAL(Business Access Layer ) will contain the code to access database and DAL(Data Access Layer) is contains the data which are require to access the data.And from .aspx .cs You have to just call perticular method which are stored in the BAL file and data from DAL file . BAL And DAL the file formate is .cs.