I'm trying to promote students from previous class to a new one. For this I'm using a gridview with checkboxes to select the students and update only selected student details in the database with the new class. Now, the problem is that when I click promote only the first row details is getting updated to new class and the rest are not. Even though I'm using foreach
so that all the selected students get updated. I'm stuck! Kindly help me.
Here is my aspx markup:
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<div class="content-wrapper">
<section class="content-header">
<h1>Student Promotion
<small>Administrator</small>
</h1>
<ol class="breadcrumb">
<li><a href="#"><i class="fa fa-dashboard"></i>Home</a></li>
<li class="active">Student Promotion</li>
</ol>
</section>
<section class="content">
<div class="box box-danger">
<div class="box-header with-border">
<h3 class="box-title">Student Promotion Manager</h3>
</div>
<div class="box-body">
<div class="col-md-6">
<div class="form-group">
<label>Select Standard You Wanna Promote</label>
<asp:DropDownList ID="drpclass" runat="server" CssClass="btn btn-default form-control" OnSelectedIndexChanged="drpclass_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
</div>
</div>
<div class="col-md-12" style="overflow: scroll; height: 200px">
<div class="form-group">
<asp:GridView ID="GridView1" runat="server" CssClass="table table-bordered table-condensed table-hover table-responsive"
GridLines="None">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkboxSelectAll" AutoPostBack="true" OnCheckedChanged="checkmobilenumbers" runat="server" onclick="checkAll(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkPromote" runat="server" OnCheckedChanged="checkmobilenumbers" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sl. No.">
<ItemTemplate>
<%#Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</div>
</div>
<div class="box-footer">
<div class="pull-right">
<asp:Button ID="btnPromote" runat="server" CssClass="btn btn-default" OnClientClick="javascript:return TestCheckBox();" Text="Promote Selected Students" OnClick="PromoteSelectedStudents" />
</div>
</div>
</div>
</section>
</div>
</asp:Content>
And here is my c# code behind:
protected void PromoteSelectedStudents(object sender, EventArgs e)
{
string strConnString = ConfigurationManager.ConnectionStrings["stjosephconnect"].ConnectionString;
if (drpclass.SelectedIndex.ToString() != "0")
{
try
{
foreach (GridViewRow row in GridView1.Rows)
{
//if eligible start promote process
CheckBox chkRow = (CheckBox)row.FindControl("chkPromote");
bool isSelected = (row.FindControl("chkPromote") as CheckBox).Checked;
if (chkRow.Checked || isSelected)
{
string nowDate = DateTime.Now.AddDays(365).ToString();
string classdrp = drpclass.SelectedItem.ToString();
//Int16 clsupdt = Convert.ToInt16(classdrp + 1);
string Student = row.Cells[4].Text.Trim();
string dateCreated = DateTime.Now.ToShortDateString();
string updtQuery = "update studentregistration set class = (Select promotable_to from addclass where classname = '" + classdrp + "'), LastPromotedOn = convert(varchar, GETDATE(), 103), NextPossiblePromotion = DATEADD(year, 1, NextPossiblePromotion) where class = '" + classdrp + "' and name = '" + Student + "'";
using (SqlConnection UpdtCon = new SqlConnection(strConnString))
using (SqlCommand UpdtCmd = new SqlCommand(updtQuery, UpdtCon))
{
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
message = "Selected Students are promoted to the new class & the next promotion date is: " + nowDate;
script += message;
script += "')};";
ClientScript.RegisterStartupScript(this.GetType(), "Message From St.Joseph", script, true);
ga.bindClassDropDown(drpclass);
GridView1.DataSource = "";
GridView1.DataBind();
}
}
}
}
}
catch (Exception ex)
{
Response.Write("<script language='javascript'>alert('" + Server.HtmlEncode(ex.Message.ToString()) + "')</script>");
}
}
}
Please go through my code and any help would be appreciated.