Create,Read,Update,Delete CRUD Operations using C#,Asp.Net,SQL Server Part 3

Bangalore: Hi Friends those who are not checked the part 1 and part 2 articles please go through it. because its continuation from that.
Part 1
Part 2
Here we are going to explain about Search and Delete operations.
First we will fetch all data from the corresponding table and on button click we will bind those data into the grid view. and there we have provided a radio button and a Delete button you can select your raw using radio button and on delete button click it will delete that row.
So design your form like this two buttons and a gridview.
For selecting only one raw at a time we have used a java script.
check the code for Delete.aspx page

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Delete.aspx.cs" Inherits="CRUD.Delete" %>

<!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>Delete</title>
    <script language="javascript" type="text/javascript">
        function SelectSingleRadiobutton(rdbtnid) {
            var rdBtn = document.getElementById(rdbtnid);
            var rdBtnList = document.getElementsByTagName("input");
            for (i = 0; i < rdBtnList.length; i++) {
                if (rdBtnList[i].type == "radio" && rdBtnList[i].id != rdBtn.id) {
                    rdBtnList[i].checked = false;
                }
            }
        }
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button id="btnFill" Text="Fill" runat="server" onclick="btnFill_Click" />
    <asp:Button id="btnDelete" Text="Delete" runat="server" onclick="btnDelete_Click"/>
    </div>
    <div>
     <div>
    <asp:GridView runat="server" ID="gdvSearch" DataKeyNames="id"
    AutoGenerateColumns="false" PageSize="6" BackColor="White"  Width="100%"
    BorderColor="Black" BorderStyle="Ridge" BorderWidth="2px" CellPadding="8" CellSpacing="1"
     ForeColor="#330199" GridLines="None" HeaderStyle-BorderColor="Black"
            RowStyle-BackColor="White" >
     <Columns>
     <asp:TemplateField ItemStyle-Width="50px" >
     <ItemTemplate>
     <asp:RadioButton ID="rdgridselect" runat="server" OnClick="javascript:SelectSingleRadiobutton(this.id)"/>
     </ItemTemplate>

     </asp:TemplateField>
     <asp:BoundField DataField="username" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="100px"  HeaderText="User Name" >
     </asp:BoundField>

     <asp:BoundField DataField="email" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="100px" ReadOnly="true" HeaderText="email">
     </asp:BoundField>
     <asp:BoundField DataField="company" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="100px" ReadOnly="true" HeaderText="company" ></asp:BoundField>

     <asp:BoundField DataField="mobile" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="100px" ReadOnly="true" HeaderText="mobile" ></asp:BoundField>
     
     </Columns>

    </asp:GridView>
    </div>
    </div>
    </form>
</body>
</html>

So In code behind on Fill button click we will fetch all data and Bind it with our GridView.
an on button delete click we will get the ID from grid view we have declared DataKeyNames="id"
in our gridview so we will get corresponding id as per the radio button click.
Check the code behind Aspx.cs code.

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;
using System.IO;

namespace CRUD
{
    public partial class Delete : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public string getConstring()
        {
            string constr = ConfigurationManager.ConnectionStrings["constrng"].ConnectionString;
            return constr;
        }

        protected void btnFill_Click(object sender, EventArgs e)
        {
            string str = getConstring();
            SqlConnection con = new SqlConnection(str);
            con.Open();
            SqlCommand cmd = new SqlCommand("Select * from crud", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                gdvSearch.DataSource = dt;
                gdvSearch.DataBind();
                ViewState["file"] = dt;

            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in gdvSearch.Rows)
            {
                if (((CheckBox)row.FindControl("rdgridselect")).Checked)
                {

                    int id = Convert.ToInt32(gdvSearch.DataKeys[row.RowIndex].Value);
                    string str = getConstring();
                    SqlConnection con = new SqlConnection(str);
                    con.Open();
                    SqlCommand cmd = new SqlCommand("delete from crud where id=" + id, con);
                    int result = cmd.ExecuteNonQuery();
                    if (result == 1)
                    {
                        SqlCommand cmds = new SqlCommand("Select * from crud", con);
                        SqlDataAdapter da = new SqlDataAdapter(cmds);
                        DataTable dt = new DataTable();
                        da.Fill(dt);
                        gdvSearch.DataSource = dt;
                        gdvSearch.DataBind();
                    }
                }
            }
        }
    }
}


Please write to us If you are not able to understand any part of our code..

0 comments: