Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there,
I want to calculate total number of "present" word occurances in datagridview i have created datagridview dynamically .
here is my code:
C#
ClsConnection.Conn.Open();
            string sql_student = string.Format("select ADD_No, FName+' '+LName as Name from ADD_Master where ADD_No IN (select Student_Id from tbstudent_batchmaster where Batch_Identity=" + batchid + ")");
            SqlCommand cmd_stud = new SqlCommand(sql_student, ClsConnection.Conn);
            SqlDataReader stud_read = cmd_stud.ExecuteReader(CommandBehavior.CloseConnection);
            int rowcount = 0,totalpresent=0;
            while (stud_read.Read())
            {
                DataRow workRow = dt.NewRow();
                
                
                //dt.Rows.Add(stud_read[1].ToString());    
                workRow[0] = stud_read[1].ToString();
                for(int i=0;i<dates.Count;i++)
                {   
                    string sql_stud_att = "select student_id from tbbatch_att where student_id= " + stud_read[0] + " and batch_id=" + batchid + " and attendance_date='"+dates[i]+"'";
                    ClsConnection.Conn_repeat.Close();
                    ClsConnection.Conn_repeat.Open();
                    SqlCommand cmd_stud_att = new SqlCommand(sql_stud_att, ClsConnection.Conn_repeat);
                    SqlDataReader stud_read_att = cmd_stud_att.ExecuteReader(CommandBehavior.CloseConnection);
                    if (stud_read_att.HasRows)
                    {
                        //dt.Rows.Add("Present");
                        workRow[(i+1)] = "Present";
                        totalpresent++;
                    }
                    else
                    {
                        //dt.Rows.Add("Absent");
                        workRow[(i + 1)] = "Absent";
                    }
                    ClsConnection.Conn_repeat.Close();
                }
                
                dt.Rows.Add(workRow);
               
                rowcount++;
            }

            ClsConnection.Conn.Close();

plz help
Posted
Comments
Anuja Pawar Indore 27-Jan-12 7:09am    
Added pre tag

1 solution

Hi,

Why not phrase the query using Group By like this?

SELECT Count(Student_id) FROM tbbatch_att GROUP BY attendance_date

If you've a specific condition for date, make it this way:

SELECT COUNT(Student_id) FROM tbbatch_att GROUP BY attendance_date HAVING attendance_date IN (@Dates) --SET OF DATES



Having a SELECT query in a FOR loop is really frowned upon as you hit the database multiple times, when you have an elegant solution as above.

*Mark as answer if this solves. So that the question doesn't hang around Unanswered bunch anymore.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900