无法正确循环发送短信,错误地向一个人发送多条短信
Not able to send sms in loop properly, sending multiple sms to single person by mistake
我正在尝试使用 asp.net 中的 gridview 向缺席者号码发送短信。功能如下:
- 在 class 中的所有学生中,由于用户未选中,学生将被标记为缺席,反之亦然。
- 现在的问题是,当我试图取消选中学生并提交时,即使只有一名学生缺席,它也会立即向我发送大约 50 条消息。它应该只向缺席者发送消息。
请帮助我参考我的以下代码。提前致谢:
protected void InsertAttendence()
{
DateTime systemdate2 = DateTime.Today.Date;
foreach (GridViewRow row in gvStudents.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkAttendance = row.FindControl("chkAttendence") as CheckBox;
string attendanceStatus = chkAttendance.Checked ? "Present" : "Absent";
string Class = ((row.FindControl("lblclass") as Label).Text.Trim());
string StudentName = ((row.FindControl("lblname") as Label).Text.Trim());
string STSNO = ((row.FindControl("lblstsno") as Label).Text.Trim());
string Mobile = ((row.FindControl("lblmobile") as Label).Text.Trim());
string Division = ((row.FindControl("lbldiv") as Label).Text.Trim());
//string sttendenceDate = (this.txtDate.Text.Trim());
string constring = ConfigurationManager.ConnectionStrings["stjosephconnect"].ConnectionString;
using (SqlConnection conInsert = new SqlConnection(constring))
{
try
{
string query = "INSERT INTO studentattendance(attdate, stsno, name, mobile, class, div, attendance, remarks, status, rem) "
+ "VALUES(@attdate, @stsno, @name, @mobile, @class, @div, @attendance, @remarks, @status, @rem)";
using (SqlCommand cmd = new SqlCommand(query, conInsert))
{
conInsert.Open();
cmd.Parameters.AddWithValue("@attdate", systemdate2);
cmd.Parameters.AddWithValue("@stsno", STSNO);
cmd.Parameters.AddWithValue("@name", StudentName);
cmd.Parameters.AddWithValue("@mobile", Mobile);
cmd.Parameters.AddWithValue("@class", Class);
cmd.Parameters.AddWithValue("@div", Division);
cmd.Parameters.AddWithValue("@attendance", attendanceStatus);
cmd.Parameters.AddWithValue("@remarks", "Test Remarks!");
cmd.Parameters.AddWithValue("@status", "1");
cmd.Parameters.AddWithValue("@rem", "0");
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Data saved successfully, Thank-You!');", true);
chkAttendance.Checked = false;
using (SqlConnection GetAbsentCon = new SqlConnection(constring))
{
string TodaysDate = DateTime.Now.ToString("yyyy-MM-dd");
GetAbsentCon.Open();
using (SqlCommand GetAbsentCmd = new SqlCommand("select mobile from studentattendance where convert(varchar(10), attdate, 120) = '" + TodaysDate + "' and attendance='Absent'", GetAbsentCon))
{
SqlDataReader dr = GetAbsentCmd.ExecuteReader();
while (dr.Read())
{
numbers = dr["mobile"].ToString().TrimStart('0');
SendSMS();
}
GetAbsentCon.Close();
}
}
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('There was some error in connection');", true);
}
}
}
catch (Exception ex)
{
Response.Write("<script language='javascript'>alert('" + Server.HtmlEncode(ex.Message.ToString()) + "')</script>");
}
finally
{
conInsert.Close();
}
}
}
}
}
请帮我摆脱这种尴尬的局面..
可能您应该在此 Sql 语句中为 studentId 或 StudentNo 添加条件。
using (SqlCommand GetAbsentCmd = new SqlCommand("select mobile from studentattendance where convert(varchar(10), attdate, 120) = '" + TodaysDate + "' and attendance='Absent'", GetAbsentCon))
我正在尝试使用 asp.net 中的 gridview 向缺席者号码发送短信。功能如下: - 在 class 中的所有学生中,由于用户未选中,学生将被标记为缺席,反之亦然。 - 现在的问题是,当我试图取消选中学生并提交时,即使只有一名学生缺席,它也会立即向我发送大约 50 条消息。它应该只向缺席者发送消息。 请帮助我参考我的以下代码。提前致谢:
protected void InsertAttendence()
{
DateTime systemdate2 = DateTime.Today.Date;
foreach (GridViewRow row in gvStudents.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkAttendance = row.FindControl("chkAttendence") as CheckBox;
string attendanceStatus = chkAttendance.Checked ? "Present" : "Absent";
string Class = ((row.FindControl("lblclass") as Label).Text.Trim());
string StudentName = ((row.FindControl("lblname") as Label).Text.Trim());
string STSNO = ((row.FindControl("lblstsno") as Label).Text.Trim());
string Mobile = ((row.FindControl("lblmobile") as Label).Text.Trim());
string Division = ((row.FindControl("lbldiv") as Label).Text.Trim());
//string sttendenceDate = (this.txtDate.Text.Trim());
string constring = ConfigurationManager.ConnectionStrings["stjosephconnect"].ConnectionString;
using (SqlConnection conInsert = new SqlConnection(constring))
{
try
{
string query = "INSERT INTO studentattendance(attdate, stsno, name, mobile, class, div, attendance, remarks, status, rem) "
+ "VALUES(@attdate, @stsno, @name, @mobile, @class, @div, @attendance, @remarks, @status, @rem)";
using (SqlCommand cmd = new SqlCommand(query, conInsert))
{
conInsert.Open();
cmd.Parameters.AddWithValue("@attdate", systemdate2);
cmd.Parameters.AddWithValue("@stsno", STSNO);
cmd.Parameters.AddWithValue("@name", StudentName);
cmd.Parameters.AddWithValue("@mobile", Mobile);
cmd.Parameters.AddWithValue("@class", Class);
cmd.Parameters.AddWithValue("@div", Division);
cmd.Parameters.AddWithValue("@attendance", attendanceStatus);
cmd.Parameters.AddWithValue("@remarks", "Test Remarks!");
cmd.Parameters.AddWithValue("@status", "1");
cmd.Parameters.AddWithValue("@rem", "0");
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Data saved successfully, Thank-You!');", true);
chkAttendance.Checked = false;
using (SqlConnection GetAbsentCon = new SqlConnection(constring))
{
string TodaysDate = DateTime.Now.ToString("yyyy-MM-dd");
GetAbsentCon.Open();
using (SqlCommand GetAbsentCmd = new SqlCommand("select mobile from studentattendance where convert(varchar(10), attdate, 120) = '" + TodaysDate + "' and attendance='Absent'", GetAbsentCon))
{
SqlDataReader dr = GetAbsentCmd.ExecuteReader();
while (dr.Read())
{
numbers = dr["mobile"].ToString().TrimStart('0');
SendSMS();
}
GetAbsentCon.Close();
}
}
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('There was some error in connection');", true);
}
}
}
catch (Exception ex)
{
Response.Write("<script language='javascript'>alert('" + Server.HtmlEncode(ex.Message.ToString()) + "')</script>");
}
finally
{
conInsert.Close();
}
}
}
}
}
请帮我摆脱这种尴尬的局面..
可能您应该在此 Sql 语句中为 studentId 或 StudentNo 添加条件。
using (SqlCommand GetAbsentCmd = new SqlCommand("select mobile from studentattendance where convert(varchar(10), attdate, 120) = '" + TodaysDate + "' and attendance='Absent'", GetAbsentCon))