Insert multiple data in database from gridview using Select All button
<asp:GridView ID="grdSelectEmp" runat="server" AllowPaging="false"AutoGenerateColumns="False" CellPadding="4" EmptyDataText="No Record Exists"
ForeColor="#333333" GridLines="Both" Height="450px" Width="100%"
onrowcommand="grdSelectEmp_RowCommand">
<AlternatingRowStyle BackColor="White" HorizontalAlign="Left" />
<Columns>
<asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderText="Employee ID" ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblEmpId" runat="server" ForeColor="#333333" Text='<%# Eval("EmpCode")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderText="Employee Name" ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblEmpName" runat="server" ForeColor="#333333" Text='<%# Eval("EmapName")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderText="Designation" ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblDesignation" runat="server" ForeColor="#333333" Text='<%# Eval("DesignationName")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left">
<HeaderTemplate>
<asp:Button ID="btnSelectAll" runat="server" CommandName="SelectAll_Clicked" Text="Select All" Width="80px" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkEmp" runat="server" AutoPostBack="true" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White"
HorizontalAlign="Left" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" HorizontalAlign="Left" />
</asp:GridView>
.cs Code
protected void btnSendNotification_Click(object sender, EventArgs e)
{
try
{
lblError.Visible = false;
foreach (GridViewRow grdRow in grdSelectEmp.Rows)
{
if (((CheckBox)grdRow.FindControl("chkEmp")).Checked == true)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ToString());
conn.Open();
SqlCommand cmd = new SqlCommand("proc_MeetingNotify", conn);
cmd.Parameters.AddWithValue("@Title", txtTitle.Text.Trim());
cmd.Parameters.AddWithValue("@MeetingDate", txtDate.Text.Trim());
cmd.Parameters.AddWithValue("@Location", txtLocation.Text.Trim());
cmd.Parameters.AddWithValue("@MeetingTime", txtTime.Text.Trim());
cmd.Parameters.AddWithValue("@EmpID", ((Label)grdRow.FindControl("lblEmpId")).Text.Trim());
cmd.Parameters.AddWithValue("@BranchID", ddlBranch.SelectedItem.Value);
cmd.Parameters.AddWithValue("@DepartmentID", ddlDepartment.SelectedItem.Value);
cmd.Parameters.AddWithValue("@EnteredBy", ViewState["user"].ToString());
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
conn.Close();
conn.Dispose();
}
}
lblError.Visible = true;
lblError.Text = "Notofication Has Send";
Insert_Notification();
}
catch (Exception ex)
{
throw ex;
}
}
No comments:
Post a Comment