Place Ads here

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();


    }

3 comments:

  1. I was looking for this in book . Thank god i found it on the web. And yes most probably thanks you to , for posting it.

    ReplyDelete
  2. I am happy for you as you got the required data from my blog.For further Disconnected Architecture you can go for this link.
    http://amul-aspnetsampleprojects.blogspot.com/2011/07/deleteupdate-data-disconnected.html
    Regards
    Amul V V W

    ReplyDelete
  3. Thanks for this useful information.

    ReplyDelete