Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Actually i am working with online attendance management system ,
but i am facing a little problem.

I fetch all record from database and display in gridview but the problem is when i am trying to display count value with each row of gridview.

What I have tried:

<pre>void gvattendcount()
    {

        SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        foreach (GridViewRow row in GVattend.Rows)
        {
            string rollno = (((Label)row.FindControl("lblroll")).Text);
            string attendlecture = (((Label)row.FindControl("lblattend")).Text);

            string query = "SELECT COUNT(*) as attendlecture FROM class8_attend where  rollno ='" + rollno + "' AND present ='p'";

            DataTable dtAdmin = new DataTable();
            SqlDataAdapter da;
            da = new SqlDataAdapter(query, cnn);
            da.Fill(dtAdmin);

            if (dtAdmin.Rows.Count > 0)
            {
                using (SqlCommand cmd = new SqlCommand(query, cnn))
                {
                    cnn.Open();
                    Int32 count = (Int32)cmd.ExecuteScalar();
                    attendlecture = count.ToString();
                    cnn.Close();
                }

                
            }
        }
    }




CSS-
<asp:TemplateField  HeaderText="ATTENDED LECTURES" >
           <ItemTemplate>
           <center>
            <asp:Label ID="lblattend" runat="server" Text=""></asp:Label>
           </center>
           </ItemTemplate>
           </asp:TemplateField>
Posted
Updated 14-Mar-17 23:17pm
Comments
Graeme_Grant 15-Mar-17 3:07am    
You posted the same question here less than an hour ago: https://www.codeproject.com/Questions/1176552/How-to-count-each-record-from-database-and-display

Please do not ask the same question repeatedly.
ADI@345 15-Mar-17 3:12am    
Actually, I solved that problem. This is another problem that I am facing right now.

You have missed assign value of the column using Bind.
<asp:TemplateFiel>
<ItemTemplate>
        <asp:Label ID="lblattend" runat="server" Text='<%# Bind("attendlecture") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateFiel>
 
Share this answer
 
v2
string attendlecture = (((Label)row.FindControl("lblattend")).Text);

// ....

attendlecture = count.ToString();


You need to appreciate the difference between "value" types and "reference" types. When you load the value of lblAttend into attendlecture that is all you are doing, that variable is not a reference to the lblattent.Text property, it is simply a copy of it, so when you update it you're only overwriting the copy. You need to do this instead

Label lblattend = (Label)row.FindControl("lblattend");

// ....

lblattend.Text = count.ToString();
 
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