Full width home advertisement

Post Page Advertisement [Top]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class detailview : System.Web.UI.Page
{
    string str = @"Data Source=My-A11648435D\SQLEXPRESS;Initial Catalog=morning;Integrated Security=True";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            filldetails();
        }

    }
    public void filldetails()
    {
        SqlConnection con = new SqlConnection(str);
        SqlDataAdapter da = new SqlDataAdapter("select * from emp", con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        DetailsView1.DataSource = ds;
        DetailsView1.DataBind();
    }
    protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
    {
        DetailsView1.PageIndex = e.NewPageIndex;
        filldetails();
    }
    protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
    {
        DetailsView1.ChangeMode(e.NewMode);
        filldetails();
    }

    protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
    {
        TextBox id = (TextBox)DetailsView1.FindControl("TextBox1");
        TextBox fname = (TextBox)DetailsView1.FindControl("TextBox2");
        TextBox lname = (TextBox)DetailsView1.FindControl("TextBox4");
        TextBox ads = (TextBox)DetailsView1.FindControl("TextBox6");
        SqlConnection con = new SqlConnection(str);
        SqlCommand cmd = new SqlCommand("update emp set fname='" + fname.Text + "',lname='" + lname.Text + "',ads='" + ads.Text + "' where id=" + int.Parse(id.Text), con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
        filldetails();


    }
    protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        TextBox fname = (TextBox)DetailsView1.FindControl("TextBox3");
        TextBox lname = (TextBox)DetailsView1.FindControl("TextBox5");
        TextBox ads = (TextBox)DetailsView1.FindControl("TextBox7");
        SqlConnection con = new SqlConnection(str);
        SqlCommand cmd = new SqlCommand("insert into emp (fname,lname,ads) values('"+fname.Text+"','"+lname.Text+"','"+ads.Text+"'", con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
        filldetails();

    }
    protected void DetailsView1_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
    {
        Label lblid = (Label)DetailsView1.FindControl("Label1");
        SqlConnection con = new SqlConnection(str);
        SqlCommand cmd = new SqlCommand("delete from emp where id="+int.Parse(lblid.Text), con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        filldetails();
     
    }
}

No comments:

Post a Comment

Bottom Ad [Post Page]

| Designed by Colorlib