Click here to Skip to main content
15,560,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Emp.aspx.cs" Inherits="Test17716.Emp" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tr>
                    <td>Name</td>
                    <td>
                        <asp:TextBox ID="txtname" runat="server"></asp:TextBox></td>
                </tr>
                <tr>
                    <td>Address</td>
                    <td>
                        <asp:TextBox ID="txtaddress" runat="server"></asp:TextBox></td>
                </tr>
                <tr>
                    <td>Qualification</td>
                    <td>
                        <asp:DropDownList ID="ddlqualification" runat="server">
                            <%-- <asp:ListItem Text="mca" Value="1"></asp:ListItem>
                            <asp:ListItem Text="mba" Value="2"></asp:ListItem>
                            <asp:ListItem Text="b.tech" Value="3"></asp:ListItem>
                            <asp:ListItem Text="bca" Value="4"></asp:ListItem>--%>
                        </asp:DropDownList></td>
                </tr>
                <tr>
                    <td>Gender</td>
                    <td>
                        <asp:RadioButtonList ID="rdbGender" runat="server" RepeatColumns="2">
                            <%--<asp:ListItem Text="MALE" Value="1"></asp:ListItem>--%>
                        </asp:RadioButtonList></td>
                </tr>
                <tr>
                    <td>Hobies</td>
                    <td>
                        <asp:CheckBoxList ID="Chklist" runat="server" RepeatColumns="3"></asp:CheckBoxList></td>
                </tr>
                <tr>
                    <td>Files</td>
                    <td>
                        <asp:FileUpload ID="fufile" runat="server"></asp:FileUpload></td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <asp:Button ID="btnsave" runat="server" Text="Save" OnClick="btnsave_Click" /></td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <asp:GridView ID="grd" DataKeyNames="Eid" runat="server"  OnRowCommand="grd_RowCommand" AutoGenerateColumns="False">
                            <Columns>

                                <asp:TemplateField HeaderText="Name">
                                    <ItemTemplate>
                                        <a href="employeeDtaials.aspx?Eid=<%#Eval("Eid") %>"><%#Eval("Name") %></a>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Address">
                                    <ItemTemplate>
                                        <%#Eval("Address") %>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Qualification">
                                    <ItemTemplate>
                                        <%#Eval("QName") %>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Gender">
                                    <ItemTemplate>
                                        <%#Eval("Gender") %>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Hobbies">
                                    <ItemTemplate>
                                        <%#Eval("Hobbies") %>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="FileName">
                                    <ItemTemplate>
                                        <%--<a href="/KB/answers/employeeDtaials.aspx"?Eid=<%#Eval("Eid") %>">--%>
                                            <img src="/Files/<%#Eval("FileName") %>" style="width: 50px">
                                        </a>

                                        &nbsp;&nbsp;

                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Action">
                                    <ItemTemplate>
                                        <asp:LinkButton ID="lnkEdit" Text="View" CommandArgument='<%#Eval("Eid") %>' CommandName="EDT" runat="server"></asp:LinkButton>
                                        <asp:LinkButton ID="lnkDelete" Text="Delete" CommandArgument='<%#Eval("Eid") %>' CommandName="DEL" runat="server"></asp:LinkButton>
                                        
                                    </ItemTemplate>

                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>
                    </td>
                </tr>


            </table>

        </div>
    </form>
</body>
</html>


CS Code goes here:

C#
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 Test17716
{
    public partial class Emp : System.Web.UI.Page
    {
        SqlCommand cmd;
        SqlDataAdapter da;
        SqlDataReader dr;
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["abc"].ConnectionString);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               Fill_Qualification();
               RdbBind();
               Hobies();
               fill_Grid();
            }
        }

        public void Fill_Qualification()
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("usp_Qualification_Select", con);
            cmd.CommandType = CommandType.StoredProcedure;
            DataSet ds = new DataSet();
            SqlDataAdapter sdr = new SqlDataAdapter(cmd);
            sdr.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                ddlqualification.DataValueField = "QID";
                ddlqualification.DataTextField = "QName";
                ddlqualification.DataSource = ds;
                ddlqualification.DataBind();
                ddlqualification.Items.Insert(0,new ListItem("--Select--","0"));
            }
            con.Close();
        }

        public void Hobies()
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("Myhobies", con);
            cmd.CommandType = CommandType.StoredProcedure;
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                Chklist.DataValueField = "HID";
                Chklist.DataTextField = "HobiesNAme";
                Chklist.DataSource = ds;
                Chklist.DataBind();

            }
            con.Close();
        }

        public void RdbBind()
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("GenderBind", con);
            cmd.CommandType = CommandType.StoredProcedure;
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                rdbGender.DataValueField = "GID";
                rdbGender.DataTextField = "GName";
                rdbGender.DataSource = ds;
                rdbGender.DataBind();
               
            }
            con.Close();
        }
        public void fill_Grid()
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("usp_Emp_Get", con);
            cmd.CommandType = CommandType.StoredProcedure;
            DataSet ds = new DataSet();
            SqlDataAdapter sdr = new SqlDataAdapter(cmd);
            sdr.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                grd.DataSource = ds;
                grd.DataBind();
            }
            con.Close();
        }

        protected void btnsave_Click(object sender, EventArgs e)
        {
            string HOB = "";
            for (int i = 0; i < Chklist.Items.Count; i++)
            {
                if (Chklist.Items[i].Selected == true)
                {
                    HOB += Chklist.Items[i].Text + ",";
                }
            }
            HOB = HOB.TrimEnd(',');

            string FN = Path.GetFileName(fufile.PostedFile.FileName);
            fufile.SaveAs(Server.MapPath("Files" + "\\" + FN));

            con.Open();
            SqlCommand cmd = new SqlCommand("usp_Emp_Insert", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Name", txtname.Text);
            cmd.Parameters.AddWithValue("@Address", txtaddress.Text);
            cmd.Parameters.AddWithValue("@Qualification", ddlqualification.SelectedValue);
            cmd.Parameters.AddWithValue("@GID", rdbGender.SelectedValue);
            cmd.Parameters.AddWithValue("@Hobbies", HOB);
            cmd.Parameters.AddWithValue("@FileName", FN);
            cmd.ExecuteNonQuery();
            con.Close();
            fill_Grid();

            txtaddress.Text = "";
            txtname.Text = "";

        }

        protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DEL")
            {
                  con.Open();
                  LinkButton lnkbtn = sender as LinkButton;
                  GridViewRow grdRow = lnkbtn.NamingContainer as GridViewRow;
                  string EmpId = grd.DataKeys[grdRow.RowIndex].Value.ToString();
                
                
                cmd = new SqlCommand("sp_Emp_Delete",con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Id", EmpId);
                
                cmd.ExecuteNonQuery();
                con.Close();
            }
            else
            {

            }

            }
           
        }

        
    }



Please help me. I am confused how to use GridView Row command event.

What I have tried:

I have used grid view row command event but i am unable to delete the row.
Posted
Updated 22-Aug-16 8:38am
Comments
Karthik_Mahalingam 20-Aug-16 8:38am    
Please dont dump the code.
add only the portion of code which is related to your issue, so that users will look into it.

Ok so what you need to do is inside your method
C#
protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)

..after the delete is executed, immediately re-bind the grid control to the updated data set.

Your page load only binds the first time page loads, so this is ok for users seeing the page for the first time. Postback commands like you "DEL" should refresh controls within before the method exits.
 
Share this answer
 
Comments
Adityakumar2318 24-Aug-16 14:10pm    
THanks. Issue is resolved.
Call your fill_Grid() method after executing delete to reflect the changes you've made in your GridView.
C#
protected void grd_RowCommand(object sender, GridViewCommandEventArgs e){
            if (e.CommandName == "DEL")
            {
                  con.Open();
                  LinkButton lnkbtn = sender as LinkButton;
                  GridViewRow grdRow = lnkbtn.NamingContainer as GridViewRow;
                  string EmpId = grd.DataKeys[grdRow.RowIndex].Value.ToString();
                
                
                cmd = new SqlCommand("sp_Emp_Delete",con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Id", EmpId);
                
                cmd.ExecuteNonQuery();
                con.Close();

                fill_Grid();//this will repopulate your grid with the changes
            }
}          


I would also suggest you to debug your code. Set a break point at RowCommand event, then step into your codes to figure out what's happening.
 
Share this answer
 
v2
Comments
Adityakumar2318 24-Aug-16 14:10pm    
THanks. Issue is resolved.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900