Place Ads here

Wednesday, February 15, 2012

Storing and Retrieving Image from Database in C# Asp.Net




Storing and Retrieving Image from Database
Write Below code in Default Page


using System;
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;


using System.Data.SqlClient;


public partial class _Default : System.Web.UI.Page 
{
    SqlConnection connection = new SqlConnection("data source=.; Initial catalog=LoadPhoto;integrated security=sspi;");




    protected void Page_Load(object sender, EventArgs e)
    {


    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            FileUpload img = (FileUpload)imgUpload;
            Byte[] imgByte = null;
            if (img.HasFile && img.PostedFile != null)
            {
                //To create a PostedFile
                HttpPostedFile File = imgUpload.PostedFile;
                //Create byte Array with file len
                imgByte = new Byte[File.ContentLength];
                //force the control to load data in array
                File.InputStream.Read(imgByte, 0, File.ContentLength);
            }
            // Insert the employee name and image into db
            //string conn = ConfigurationManager.ConnectionStrings["EmployeeConnString"].ConnectionString;
            //connection = new SqlConnection(conn);


            connection.Open();
            string sql = "INSERT INTO EmpDetails(empname,empimg) VALUES(@enm, @eimg) SELECT @@IDENTITY";
            SqlCommand cmd = new SqlCommand(sql, connection);
            cmd.Parameters.AddWithValue("@enm", txtEName.Text.Trim());
            cmd.Parameters.AddWithValue("@eimg", imgByte);
            int id = Convert.ToInt32(cmd.ExecuteScalar());
            lblResult.Text = String.Format("Employee ID is {0}", id);


            Image1.ImageUrl = "~/ShowImage.ashx?id=" + id;
        }
        catch
        {
            lblResult.Text = "Photo Size should be less than 20 KB";
        }
        finally
        {
            connection.Close();
        }
    }
}




////////////////////////////////////////////////////////////////////////////////////////////////


Write the below code in  ShowImage.ashx file



<%@ WebHandler Language="C#" Class="ShowImage" %>


using System;

using System.Configuration;
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;

public class ShowImage : IHttpHandler

{


    SqlConnection connection = new SqlConnection("data source=.; Initial catalog=LoadPhoto;integrated security=sspi;");
    public void ProcessRequest(HttpContext context)
    {
       Int32 empno;
       if (context.Request.QueryString["id"] != null)
            empno = Convert.ToInt32(context.Request.QueryString["id"]);
       else
            throw new ArgumentException("No parameter specified");

       context.Response.ContentType = "image/jpeg";

       Stream strm = ShowEmpImage(empno);
       byte[] buffer = new byte[4096];
       int byteSeq = strm.Read(buffer, 0, 4096);

       while (byteSeq > 0)

       {
           context.Response.OutputStream.Write(buffer, 0, byteSeq);
           byteSeq = strm.Read(buffer, 0, 4096);
       }       
       //context.Response.BinaryWrite(buffer);
    }

    public Stream ShowEmpImage(int empno)

    {
 //string conn = ConfigurationManager.ConnectionStrings     ["EmployeeConnString"].ConnectionString;
        //SqlConnection connection = new SqlConnection(conn);
        
        
        string sql = "SELECT empimg FROM EmpDetails WHERE empid = @ID";
        SqlCommand cmd = new SqlCommand(sql,connection);
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@ID", empno);
        connection.Open();
        object img = cmd.ExecuteScalar();
       
        try
        {
            return new MemoryStream((byte[])img);
        }
        catch
        {
            return null;
        }
        finally
        {
            connection.Close();
        }
    }

    public bool IsReusable

    {
        get
        {
            return false;
        }
    }


}



Thursday, February 2, 2012

InterFace


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


interface ICalculate
{
    void IADD();
    void ISUB();
    
}


class Calculate : ICalculate /////////Child class Calculate Inherited Interface ICalculate
{
    #region ICalculate Members


    public void IADD()
    {
        //throw new System.NotImplementedException();


        int x, y, z;
        Console.WriteLine("Enter values for x and y");
        x = int.Parse(Console.ReadLine());
        y = Convert.ToInt32(Console.ReadLine());
        z = x + y;
        Console.WriteLine("Addition of x and y is: " + z);


    }


    public void ISUB()
    {
       // throw new System.NotImplementedException();


        int x, y, r;
        Console.WriteLine("Enter values for x and y");
        x = int.Parse(Console.ReadLine());
        y = Convert.ToInt32(Console.ReadLine());
       r = x - y;
        Console.WriteLine("Subtraction of x and y is: " +r);




    }


    
   #endregion
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Interface
{
    class Program
    {
        static void Main(string[] args)
        {




            
            Console.WriteLine("An interface looks like a class, but has no implementation. The only thing it contains are declarations of events, indexers, methods and/or properties. The reason interfaces only provide declarations is because they are inherited by classes and structs, which must provide an implementation for each interface member declared.");
            Console.WriteLine();

            Console.WriteLine("C# does not support multiple inheritence to overcome these problems we use interfaces. This is main use of Interfaces we have in C#.");
            Console.WriteLine();
            Console.WriteLine("Interfaces are a very logical way of grouping objects in terms of behavior. ");
            Console.WriteLine();
            ICalculate obj = new Calculate();/////Instance ie representative of Interface and Abstract class cannot be created
                                              /////ICalculate obj = new ICalculate();
                                              ////// object of interface is a representative of Class Calculate 
                                              ////// Break Point cannot be allocated for interface for debugging.
            obj.IADD();
            obj.ISUB();
            
            
            //Console.WriteLine("Division of 2 nos is");
            //Console.WriteLine("" + obj.IDIV(10,2));

        }
    }
}

InterFace


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


interface ICalculate
{
    void IADD();
    void ISUB();
    










   
}


class Calculate : ICalculate /////////Child class Calculate Inherited Interface ICalculate
{
    #region ICalculate Members


    public void IADD()
    {
        //throw new System.NotImplementedException();


        int x, y, z;
        Console.WriteLine("Enter values for x and y");
        x = int.Parse(Console.ReadLine());
        y = Convert.ToInt32(Console.ReadLine());
        z = x + y;
        Console.WriteLine("Addition of x and y is: " + z);


    }


    public void ISUB()
    {
       // throw new System.NotImplementedException();


        int x, y, r;
        Console.WriteLine("Enter values for x and y");
        x = int.Parse(Console.ReadLine());
        y = Convert.ToInt32(Console.ReadLine());
       r = x - y;
        Console.WriteLine("Subtraction of x and y is: " +r);




    }


    
   #endregion
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Interface
{
    class Program
    {
        static void Main(string[] args)
        {




            
            Console.WriteLine("An interface looks like a class, but has no implementation. The only thing it contains are declarations of events, indexers, methods and/or properties. The reason interfaces only provide declarations is because they are inherited by classes and structs, which must provide an implementation for each interface member declared.");
            Console.WriteLine();

            Console.WriteLine("C# does not support multiple inheritence to overcome these problems we use interfaces. This is main use of Interfaces we have in C#.");
            Console.WriteLine();
            Console.WriteLine("Interfaces are a very logical way of grouping objects in terms of behavior. ");
            Console.WriteLine();
            ICalculate obj = new Calculate();/////Instance ie representative of Interface and Abstract class cannot be created
                                              /////ICalculate obj = new ICalculate();
                                              ////// object of interface is a representative of Class Calculate 
                                              ////// Break Point cannot be allocated for interface for debugging.
            obj.IADD();
            obj.ISUB();
            
            
            //Console.WriteLine("Division of 2 nos is");
            //Console.WriteLine("" + obj.IDIV(10,2));

        }
    }
}

Method OverRiding


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


namespace MethodOverriding1
{
    //sealed class M
    //{
        public class Base
        {


            public virtual void Test(int a, int b)//"Virtual Keyword" is used when u need to 
                                                 //override same Method called as Test in Child Class.
            {
                int c;
                c = a + b;
                System.Console.WriteLine("Add is: " + c);
            }
        }
        public class Child : Base
        {


            public override void Test(int a, int b)
            {
                int c;
                c = a * b;
                System.Console.WriteLine("Multiplication is: " + c);
            }


            public class Child1 : Base
            {


                public override void Test(int a, int b)
                {
                    int c;
                    c = a / b;
                    System.Console.WriteLine("Division is: " + c);
                }


            }




            static public void Main()
            {
                Base B = new Base();
                Child c = new Child();
                Child1 d = new Child1();


                B.Test(10, 20);
                c.Test(30, 40);
                d.Test(20, 5);




            }
        }


    }
//}

Tuesday, November 8, 2011

Sending Mail in C# Asp.net


Sending Mail in C# Asp.net

using System;
using System.Collections.Generic;
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 BtnSend_Click(object sender, EventArgs e)
    {
        System.Net.Mail.MailMessage obj = new System.Net.Mail.MailMessage();
        obj.From = new System.Net.Mail.MailAddress(TxtFrm.Text);
        if (TxtTo.Text.Contains(","))
        {
            string[] to = TxtTo.Text.Split(',');
            foreach (string r in to)
            {
                obj.To.Add(new System.Net.Mail.MailAddress(r));
            }
        }
        else
        {
            obj.To.Add(new System.Net.Mail.MailAddress(TxtTo.Text));
        }
        obj.CC.Add(new System.Net.Mail.MailAddress(TxtCc.Text));
        obj.Bcc.Add(new System.Net.Mail.MailAddress(TxtBCc.Text));
        obj.Subject = TxtSub.Text;
        obj.IsBodyHtml = true;
        System.Net.Mail.Attachment att = new System.Net.Mail.Attachment(FileUpload1.PostedFile.InputStream,FileUpload1.PostedFile.FileName,FileUpload1.PostedFile.ContentType);
        obj.Attachments.Add(att);
        obj.Body = Txtbdy.Text;
        System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("1.0.0.0", 25);
        try
        {
            smtp.Send(obj);
        }
        catch (Exception ex)
        {
            Response.Redirect(ex.Message);
        }
    }
}

sql stored procedure Add Operation


sql stored procedure
Create PROCEDURE [dbo].[AddDEmp]
(
   @empName  Varchar(50),
   @empAge    int,
   @empEmail  nVarchar(50),
   @PhoneNo  nVarchar(50)      
)
AS
INSERT  INTO  empRecord(empName,empAge,empEmail,PhoneNo) VALUES(@empName,@empAge,@empEmail,@PhoneNo)


--------------------------------------------------------------------------------




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;


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


    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string constr = "Data Source=RLABSRV/SQLEXPRESS;Initial Catalog=employee;Integrated Security=True";
        //DataTable allData = new DataTable();


        SqlConnection con = new SqlConnection(constr);




        try
        {
            SqlCommand cmd = new SqlCommand("AddDEmp", con);
            cmd.CommandType = CommandType.StoredProcedure;


            con.Open();
            cmd.Parameters.Add("empName", SqlDbType.VarChar, 50);
            cmd.Parameters["empName"].Value = TextBox1.Text;




            cmd.Parameters.Add("empAge", SqlDbType.Int);
            cmd.Parameters["empAge"].Value = TextBox2.Text;


            cmd.Parameters.Add("empEmail", SqlDbType.NVarChar, 50);
            cmd.Parameters["empEmail"].Value = TextBox3.Text;


            cmd.Parameters.Add("PhoneNo", SqlDbType.NVarChar, 50);
            cmd.Parameters["PhoneNo"].Value = TextBox4.Text;


            cmd.ExecuteNonQuery();
         
            con.Close();
        }


        catch(Exception ex)
        {
            con.Close();




            Label2.Text = ex.Message;






        }
     
    }
}







Difference between DLL and Assembly

Difference between DLL and Assembly

DLL is an Inbound Field.
There are many Dlls which can be present in a single Application.
Dlls can be shared with other Applications.
Dll is linked to an Exe during Run Time.
------------------------------------------------------------------------------------
Exe is used for Launching an Application.
Exe is used for Single Application.
Exe cannot be shared with other Applications.
Exe runs on its own.

Monday, July 25, 2011

Connected and disconnected architecture in ADO.Net with Example (Delete/Update Data in Disconnected architecture in Asp.net)


Theory for Disconnected architecture in asp.net

Connected and disconnected architecture in ADO.Net with Example (Delete/Update Data in Disconnected architecture in Asp.net)
What is Disconnected Architecture in ADO.Net?
Disconnected Architecture is used when there is no requirement to have quick access to the Server.Thus Connection is not maintained with the server. Data is stored remotely using an Object called "DataSet".
A DataSet is a Local Copy of Relational Data and all Operations that needs to be carried out on the data are done on this Local copy on Client Side with no connection to the original data on Server.
SqlDataAdapter Object is used to handle the additon,deletion,updation of data from the Data Source to DataSet.
Data Source is just a DataTable. adp.Fill() is a method  of SqlDataAdapter Object to populate a DataSet with the result of executed query.


Code for Default.aspx.cs is 
--------------------------------------------------------------------------------------------------------------
using System;
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;
using System.Data.SqlClient;


public partial class _Default : System.Web.UI.Page
{       //DISCONNECTED


    SqlConnection cn = new SqlConnection("data source=.;initial catalog=Amul;integrated security=sspi");
    SqlDataAdapter adp;
    DataSet dr;
    SqlCommandBuilder cmb;


    protected void Page_Load(object sender, EventArgs e)
    {
        adp = new SqlDataAdapter("select * from employee", cn);
        cmb = new SqlCommandBuilder(adp);
        dr = new DataSet();
        adp.Fill(dr, "emp");
        GridView1.DataSource = dr;
        GridView1.DataBind();


        if (!Page.IsPostBack)
        {
            adp = new SqlDataAdapter("select emp_id from employee", cn);


 foreach (DataRow r in dr.Tables[0].Rows)
            {                
                DropDownList1.Items.Add(r[0].ToString());            }        }
    }




    protected void Button1_Click(object sender, EventArgs e)//UPDATE
    {
        DataRow rw = dr.Tables[0].Rows[DropDownList1.SelectedIndex];


        rw.BeginEdit();
        rw[1] = TextBox1.Text;
        rw[2] = TextBox2.Text;
        rw[3] = TextBox3.Text;
        rw.EndEdit();


        adp.Update(dr, "emp");
        Response.Write("Data is updated");
        GridView1.DataSource = dr;
        GridView1.DataBind();


    }
   
    protected void Button2_Click(object sender, EventArgs e)//DELETE
    {
        
        string s = DropDownList1.SelectedItem.Text;
        DataRow rw = dr.Tables[0].Rows[DropDownList1.SelectedIndex];
        rw.Delete();
        adp.Update(dr, "emp");
        Response.Write("Record is deleted");
        //Label5.Visible = true;


        DropDownList1.Items.Remove(s);


        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
        GridView1.DataSource = dr;
        GridView1.DataBind();


 }
    protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
    {
        int i = 0;
        adp = new SqlDataAdapter("select * from employee where emp_id='" + DropDownList1.SelectedItem.Text + "'", cn);
        cmb = new SqlCommandBuilder(adp);
        //DataRow rw = new SqlCommandBuilder(adp);
       
        DataRow rw = dr.Tables[i].Rows[DropDownList1.SelectedIndex];
       
        //DropDownList1.Text = rw["e_id"].ToString();
      
}
    
}

Inheritance



 

 

 

 

              

 Inheritance











2E: A Learner's Guide to Real-World Programming with Visual C# and .NET (Head First Guides]Asp.Net sample projects
How to use inheritance in c# asp.netSource Code to use Inheritance in c# Asp.Net
 Inheritance
Inheritance is a feature of Object Oriented Programming language.
Inheritance is a way of communication  between classes.
In Inheritance one is a parent class and other is a child class.
In Inheritance parent class is called as base class and child class is called as sub class.
Inheritance Creates hierarchy of Objects.
In C#,Inheritance is a process of  creating type such as a class or interface from an existing type such as a class or interface.

Source Code to use Inheritance in c# asp.netusing System;
public class parentclass
{
public parentclass()                          ////I am coding a constructor
{
Console.Writeline("Hi, My name is Amul.I am a parentclass constructor.");
Console.Writeline("I have same name as a class name.");
Console.Writeline("");
}
public void  parentclassmethod()              //// I am coding a Method
  {
Console.Writeline("There is no need to create an Object for me as I am a constructor.(parentclass constructor.)");
Console.Writeline("I get Invoked Automatically.So Whatever is written in constructor is invoked first.");
Console.Writeline("Because of Me as a Constructor,Loading time required  is less.");
Console.Writeline("");
 }
}

public class childclass : parentclass    //////ParentClass is a base class of ChildClass.
                                                       //////ChildClass inherites the functionalities of ParentClass
{
public childclass()                          //////I am coding a constructor
{
                 Console.Writeline("Hi , I am Clone of Amul. I am a childclass constructor.");
                     Console.Writeline("I also have a name as that of class name.");
                        Console.Writeline("");
}

public void childclassmethod()         //// I am coding a Method
{
Console.Writeline("There is no need to create an Object for me  also as I am a constructor.(childclass constructor.)");
Console.Writeline("I get Invoked Automatically.So Whatever is written in constructor is invoked first.");
Console.Writeline("Because of Me as a Constructor, Loading time required  is less.");
Console.Writeline("");
}

public static void Main()
{
       childclass  object1 = new childclass();
       childclass.parentclassmethod();
       childclass.childclassmethod();
}
}
OUT PUT        //////For Inheritance
Hi, My name is Amul.I am a parentclass constructor.        
I have same name as a class name.
Hi , I am Clone of Amul. I am a childclass constructor.
I also have a name as that of class name.

There is no need to create an Object for me as I am a constructor.(parentclass constructor.)
I get Invoked Automatically.So Whatever is written in constructor is invoked first.
Because of Me as a Constructor, Loading time required  is less.

There is no need to create an Object for me also as I am a constructor.(childclass constructor.)
I get Invoked Automatically.So Whatever is written in constructor is invoked first.
Because of Me as a Constructor, Loading time required  is less.
.
 

Method Overloading

 

 

 

 

 

 

 

Method OverLoading

Asp.Net sample projects



                                                         Method OverLoading








Method Overloading source code in C# asp.net








In Method Overloading one method can be used for two different Datatypes Int,Double.
Method OverLoading  is Compile Time Polymorphism.
Polymorphism is an Object Oriented Programming Concept in which same operation may behave differently on different classes.




///////////////////////MethodOverloadclass.cs/////////////////////////////////////

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

namespace MethodOverloading
{
    class MethodOverloadclass
    {

        public int Add(int a, int b)
        {
            int c;
            c = a + b;

            return c;


        }
        public int Add(int a, int b, int c)
        {
            int d;
            d = a + b + c;

            return d;


        }
        public static void Add(double x, double y, double p, double q)//Word Static says no need to create an object.
                                                                      //directly call method by class reference.
        {
            double z;
            z = x + y + p + q;

            Console.WriteLine("Addition  of 4 nos is: " + z);


        }

       

    }
}
--------------------------------------------------------------------------------------------------------------------

////////////////////

Program.cs////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MethodOverloading;

namespace MethodOverloading1
{
    class Program
    {
        static void Main(string[] args)
        {

         MethodOverloadclass obj = new MethodOverloadclass();

         Console.WriteLine("This is a program of Method OverLoading which uses Polymorphism");
         Console.WriteLine("Here we  added 2 nos and 3 nos and 4 nos with same Method defined  as Add.");

       Console.WriteLine("Addition of 2 nos is: " + obj.Add(2,2));
       Console.WriteLine("Addition of 3 nos is: " + obj.Add(2, 2, 2));
       MethodOverloadclass.Add(10,10,10,10);

      

        }
    }
}

Home

Saturday, July 2, 2011

CRYPTOGRAPHY

Asp.Net sample projects


using System;
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;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.Text;


public partial class_Default:System.Web.UI.Page
{
byte[] hashvalue,Messagebytes;
string StringtoConvert;
UnicodeEncoding MyUnicodeEncoding;


protected void Page_Load(object sender,EventsArgs e)
{
}


protected void ButtonSHA1_click(object sender,EventsArgs e)
{
TextBox2.Text="";
StringtoConvert=TextBox1.Text;
MyUniCodeEncoding = new UniCodeEncoding();
MessageBytes = MyUniCodeEncoding.GetBytes(StringtoConvert);
SHA1Managed SHhash = new SHA1Managed();
hashvalue = SHhash.ComputeHash(MessageBytes);
foreach (byte b in hashValue)
{
TextBox2.Text= TextBox2.Text + String.Format(" {0:X2}",b);
}
}
protected void ButtonSHA256_click(object sender,EventsArgs e)
{
 SHA256Managed SHhash = new SHA256Managed();
}


 
protected void ButtonSHA384_click(object sender,EventsArgs e)
{
 SHA384Managed SHhash = new SHA384Managed();
}



protected void ButtonSHA512_click(object sender,EventsArgs e)
{
 SHA512Managed SHhash = new SHA512Managed();
}


}


Friday, July 1, 2011

Connected and Disconnected architecture in ADO.Net with Example(Delete/Update Data Connected Architecture in Asp.net)

          Asp.Net sample projects
                       Connected and Disconnected architecture in ADO.Net with Example(Delete/Update Data Connected Architecture in Asp.net)
  • What is Connected Architecture in ADO.Net?
  • "Connected  Architecture"  means the connection is maintained to the server (or database) and it is  maintained Continously.
  • DataReader is used in Connected Architecture.
  • DataReader is used especialy when quick access to data is required without Storing the extracted data remotely.
  • DataReader is created using the Command.Execute Reader.

using System;
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;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{//CONNECTED ARCHITECTURE.
    SqlConnection cn = new SqlConnection("data source=.;initial catalog=Amul;integrated security=sspi");
    SqlCommand cmd;
    SqlDataReader rd;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            cmd = new SqlCommand("select emp_id from employee", cn);
            cn.Open();
            rd = cmd.ExecuteReader();
            while (rd.Read())
            {
                DropDownList1.Items.Add(rd[0].ToString());
            }
            rd.Close();
            cn.Close();
        }
    }
    protected void Button2_Click(object sender, EventArgs e)//DELETE
    {
        string id = DropDownList1.SelectedItem.Text;
        cmd = new SqlCommand("delete  from employee where emp_id='" +
            DropDownList1.SelectedItem.Text + "'", cn);
        cn.Open();
        cmd.ExecuteNonQuery();
        cn.Close();


        Response.Write("Record is deleted");


        DropDownList1.Items.Remove(id);
    }
    protected void Button1_Click(object sender, EventArgs e)//UPDATE
    {
        int s;
        string n, d;


        n = TextBox1.Text;
        d = TextBox2.Text;
        s = int.Parse(TextBox3.Text);


        cmd = new SqlCommand("update employee set emp_name='" + n +
            "',emp_dept='" + d + "',emp_salary='" + s +
            "' where emp_id='" + DropDownList1.SelectedItem.Text +
            "'", cn);
        cn.Open();
        cmd.ExecuteNonQuery();
        cn.Close();
        Response.Write("Record is updated");
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        cmd = new SqlCommand("select * from employee where emp_id='"
              + DropDownList1.SelectedItem.Text + "'", cn);
        cn.Open();
        rd = cmd.ExecuteReader();
        rd.Read();
        TextBox1.Text = rd[1].ToString();
        TextBox2.Text = rd[2].ToString();
        TextBox3.Text = rd[3].ToString();
        rd.Close();
        cn.Close();


    }