Wednesday, August 27, 2014

ASP.NET with C# Disconnected architecture

using System.Data;
using System.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
{
    string str = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Sagar\Documents\Visual Studio 2010\WebSites\kalp\App_Data\example.mdf;Integrated Security=True;User Instance=True";
    SqlConnection con;
    protected void Page_Load(object sender, EventArgs e)
    {
        con = new SqlConnection(str);
        dispdata();  
    }
    protected void submit_Click(object sender, EventArgs e)
    {

        con.Open();
        SqlCommand cmd = new SqlCommand("insert into tblstu values('"+name.Text+"','"+city.Text+"')",con);
        cmd.ExecuteNonQuery();
        Response.Write("<script>alert('record inserted successfully');</script>");
        con.Close();
    }
    protected void dispdata()
    {
        DataTable dt = new DataTable();
        SqlCommand cmd = new SqlCommand("select * from tblstu",con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        GridView1.DataSource=dt;
        GridView1.DataBind();

    }
}

No comments: