0

I have no idea what had I coded wrong in my project, please help. Basically, I was creating a function for the program to check if a certain string is in a string. I have connected to Access Database, return a string such as "asdf,dfgh,ghjk" then the function will check if "dfgh" is in it. Here is my code:

private void RefreshAppliedLessonsTable()
    {

        Table_AppliedLessons.Rows.Clear();
        for (int i = 0; i < Table_Lessons.Rows.Count; i++)
        {

            string CursorLessonName = Table_Lessons.Rows[i].Cells[0].Value.ToString();
            string LessonID = functions.ReturnLessonID(CursorLessonName);
            string AppliedStudentsPerLesson = functions.ReturnAppliedStudents(LessonID);
            if (AppliedStudentsPerLesson.IndexOf(LTB_StudentID.Text) != -1)
            {
                string LessonName = string.Empty;
                string LessonCourse = string.Empty;
                string LessonTeacher = string.Empty;
                string Level = string.Empty;
                string Time = string.Empty;
                string QuotaLeft = string.Empty;
                string Price = string.Empty;
                LessonName = Table_Lessons.Rows[i].Cells[0].Value.ToString();
                LessonCourse = Table_Lessons.Rows[i].Cells[1].Value.ToString();
                LessonTeacher = Table_Lessons.Rows[i].Cells[2].Value.ToString();
                Level = Table_Lessons.Rows[i].Cells[3].Value.ToString();
                Time = Table_Lessons.Rows[i].Cells[4].Value.ToString();
                QuotaLeft = Table_Lessons.Rows[i].Cells[5].Value.ToString();
                Price = Table_Lessons.Rows[i].Cells[6].Value.ToString();

                Table_AppliedLessons.Rows.Add(new object[] { LessonName, LessonCourse, LessonTeacher, Level, Time, QuotaLeft, Price });
            }
        }

Then I'll execute this code when the form loads. However the datagridview table "Table_AppliedLessons" will never be populated. It is being confirmed that the database has the string in it. Anyone can help?


Answer to this problem:

As @SchlaWiener suggested I might have excluded some bugs in my program by default. After using the CTRL + ALT + E and check Common Language Runtime exceptions -> thrown checkbox. I saw that there is an error in the functions.ReturnLessonID(CursorLessonName); Where I've supplied the wrong parameters to the function, causing it to return a string.Empty; thus it cannot be added to the table. Thank you @SchlaWiener once again for the suggestion.

Finally, I would suggest having this modification of settings in visual studio such that you would not miss a "Hidden bug".

I'm sorry that I have to post this in my question since I cannot answer my own question due to my points.

  • Your question says "unknown error" but you don't mention anything related to an error? The fact that the data view is empty could simply mean it's A) not getting hooked up correctly or B) there is nothing to return. You need to give more information... – James May 07 '13 at 09:20
  • Put a breakpoint on "if (AppliedStudentsPerLesson.IndexOf(LTB_StudentID.Text) != -1)" and debug to it. What are the values of AppliedStudentsPerLesson and LTB_StudentID.Text? Can you see what the problem is? – Polyfun May 07 '13 at 09:21
  • Since your code is cleanly executing and not resulting in an exception means that the logic you have written is wrong somewhere. – Amit Rai Sharma May 07 '13 at 09:22

4 Answers4

0

IndexOf without parameters is case sensitive, if your student name is spelled differently your test will fail.
Try to change the test to use the IndexOf overload that allow to ignore the case

if (AppliedStudentsPerLesson.IndexOf(LTB_StudentID.Text, 
            StringComparison.CurrentCultureIgnoreCase) != -1)
     ......
Steve
  • 213,761
  • 22
  • 232
  • 286
0

First check if the code is going into the for loop but a breakpoint over there..

Second since your Table_AppliedLesson is populated in the if statement, put a breakpoint at

if (AppliedStudentsPerLesson.IndexOf(LTB_StudentID.Text) != -1)

to find out if you code actually going in the "if statement".

Amit Rai Sharma
  • 4,015
  • 1
  • 28
  • 35
  • Hi thanks for replying, I added a breakpoint to it however it seemed that it didn't break the execution... –  May 07 '13 at 09:28
  • @Matthew: check if the code is even getting into for loop. Use Immediate Window or Watch window to see the values of variables – Amit Rai Sharma May 07 '13 at 09:34
0

after you set the values you need to set it as DataSource

 RefreshAppliedLessonsTable();
 dataGridView1.DataSource = Table_AppliedLessons;
Damith
  • 62,401
  • 13
  • 102
  • 153
0

Debugging an Windows Forms app on 64bit silently swallows all bugs in a Form_Load event, try hitting CTRL + ALT + E and check Common Language Runtime exceptions -> thrown checkbox. There might be an exception that you miss.

Jürgen Steinblock
  • 30,746
  • 24
  • 119
  • 189
  • Thanks! I've figured out the problem now. Please refer to my answer. –  May 07 '13 at 09:37
  • I usually avoid code in the Form_Load event but prefer shadowing `ShowDialog()` (is executed before Load) or using the Shown event. As a plus, this keeps your form responsive, since filling a table in the form load event will display your form half painted which is ugly. btw here is a link explaining why exceptions are swallowed on 64bit versions of windows. http://stackoverflow.com/questions/1624156/visual-studio-does-not-break-at-exceptions-in-form-load-event – Jürgen Steinblock May 07 '13 at 11:05