Place Ads here

Friday, July 1, 2011

Connected and Disconnected architecture in ADO.Net with Example (Insert Data in DisConnected Architecture in Asp.Net)

Asp.Net sample projects

         

                Insert Data in DisConnected Architecture
  • 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.



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
{ //Disconnnected 
    SqlConnection cn = new SqlConnection("data source=.;initial catalog=Amul;integrated security=sspi");
    SqlDataAdapter adp;
    DataSet ds;
    SqlCommandBuilder cmb;
    protected void Page_Load(object sender, EventArgs e)
    {
       show();
    }


    private void show()
    {
        adp = new SqlDataAdapter("select * from employee", cn);
        cmb = new SqlCommandBuilder(adp);
        ds = new DataSet();
        adp.Fill(ds, "emp");


        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)//INSERT
    {
        adp = new SqlDataAdapter("select * from employee", cn);
        cmb = new SqlCommandBuilder(adp);
        ds = new DataSet();
        adp.Fill(ds, "emp");
        DataRow rw = ds.Tables[0].NewRow();


        rw[0] = TextBox1.Text;
        rw[1] = TextBox2.Text;
        rw[2] = TextBox3.Text;
        rw[3] = TextBox4.Text;


        ds.Tables[0].Rows.Add(rw);


        adp.Update(ds.Tables[0]);


        Response.Write("Data is added");
        show();
    }
   
}

6 comments: