crud operation in datalist controls use disconnected

Write a program to perform insert, update, delete Operation on Datalist Controls  use disconnected Architecture.

Aspx Files

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="datalist.aspx.cs" Inherits="datalist" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:DataList ID="DataList1" DataKeyField="Id" ShowFooter="true" runat="server" OnCancelCommand="DataList1_CancelCommand" OnDeleteCommand="DataList1_DeleteCommand" OnEditCommand="DataList1_EditCommand" OnItemCommand="DataList1_ItemCommand" OnUpdateCommand="DataList1_UpdateCommand">
        <HeaderTemplate>
            <table>
                <tr>
                  
                        <th>Id</th>
                        <th>Name</th>
                        <th>Department</th>
                        <th>Semster</th>
                        <th>Gender</th>
                        <th>Operation</th>
                   
                </tr>
          
        </HeaderTemplate>
        <ItemTemplate>
         
           <tr>
               <td><%#Eval("Id") %></td>
               <td><%#Eval("name") %></td>
               <td><%#Eval("dept") %></td>
               <td><%#Eval("sem") %></td>
               <td><%#Eval("gender") %></td>
               <td>
                   <asp:LinkButton ID="lbedit" runat="server" CommandName="Edit">Edit</asp:LinkButton>
                   <asp:LinkButton ID="lbdelete" runat="server" CommandName="Delete">Delete</asp:LinkButton>
               </td>
           </tr>
              
        </ItemTemplate>
       
        <AlternatingItemTemplate >
            <tr style="background-color:aqua;">
               <td><%#Eval("Id") %></td>
               <td><%#Eval("name") %></td>
               <td><%#Eval("dept") %></td>
               <td><%#Eval("sem") %></td>
               <td><%#Eval("gender") %></td>
               <td>
                   <asp:LinkButton ID="lbedit" runat="server" CommandName="Edit">Edit</asp:LinkButton>
                   <asp:LinkButton ID="lbdelete" runat="server" CommandName="Delete">Delete</asp:LinkButton>
               </td>
           </tr>
        </AlternatingItemTemplate>
        <EditItemTemplate>
           
             <tr>
                <td><%#Eval("Id") %></td>
               <td><asp:TextBox ID="txtname" runat="server" Text='<%#Eval("name") %>'></asp:TextBox></td>
               <td><asp:DropDownList ID="ddldept" SelectedValue='<%#Eval("dept") %>' runat="server">
                        <asp:ListItem>CE</asp:ListItem>
                        <asp:ListItem>IT</asp:ListItem>
                        <asp:ListItem>EC</asp:ListItem>
                        <asp:ListItem>ME</asp:ListItem>
                        <asp:ListItem>EE</asp:ListItem>
                        <asp:ListItem>CV</asp:ListItem>
                    </asp:DropDownList></td>
               <td><asp:DropDownList ID="ddlsem" SelectedValue='<%#Eval("sem") %>' runat="server">
                        <asp:ListItem>1</asp:ListItem>
                        <asp:ListItem>2</asp:ListItem>
                        <asp:ListItem>3</asp:ListItem>
                        <asp:ListItem>4</asp:ListItem>
                        <asp:ListItem>5</asp:ListItem>
                        <asp:ListItem>6</asp:ListItem>
                        <asp:ListItem>7</asp:ListItem>
                    </asp:DropDownList></td>
               <td>
                   <asp:RadioButtonList ID="rbgender"  runat="server" SelectedValue='<%#Eval("gender") %>'>
                        <asp:ListItem>Male</asp:ListItem>
                        <asp:ListItem>Female</asp:ListItem>
                    </asp:RadioButtonList></td>
            
               <td>
                   <asp:LinkButton ID="lbUpdate" runat="server" CommandName="Update">Update</asp:LinkButton>
                   <asp:LinkButton ID="lbcancel" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
               </td>
           </tr>
      
        </EditItemTemplate>
        <FooterTemplate>
         
             <tr>
                <td></td>
               <td><asp:TextBox ID="txtnameadd" runat="server" ></asp:TextBox></td>
               <td><asp:DropDownList ID="ddldeptadd"  runat="server">
                        <asp:ListItem>CE</asp:ListItem>
                        <asp:ListItem>IT</asp:ListItem>
                        <asp:ListItem>EC</asp:ListItem>
                        <asp:ListItem>ME</asp:ListItem>
                        <asp:ListItem>EE</asp:ListItem>
                        <asp:ListItem>CV</asp:ListItem>
                    </asp:DropDownList></td>
               <td><asp:DropDownList ID="ddlsemadd"  runat="server">
                        <asp:ListItem>1</asp:ListItem>
                        <asp:ListItem>2</asp:ListItem>
                        <asp:ListItem>3</asp:ListItem>
                        <asp:ListItem>4</asp:ListItem>
                        <asp:ListItem>5</asp:ListItem>
                        <asp:ListItem>6</asp:ListItem>
                        <asp:ListItem>7</asp:ListItem>
                    </asp:DropDownList></td>
               <td>
                   <asp:RadioButtonList ID="rbgenderadd"  runat="server" >
                        <asp:ListItem>Male</asp:ListItem>
                        <asp:ListItem>Female</asp:ListItem>
                    </asp:RadioButtonList></td>
            
               <td>
                   <asp:LinkButton ID="lbinsert" runat="server" CommandName="Insert">Insert</asp:LinkButton>
                 
               </td>
           </tr>
      
        </FooterTemplate>
    </asp:DataList>
</asp:Content>

 

Aspx.cs Files

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.Data.SqlClient;
using System.Configuration;
public partial class datalist : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            getdata();
        }
       
    }
    public void getdata()
    {
        SqlDataAdapter adtp = new SqlDataAdapter("select * from student", con);
        DataSet ds = new DataSet();
        adtp.Fill(ds);
         DataList1.DataSource = ds.Tables[0];
            DataList1.DataBind();

    }

    protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
    {
        DataList1.EditItemIndex = -1;
        getdata();


    }

    protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
    {
        DataList1.EditItemIndex = e.Item.ItemIndex;
        getdata();
    }

    protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        int id = Convert.ToInt16(DataList1.DataKeys[e.Item.ItemIndex].ToString());
        TextBox txtname = (TextBox)e.Item.FindControl("txtname");
        DropDownList ddldept = (DropDownList)e.Item.FindControl("ddldept");
        DropDownList ddlsem = (DropDownList)e.Item.FindControl("ddlsem");
        RadioButtonList rbgen = (RadioButtonList)e.Item.FindControl("rbgender");
        SqlDataAdapter adpt = new SqlDataAdapter("select * from student where Id='" + id + "'", con);
        DataSet ds = new DataSet();
        adpt.Fill(ds);
        SqlCommandBuilder scb = new SqlCommandBuilder(adpt);
        ds.Tables[0].Rows[0]["name"] = txtname.Text;
        ds.Tables[0].Rows[0]["dept"] = ddldept.SelectedItem.ToString();
        ds.Tables[0].Rows[0]["sem"] = ddlsem.SelectedItem.ToString();
        ds.Tables[0].Rows[0]["gender"] = rbgen.SelectedItem.ToString();
        adpt.Update(ds);
        DataList1.EditItemIndex = -1;
        getdata();
    }

    protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        int id = Convert.ToInt16(DataList1.DataKeys[e.Item.ItemIndex].ToString());
        SqlDataAdapter adpt = new SqlDataAdapter("select * from student where Id='" + id + "'", con);
        SqlCommandBuilder scb = new SqlCommandBuilder(adpt);
        DataSet ds = new DataSet();
        adpt.Fill(ds);
        ds.Tables[0].Rows[0].Delete();
        adpt.Update(ds);
        getdata();
       
    }

    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "Insert")
        {
            TextBox txtnames = (TextBox)e.Item.FindControl("txtnameadd");
            DropDownList ddldepts = (DropDownList)e.Item.FindControl("ddldeptadd");
            DropDownList ddlsems = (DropDownList)e.Item.FindControl("ddlsemadd");
            RadioButtonList rbgens = (RadioButtonList)e.Item.FindControl("rbgenderadd");
            SqlDataAdapter adpt = new SqlDataAdapter("select * from student", con);
            DataTable dt = new DataTable();
            SqlCommandBuilder scb = new SqlCommandBuilder(adpt);
            adpt.Fill(dt);
            DataRow dr = dt.NewRow();
            dr["name"] = txtnames.Text;
            dr["dept"] = ddldepts.SelectedItem.ToString();
            dr["sem"] = ddlsems.SelectedItem.ToString();
            dr["gender"] = ddlsems.SelectedItem.ToString();
            dt.Rows.Add(dr);
            adpt.Update(dt);
            getdata();
        }
    }
}

Comments

Popular Post

Python - Scramble File