I have a gridview with CheckBox
inside it. When the user selects a row and clicks
on a button, a message is sent for the specific user.
How can this be optimized?
protected void btnSendSMSForSpeceficUser_Click(object sender, EventArgs e)
{
using (NoavaranModel.NoavaranEntities1 dbContext=new NoavaranModel.NoavaranEntities1())
{
int userId = Int32.Parse(Session["UserId"].ToString());
var query = (from p in dbContext.Users
where p.UserId == userId
select p.CountOfSMS).FirstOrDefault();
var query2 = (from p in dbContext.Users
where p.UserId == userId
select p).FirstOrDefault();
int j = 0,sendCount=0;
for (int i = 0; i <= grdStudents.Rows.Count - 1; i++)
{
string MobileNumbers = grdStudents.Rows[i].Cells[2].Text;
int stID = Int32.Parse(grdStudents.Rows[i].Cells[0].Text);
var query3 = (from p in dbContext.Students
where p.Id == stID
where p.IsRecivedSMS==false
select p).FirstOrDefault();
GridViewRow row = grdStudents.Rows[i];
CheckBox Ckbox = (CheckBox)row.FindControl("chkSelectStudents");
if (Ckbox.Checked)
{
j++;
if (j <= query) {
sendCount++;
Utility.SendMessageForStudents(MobileNumbers, txtMessage.Text);
query3.IsRecivedSMS = true;
}
}
}
query2.CountOfSMS = query.Value - sendCount;
dbContext.SaveChanges();
ListBox1.Items.Add(sendCount.ToString());
}
}