Place Ads here

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

1 comment:

  1. good article thank you . i was searching this article.Asp.net + sql disconnected architecture. Thanks.

    ReplyDelete