**Hi Respected Sir/Ma'am,
There are four columns in an Employee table, EmpID,EmpName,Address,Status. I'd like to update the status column from front by CheckBox. In below given, code I'm experiencing 'Index-out of range' exception, please help. Please note that Emp_Status is type of integer in this case. However, if I update Emp_Status row by row in edit mode. In this case, checkbox is checked where Emp_Status having 1 value, and unchecked where is 0. My requirement is, if user check/uncheck either one checkbox or multiple across paging, then on this basis value shall be updated in database as well.
I am not a software developer, I am just a learner, never worked in IT so far, so just learning. Please help me out. **
Database:-
CREATE TABLE [dbo].[Employee](
[Employee_ID] [int] IDENTITY(1,1) NOT NULL,
[Employee_Name] [varchar](50) NULL,
[Employee_Address] [varchar](100) NULL,
[Emp_Status] [int] NULL,
PRIMARY KEY CLUSTERED
(
[Employee_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON
Design:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://ift.tt/lH0Osb">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>Employee Name: </td>
<td><asp:TextBox ID="txt_EmployeeName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Employee Address: </td>
<td><asp:TextBox ID="txt_EmployeeAddress" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td><asp:Button ID="btn_Submit" runat="server" Text="Submit" OnClick="btn_Submit_Click" style="width: 61px" /></td>
</tr>
<tr>
<td><asp:LinkButton ID="Link_Search" runat="server" Text="Go For Search"></asp:LinkButton></td>
</tr>
<tr>
<td>Enter Name or Email ID for Search: </td>
<td><asp:TextBox ID="txt_Search" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td><asp:Button ID="btn_Search" runat="server" Text="Search" OnClick="btn_Search_Click" /></td>
</tr>
<tr>
<td>
<asp:GridView ID="GV_Product" runat="server" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" AutoGenerateColumns="false" ShowFooter="true" OnRowEditing="GV_Product_RowEditing" OnRowUpdating="GV_Product_RowUpdating" OnRowDeleting="GV_Product_RowDeleting" OnRowCancelingEdit="GV_Product_RowCancelingEdit" OnRowCommand="GV_Product_RowCommand" AllowPaging="true" PageSize="5" OnPageIndexChanging="GV_Product_PageIndexChanging">
<AlternatingRowStyle BackColor="Gainsboro" />
<Columns>
<asp:TemplateField HeaderText="Employee ID">
<ItemTemplate>
<asp:Label ID="lbl_ID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Employee_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Employee Name">
<ItemTemplate>
<asp:TextBox ID="txt_Name" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Employee_Name") %>'></asp:TextBox>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEdit_Name" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Employee_Name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAdd_Name" runat="server"></asp:TextBox>
<%--<asp:RequiredFieldValidator ID="txtName" runat="server" ControlToValidate="txtAdd_Name" ErrorMessage="Please enter Employee Name"></asp:RequiredFieldValidator>--%>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Employee Address">
<ItemTemplate>
<asp:TextBox ID="txt_Address" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Employee_Address") %>'></asp:TextBox>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEdit_Address" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Employee_Address") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAdd_Address" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" />
<asp:Button ID="btnDelete" runat="server" Text="Delete" CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete this record?')" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="btnUpdate" runat="server" Text="Update" CommandName="Update" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" />
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lblAdd" runat="server" Width="100px" Text="Add" CommandName="ADD"></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:CheckBox ID="ChkBox" runat="server" Checked='<%# GetStatus(DataBinder.Eval(Container.DataItem,"Emp_Status").ToString() )%>' AutoPostBack="true"
Text='<%# Eval("Emp_Status").ToString()=="1"?"Active":"Inactive" %>' OnCheckedChanged="ChkBox_CheckedChanged"
/>
</ItemTemplate>
<%--<EditItemTemplate>
<asp:CheckBox ID="EditChkBox" runat="server" Checked='<%# GetStatus(DataBinder.Eval(Container.DataItem,"Emp_Status").ToString() )%>' Text='<%# Eval("Emp_Status").ToString()=="1"?"Active":"Inactive" %>'/>
</EditItemTemplate>--%>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Code:-
protected void ChkBox_CheckedChanged(object sender, EventArgs e)
{
CheckBox ChkBox = (CheckBox)sender;
//int eID = -1;
//eID = Convert.ToInt32(GV_Product.DataKeys[((GridViewRow)ChkBox.NamingContainer).RowIndex].Value);
string sQuery = "UPDATE Employee SET Emp_Status = @Status WHERE Employee_ID = @ID";
string consString = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
using (SqlConnection conn = new SqlConnection(consString))
{
SqlCommand com = new SqlCommand(sQuery, conn);
com.Parameters.Add("@Status", SqlDbType.Int).Value =0;
com.Parameters.Add("@ID", SqlDbType.Int).Value = eID;
conn.Open();
com.ExecuteNonQuery();
}
BindGV();
}
protected void GV_Product_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GV_Product.PageIndex = e.NewPageIndex;
BindGV();
}
protected bool GetStatus(string str)
{
if (str=="1")
{
return true;
}
else
{
return false;
}
}
protected void BindGV()
{
string Table_Name = "Employee";
BAL obj = new BAL();
DataTable dt = obj.Select(Table_Name);
GV_Product.DataSource = dt;
GV_Product.DataBind();
}
Aucun commentaire:
Enregistrer un commentaire