Match database coloumn data with html table cell text
<p>Html is static build in aspx page. i give table id runat=server to tabel And acess table in server code.Now what i have data in databse coloumn start time (08:15:00), end time (09:00:00) ,StudentID='123'.lets say this data i have in databse.Now i fire select query and gets start hour (08) using substring.here i checking if hour and minute is match then make color green of start minute till end Minute.I have do Logic but makes me something wrong when runs the code. </p>

<pre><code>&lt;table id="tableAppointment" runat="server"&gt;
                            &lt;thead&gt;
                                &lt;tr&gt;
                                    &lt;td &gt;
                                    &lt;/td&gt;
                                    &lt;td&gt;
                                             Exam Time
                                    &lt;/td&gt;
                                    &lt;td &gt;
                                             Student Name
                                    &lt;/td&gt;
                                &lt;/tr&gt;
                            &lt;/thead&gt;
                            &lt;tbody&gt;
                                &lt;tr&gt;
                                    &lt;td &gt;
                                        08:00AM
                                    &lt;/td&gt;
                                    &lt;td&gt;
                                        00
                                    &lt;/td&gt;
                                    &lt;td&gt;
                                        &lt;span&gt;&lt;/span&gt;
                                    &lt;/td&gt;
                                &lt;/tr&gt;
                                &lt;tr&gt;
                                    &lt;td&gt;
                                    &lt;/td&gt;
                                    &lt;td&gt;
                                        15
                                    &lt;/td&gt;
                                    &lt;td&gt;
                                        &lt;span&gt;&lt;/span&gt;
                                    &lt;/td&gt;
                                &lt;/tr&gt;
                                &lt;tr&gt;
                                    &lt;td&gt;
                                    &lt;/td&gt;
                                    &lt;td&gt;
                                        30
                                    &lt;/td&gt;
                                    &lt;td&gt;
                                        &lt;span&gt;&lt;/span&gt;
                                    &lt;/td&gt;
                                &lt;/tr&gt;
                                &lt;tr&gt;
                                    &lt;td&gt;
                                    &lt;/td&gt;
                                    &lt;td&gt;
                                        45
                                    &lt;/td&gt;
                                    &lt;td&gt;
                                        &lt;span&gt;&lt;/span&gt;
                                    &lt;/td&gt;
                                &lt;/tr&gt;
                                                            &lt;tr&gt;
                                    &lt;td &gt;
                                        06:00PM
                                    &lt;/td&gt;
                                    &lt;td&gt;
                                        00
                                    &lt;/td&gt;
                                    &lt;td&gt;
                                        &lt;span&gt;&lt;/span&gt;
                                    &lt;/td&gt;
                                &lt;/tr&gt;
                                &lt;tr&gt;
                                    &lt;td&gt;
                                    &lt;/td&gt;
                                    &lt;td&gt;
                                        15
                                    &lt;/td&gt;
                                    &lt;td&gt;
                                        &lt;span&gt;&lt;/span&gt;
                                    &lt;/td&gt;
                                &lt;/tr&gt;
                                &lt;tr&gt;
                                    &lt;td&gt;
                                    &lt;/td&gt;
                                    &lt;td&gt;
                                        30
                                    &lt;/td&gt;
                                    &lt;td&gt;
                                        &lt;span&gt;&lt;/span&gt;
                                    &lt;/td&gt;
                                &lt;/tr&gt;
                                &lt;tr&gt;
                                    &lt;td&gt;
                                    &lt;/td&gt;
                                    &lt;td&gt;
                                        45
                                    &lt;/td&gt;
                                    &lt;td&gt;
                                        &lt;span&gt;&lt;/span&gt;
                                    &lt;/td&gt;
                                &lt;/tr&gt;
    &lt;/table&gt;

    protected void FixedAppointment()
        {
            strSQL = @"SELECT Student_NAME,From_Time,To_Time FROM STUDENT_MASTER";
            DataTable dataTableAppointments = null;
            dataTableAppointments = objSqlDbComm.ExecuteDatasetQuery(strSQL).Tables[0];

            string studentName = "";
            string fromTime = "";
            string toTime = "";
            string startHour = "";
            string startMinute = "";
            string endMinute = "";
            int rowspan = 0;
            foreach(DataRow dr in dataTableAppointments.Rows)
            {
                studentName = dr["Student_NAME"].ToString();//John
                fromTime = dr["From_Time"].ToString();//08:15:00
                toTime = dr["To_Time"].ToString();//09:00:00
                startHour = fromTime.Substring(0, 2);
                startMinute = fromTime.Substring(3, 2);
                endMinute = toTime.Substring(3, 2);
                rowspan = Convert.ToInt32(endMinute) / 15;//Each cell is 15 minute
                ReadHtmlTable(studentName , startHour, startMinute, rowspan);           
            }   

        }

    protected void ReadHtmlTable(string studentName , string startHour, string startMinute, int rowspan)
        {

            string rowHourText = "";
            string hour = "";
            string rowMinuteText = "";
            string matchedHour = "";
            for(int i = 0; i &lt;= tableAppointment.Rows.Count - 1; i++)
            {
                for(int j = 0; j &lt;= tableAppointment.Rows[i].Cells.Count - 1; j++)
                {       
                  //rowHourText is first cell innerText of tableAppointment ie:08:00AM
                    rowHourText = tableAppointment.Rows[i + 1].Cells[0].InnerText.Trim();

                    if(matchedHour == "")
                    {
                        hour = rowHourText.Substring(0, 2);
                    }
                    if(hour == startHour &amp;&amp; matchedHour == "")
                    {
                        matchedHour = hour;                 
                    }
                  //rowMinuteText is second cell innerText of tableAppointment ie:15,30,45
                    rowMinuteText = tableAppointment.Rows[i + 1].Cells[1].InnerText.Trim();
                    if(Convert.ToInt32(rowMinuteText) == Convert.ToInt32(startMinute) &amp;&amp; hour == startHour)
                    {
                            //here i checking if hour and minute is match
                         //then make color green of start minute till end Minute.
                        tableAppointment.Rows[i + 1].Cells[2].InnerText = studentName;
                        tableAppointment.Rows[i + 2].Cells.RemoveAt(2);
                        tableAppointment.Rows[i + 3].Cells.RemoveAt(2);
                        tableAppointment.Rows[i + 1].Cells[2].RowSpan = rowspan;
                        tableAppointment.Rows[i + 1].Cells[1].Attributes.Add("Class", "csstdgreen");
                        tableAppointment.Rows[i + 2].Cells[1].Attributes.Add("Class", "csstdgreen");
                        tableAppointment.Rows[i + 3].Cells[1].Attributes.Add("Class", "csstdgreen");
                        tableAppointment.Rows[i + 1].Cells[2].Attributes.Add("Class", "csstdgreen");            

                    }
                    else
                    {
                        break;
                    }
                }
            }
        }


    protected override void OnSaveStateComplete(EventArgs e)
        {
            FixedAppointment();
        }
</code></pre>

<p><img src="http://i.stack.imgur.com/qQVgf.png" alt="like this"></p>
