Place Ads here

Friday, July 1, 2011

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

Source Code to Insert Data in Connected Architecture


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)
    {


    }
    protected void Button1_Click(object sender, EventArgs e)//INSERT BUTTON
    {
        int id, sal;
        string name, dept;


        id = int.Parse(TextBox1.Text);
        name = TextBox2.Text;
        dept = TextBox3.Text;
        sal = int.Parse(TextBox4.Text);


        cmd = new SqlCommand("insert into employee values('" +
            id + "','" + name + "','" + dept + "','" + sal + "')", cn);


        cn.Open();
        cmd.ExecuteNonQuery();
        cn.Close();


        Response.Write("New Record is added");


        AmulMethod();
    }
    protected void Button2_Click(object sender, EventArgs e)//SHOW DETAILS BUTTON
    {
        AmulMethod();
    }


    private void AmulMethod()
    {


        cmd = new SqlCommand("select * from employee", cn);
        cn.Open();
        rd = cmd.ExecuteReader();
        GridView1.DataSource = rd;
        GridView1.DataBind();
        cn.Close();
    }
}

No comments:

Post a Comment