Friday, 13 April 2012

2 Tier Concept


/////Insert and Select Using 2 tier/////

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label3.Visible = false;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string cnstr = ConfigurationSettings.AppSettings["T"];
(Where T = <add key="T" value="Data Source=CESW302\SQLEXPRESS;Initial Catalog=Password;Integrated Security=True;Pooling=False"/>
)
        SqlConnection cn = new SqlConnection(cnstr);
        string insertsql="insert into Table1(UserName,Password) values('"+ TextBox1.Text +"','"+ TextBox2.Text +"')";
        SqlCommand cmd=new SqlCommand(insertsql, cn);
        try
        {
            cn.Open();
            cmd.ExecuteNonQuery();
            Label3.Visible=true;
            Label3.Text="inserted successfully";
        }
        catch
        {
            Label3.Visible=true;
            Label3.Text="Error";
        }
        finally
        {
            cn.Close();
        }
    }
protected void  Button2_Click(object sender, EventArgs e)
{
    string cnstr = ConfigurationSettings.AppSettings["T"];
        SqlConnection cn = new SqlConnection(cnstr);
        string selectsql ="Select * from Table1";
        SqlCommand cmd = new SqlCommand(selectsql, cn);
        cn.Open();
        SqlDataReader rw;
        rw = cmd.ExecuteReader();
        if (rw.Read())
        {
            TextBox3.Text = rw[0].ToString();
            TextBox4.Text = rw[1].ToString();
        }
        cn.Close();
       
}
    protected void TextBox3_TextChanged(object sender, EventArgs e)
    {

    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {

    }
}

No comments:

Post a Comment