lauren brown

lauren brown

  • NA
  • 33
  • 93k

Will For Loop overwrite my database records?

Feb 9 2011 10:18 PM
Hi experts, I am doing a retrieving of timetable from my database. I have a total of records in the database and the system only populate out 4 records. 2 of the records have been overwrite by my For Loop. I need your help to prevent the For Loop for overwriting my codes.

 string tempDay = null;
            string tempTime = null;
            if (resultTable.Rows.Count > 0)
            {
                for (int i = 0; i < resultTable.Rows.Count; i++)
                {
                    //tempDay and tempTime is used to compare with the database values so that the timetable can be displayed correctly.
                    tempDay = resultTable.Rows[i][2].ToString();
                    tempTime = resultTable.Rows[i][4] + ConfigurationSettings.AppSettings["DASH"] + resultTable.Rows[i][5];

                    for (int a = 0; a < dr.Length; a++)
                    {
                        for (int b = 0; b < keys.Length; b++)
                        {
                            string day = keys[b].ColumnName.Substring(0, 3).ToString();

                            if (tempDay == day)
                            {
                                if (tempTime == dr[a][keys[0]].ToString())
                                {
                                    //additional loop to check whether there is V as one of the week type
                                    for (int z = 0; z < dr.Length; z++)
                                    {
                                        //check whether there is "V" in the database, supervisor dont want the V to be shown.
                                        
                                        if (resultTable.Rows[i][3].ToString() == ConfigurationSettings.AppSettings["EVERYWEEK"])
                                        {
                                            string result = ConfigurationSettings.AppSettings["WEEK"] + ConfigurationSettings.AppSettings["SPACE"] + resultTable.Rows[i][10].ToString() + ConfigurationSettings.AppSettings["DASH"] + resultTable.Rows[i][11].ToString() + ConfigurationSettings.AppSettings["NEWLINE"] + resultTable.Rows[i][6].ToString() + ConfigurationSettings.AppSettings["SPACE"] + resultTable.Rows[i][7].ToString() + ConfigurationSettings.AppSettings["NEWLINE"] + resultTable.Rows[i][8].ToString() + ConfigurationSettings.AppSettings["SPACE"] + resultTable.Rows[i][1].ToString();
                                            dr[a][keys[b]] = result;
                                        }
                                            // if there are no V, normally display the timetable out with the complimenting week type
                                        else
                                        {
                                            string result = ConfigurationSettings.AppSettings["WEEK"] + ConfigurationSettings.AppSettings["SPACE"] + resultTable.Rows[i][10].ToString() + ConfigurationSettings.AppSettings["DASH"] + resultTable.Rows[i][11].ToString() + ConfigurationSettings.AppSettings["SPACE"] + ConfigurationSettings.AppSettings["LEFT_BRACKET"] + resultTable.Rows[i][3].ToString() + ConfigurationSettings.AppSettings["RIGHT_BRACKET"] + ConfigurationSettings.AppSettings["NEWLINE"] + resultTable.Rows[i][6].ToString() + ConfigurationSettings.AppSettings["SPACE"] + resultTable.Rows[i][7].ToString() + ConfigurationSettings.AppSettings["NEWLINE"] + resultTable.Rows[i][8].ToString() + ConfigurationSettings.AppSettings["SPACE"] + resultTable.Rows[i][1].ToString();
                                            dr[a][keys[b]] = result;
                                        }

                                        
                                    }
                                    
                                    break;
                                }
                            }
                        }
                    }
                }
            }

Thanks in advance! 

Answers (3)