Sending a message based on selected row 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 &lt;= 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 &lt;= query) {
                        sendCount++;
                        Utility.SendMessageForStudents(MobileNumbers, txtMessage.Text);
                        query3.IsRecivedSMS = true;
                    }
                }
            }
            query2.CountOfSMS = query.Value - sendCount;
            dbContext.SaveChanges();
            ListBox1.Items.Add(sendCount.ToString());
        }
    }
I'm not sure of everything (worked with notepad) but:

protected void btnSendSMSForSpeceficUser_Click(object sender, EventArgs e)
{
    using (NoavaranModel.NoavaranEntities1 dbContext = new NoavaranModel.NoavaranEntities1())
    {
        var userId = int.Parse(Session["UserId"].ToString());

        var user = dbContext.Users.FirstOrDefault(p =&gt; p.UserId == userId);
        var countOfSms = user.CountOfSMS.Value;
        var j = 0;
        foreach (var row in grdStudents.Rows)
        {
            var cells = row.Cells;
            var MobileNumbers = cells[2].Text;
            var stID = int.Parse(cells[0].Text);

            var student = dbContext.Students.FirstOrDefault(p =&gt; p.Id == stID &amp;&amp; !p.IsRecivedSMS);

            var Ckbox = (CheckBox)row.FindControl("chkSelectStudents");
            if (Ckbox.Checked)
            {
                j++;
                if (j &lt;= countOfSms) {
                    Utility.SendMessageForStudents(MobileNumbers, txtMessage.Text);
                    student.IsRecivedSMS = true;

                    user.CountOfSMS = countOfSms - 1;
                }
            }
        }

        dbContext.SaveChanges();
        ListBox1.Items.Add(sendCount.ToString());
    }
}


This is a cleaner version of your code with less query (the first was unneccesary) and inside the loop you are making a lot of small query which can be really bad but first you have to modify your code to not work directly with the context in you page code but through an interface (first iterate through the rows and get all data then pass them to the worker (which can work with the context) and return the result &amp; update the UI).