Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
Hello everyone,
my problem is more then i could chew im aware of it and there are no usefull info in google.

What i need is display and edit a class attendance chart for a year. I was thinking using image buttons with jquerry to display 5 status color for a students attend date cell but i can accept checkboxes for now. (yeah thinking big with little ability)

Now im in a fork that either i need to simplify the tables or get on with the repeater and solve the .net riddle in code-behind.

This is my Table[^]

There are some turkish here and there but its understandable i guess...

XML
<table>
        <tr>
            <th style="width: 120px">USERNAME\DAY</th>
            <asp:Repeater ID="repeatWeekdays" runat="server">
                <ItemTemplate>
                    <th style="width: 30px"><%# Container.DataItem %></th>
                </ItemTemplate>
            </asp:Repeater>
        </tr>
        <asp:Repeater ID="Repeater2" runat="server" OnItemDataBound="Repeater2_ItemDataBound" DataSourceID="SqlDataSource1">
            <FooterTemplate>
                </table>
            </FooterTemplate>
            <ItemTemplate>
                <tr>
                    <th style="width: 150px">
                        <asp:Label ID="lblUser" runat="server" Text='<%#Eval("iduser").ToString()+Eval("name").ToString() %>'></asp:Label><asp:HiddenField ID="hfUser" runat="server" Value='<%#Eval("iduser") %>' />
                    </th>
                    <asp:Repeater ID="repeatPresent" runat="server">
                        <ItemTemplate>
                            <th style="width: 30px">
                                <asp:HiddenField ID="hfAttendid" runat="server" Value='<%#Eval("idattendance") %>' />
                                <asp:TextBox Width="20px" Height="20px" Font-Size="X-Small" ID="txtPresent" runat="server" Text='<%# Eval("present") %>'></asp:TextBox>
                            </th>
                        </ItemTemplate>
                    </asp:Repeater>
                </tr>
            </ItemTemplate>
        </asp:Repeater>
        <asp:LinkButton ID="btnUpdate" runat="server" CausesValidation="True" Text="SAVE" OnClick="btnUpdate_Click" />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:sql354211ConnectionString %>" ProviderName="<%$ ConnectionStrings:sql354211ConnectionString.ProviderName %>" SelectCommand="SELECT DISTINCT attendance.iduser,name, idclasslesson FROM attendance inner join user on user.iduser=attendance.iduser WHERE (idclasslesson = 6)"></asp:SqlDataSource>


protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item ||
                     e.Item.ItemType == ListItemType.AlternatingItem)
            {
                HiddenField hfUserid = (e.Item.FindControl(&quot;hfUser&quot;) as HiddenField);
                MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings[&quot;sql354211ConnectionString&quot;].ConnectionString);
                MySqlCommand command = new MySqlCommand(&quot;select idattendance,present from attendance where iduser=@userid and idclasslesson=@classlessonid&quot;, conn);
                command.Parameters.AddWithValue(&quot;userid&quot;, hfUserid.Value.ToString());
                command.Parameters.AddWithValue(&quot;classlessonid&quot;, 6);
                try
                {
                    conn.Open();
                    MySqlDataAdapter dat = new MySqlDataAdapter(command);
                    DataTable ds = new DataTable();
                    dat.Fill(ds);
                    DataView dv = new DataView(ds);
                    Repeater r = ((Repeater)e.Item.FindControl(&quot;repeatPresent&quot;));
                    r.DataSource = dv;
                    r.DataBind();
                }
                catch { Response.Write("<h3>Something Bad Happened During database connection. Refresh the page </h3>"); }
                finally { conn.Close(); }
            }

        }




Its working so far. now i need to put square textbox control in nested repeater1 and be able to edit that text box in every corner of the screen.

repeater1 has 30 dayOfWeek columns to right and repeater2 has 40 student rows to bottom. it means 120 editable texbox controls for attendance.

i've come so far
What to do next ?

i populate rows with users and their ids, for every row i populate columns to the right, but how do i update the textboxes once its finished rendering page ?
Should i put hiddenfields near textboxes so that i can store their date id and present value ?
Posted
Updated 18-Oct-14 22:52pm
v6
Comments
Seems correct to me. Not sure of any other easier way.
burakdogmus 18-Oct-14 0:50am    
im just stuck how to change repeater1.datasource so that that row belong to right person.

i need something like this
onRepeater2RowUpdate()
{repeater1.datasource = "select present from attendance where userid="+repeater2.lastrow.lbl.value.tostring()+"**somemorequerrytolimit the class and lesson****;
}

ill quit whining and start to find the solution :D thanks...
See my answer.

To get the User Id, you can do like...
C#
Label lblUserId = ((Label)e.Item.FindControl("Label1"));
string userId = lblUserId.Text;

So, you can now use this userId in your query.
 
Share this answer
 
this demonstrates what my way accomplish. thanks for no help in here i wasted my time for nothing cuz it takes forever to update 4 users 5 day attendance.

need to start over and find a better solution.
i hope this code covers someone elses problem in future...
Conclusion: different querries for every repeter row and updating them seperately is a bad idea. Creating yearly 30 column tables seems more efficient right now.


When i first tried it my database was online but website was on my localhost. After publishing the site code works faster. I may put this as an article after i finished everything. Still opening and closing 90 connections to database in one mouse click seems unefficient to me. is this the way to do really ?


C#
protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item ||
             e.Item.ItemType == ListItemType.AlternatingItem)
    {
        HiddenField hfUserid = (e.Item.FindControl("hfUser") as HiddenField);
        MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["sql354211ConnectionString"].ConnectionString);
        MySqlCommand command = new MySqlCommand("select idattendance,present from attendance where iduser=@userid and idclasslesson=@classlessonid", conn);
        command.Parameters.AddWithValue("userid", hfUserid.Value.ToString());
        command.Parameters.AddWithValue("classlessonid", 6);
        try
        {
            conn.Open();
            MySqlDataAdapter dat = new MySqlDataAdapter(command);
            DataTable ds = new DataTable();
            dat.Fill(ds);
            DataView dv = new DataView(ds);
            Repeater r = ((Repeater)e.Item.FindControl("repeatPresent"));
            r.DataSource = dv;
            r.DataBind();
        }
        catch { Response.Write("<h3>Something Bad Happened During database connection. Refresh the page</h3>"); }
        finally { conn.Close(); }
        //Bind the add button here.
    }

}

protected void btnUpdate_Click(object sender, EventArgs e)
{
    foreach (RepeaterItem masteritem in Repeater2.Items)
    {
        Repeater r = ((Repeater)masteritem.FindControl("repeatPresent"));
        foreach (RepeaterItem item in r.Items)
        {
            string pr = ((TextBox)item.Controls[3]).Text;
            string id = ((HiddenField)item.Controls[1]).Value;
            if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
            {
                MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["sql354211ConnectionString"].ConnectionString);
                MySqlCommand command = new MySqlCommand("update attendance set present="+pr+" where idattendance="+id , conn);
                try
                {
                    conn.Open();
                    command.ExecuteNonQuery();
                    //////////testing place
                    /*
                    string a = ((TextBox)item.Controls[3]).Text;
                    string b = ((HiddenField)item.Controls[1]).Value;
                    Response.Write("<h2>" + a.ToString() + " - " + b.ToString() + "</h2>");*/
                }
                catch { }
                finally { conn.Close(); }
            }
        }
    }
}


XML
<table>
<asp:Repeater ID="Repeater2" runat="server" OnItemDataBound="Repeater2_ItemDataBound" DataSourceID="SqlDataSource1">
    <HeaderTemplate>
    </HeaderTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
    <ItemTemplate>
        <tr>
            <th style="width: 150px">
                <asp:Label ID="lblUser" runat="server" Text='<%#Eval("iduser").ToString()+Eval("name").ToString() %>'></asp:Label><asp:HiddenField ID="hfUser" runat="server" Value='<%#Eval("iduser") %>' />
            </th>
            <asp:Repeater ID="repeatPresent" runat="server">
                <ItemTemplate>
                    <th style="width: 30px">
                        <asp:HiddenField ID="hfAttendid" runat="server" Value='<%#Eval("idattendance") %>' />
                        <asp:TextBox Width="20px" Height="20px" Font-Size="X-Small" ID="txtPresent" runat="server" Text='<%# Eval("present") %>'></asp:TextBox></th>
                </ItemTemplate>
            </asp:Repeater>
        </tr>
    </ItemTemplate>
</asp:Repeater>
<asp:LinkButton ID="btnUpdate" runat="server" class="btn btn-primary" CausesValidation="True" Text="KAYDET" OnClick="btnUpdate_Click" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:sql354211ConnectionString %>" ProviderName="<%$ ConnectionStrings:sql354211ConnectionString.ProviderName %>" SelectCommand="SELECT DISTINCT attendance.iduser,name, idclasslesson FROM attendance inner join user on user.iduser=attendance.iduser WHERE (idclasslesson = 6)"></asp:SqlDataSource>


SQL
CREATE TABLE `attendance` (
  `idattendance` int(11) NOT NULL AUTO_INCREMENT,
  `iduser` int(11) DEFAULT NULL,
  `idclasslesson` int(11) DEFAULT NULL,
  `attenddate` date DEFAULT NULL,
  `present` int(11) DEFAULT '0',
  `excuse` varchar(45) COLLATE utf8_turkish_ci DEFAULT NULL,
  PRIMARY KEY (`idattendance`)
) ENGINE=InnoDB AUTO_INCREMENT=50 DEFAULT CHARSET=utf8 COLLATE=utf8_turkish_ci;
 
Share this answer
 
v3

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