How to delete data from data list through item command in C#?

..............................source code..................

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="datalist.aspx.cs" Inherits="datalist" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
      <script type="text/javascript">
          function ShowDiv()
          { setTimeout('document.getElementById("PB").style.display ="inline";', 500); }
    </script>
</head>
<body>
    <form id="form1" runat="server">

        <asp:DataList ID="DataList1" runat="server" RepeatLayout="Table"
            RepeatDirection="Horizontal" onitemcommand="DataList1_ItemCommand">
       
     
     
          <ItemTemplate>
          <table>
         
          <tr>
          <td>
          UserId
          </td>
          <td>
         
            <asp:Label ID="Labelid" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
          </td>
          </tr>
               <tr>
          <td>
          Nmae
          </td>
          <td>
            <asp:Label ID="Label1" runat="server" Text='<%#Eval("name") %>'></asp:Label>
          </td>
          </tr>
               <tr>
          <td>
          Salary
          </td>
          <td>
            <asp:Label ID="Label2" runat="server" Text='<%#Eval("salary") %>'></asp:Label>
          </td>
          </tr>
          </tr>
               <tr>
          <td>
       
          </td>
          <td><asp:Button ID="Button1" runat="server" Text="Delete" CommandName="delete"></asp:Button>
          </td>
          </tr>
            </table>
        </ItemTemplate>
     
        </asp:DataList>
 

      <asp:Button ID="Button1" runat="server" OnClientClick="ShowDiv()" Text="Button" OnClick="Button1_Click1" />
   <div style="background-color: Red; display: none;" id="PB"> Progress bar   </div>
    </form>
</body>
</html>
............................end source code..........................
....................................code behind......................................
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Text;
using System.Data.SqlClient;
using System.IO;
public partial class datalist : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
    SqlCommand cmd;
    SqlDataReader dr;
    SqlDataAdapter da;
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           
        }
    }

    protected void filldatalist()
    {
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter("select * from emp44", con);
        da.Fill(ds);
        DataList1.DataSource = ds;
        DataList1.DataBind();
        con.Close();
    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "delete")
        {
            Label imgid1 = (Label)e.Item.FindControl("Labelid");
            cmd = new SqlCommand("delete from emp44 where id=" + imgid1.Text + "", con);
                //cmd = new SqlCommand("usp_delete", con);
                //cmd.CommandType = CommandType.StoredProcedure;
                //cmd.Parameters.AddWithValue("@id", imgid1.Text);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                    filldatalist();
    
        }
    }
   
    protected void Button1_Click1(object sender, EventArgs e)
    {
        filldatalist();
    }
}
..........................end code behind..............................
Share:

No comments:

Post a Comment

Sunday, 16 December 2012

How to delete data from data list through item command in C#?

..............................source code..................

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="datalist.aspx.cs" Inherits="datalist" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
      <script type="text/javascript">
          function ShowDiv()
          { setTimeout('document.getElementById("PB").style.display ="inline";', 500); }
    </script>
</head>
<body>
    <form id="form1" runat="server">

        <asp:DataList ID="DataList1" runat="server" RepeatLayout="Table"
            RepeatDirection="Horizontal" onitemcommand="DataList1_ItemCommand">
       
     
     
          <ItemTemplate>
          <table>
         
          <tr>
          <td>
          UserId
          </td>
          <td>
         
            <asp:Label ID="Labelid" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
          </td>
          </tr>
               <tr>
          <td>
          Nmae
          </td>
          <td>
            <asp:Label ID="Label1" runat="server" Text='<%#Eval("name") %>'></asp:Label>
          </td>
          </tr>
               <tr>
          <td>
          Salary
          </td>
          <td>
            <asp:Label ID="Label2" runat="server" Text='<%#Eval("salary") %>'></asp:Label>
          </td>
          </tr>
          </tr>
               <tr>
          <td>
       
          </td>
          <td><asp:Button ID="Button1" runat="server" Text="Delete" CommandName="delete"></asp:Button>
          </td>
          </tr>
            </table>
        </ItemTemplate>
     
        </asp:DataList>
 

      <asp:Button ID="Button1" runat="server" OnClientClick="ShowDiv()" Text="Button" OnClick="Button1_Click1" />
   <div style="background-color: Red; display: none;" id="PB"> Progress bar   </div>
    </form>
</body>
</html>
............................end source code..........................
....................................code behind......................................
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Text;
using System.Data.SqlClient;
using System.IO;
public partial class datalist : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
    SqlCommand cmd;
    SqlDataReader dr;
    SqlDataAdapter da;
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           
        }
    }

    protected void filldatalist()
    {
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter("select * from emp44", con);
        da.Fill(ds);
        DataList1.DataSource = ds;
        DataList1.DataBind();
        con.Close();
    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "delete")
        {
            Label imgid1 = (Label)e.Item.FindControl("Labelid");
            cmd = new SqlCommand("delete from emp44 where id=" + imgid1.Text + "", con);
                //cmd = new SqlCommand("usp_delete", con);
                //cmd.CommandType = CommandType.StoredProcedure;
                //cmd.Parameters.AddWithValue("@id", imgid1.Text);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                    filldatalist();
    
        }
    }
   
    protected void Button1_Click1(object sender, EventArgs e)
    {
        filldatalist();
    }
}
..........................end code behind..............................

No comments:

Post a Comment

Popular

Blog Archive

Total Pageviews

Archive