always get false

Last post 05-08-2008 12:27 PM by Svante. 9 replies.

Sort Posts:

  • always get false

    05-08-2008, 8:51 AM
    • Loading...
    • sara1
    • Joined on 05-04-2008, 4:35 PM
    • Posts 10

    I always get false in my code
    i compare two integers but always the second condition is execute and first conditin never execute but it is wrong result

    code:
         int a = int.Parse(TextBox1.Text.Trim());
         int ExamId=   int.Parse(DropDownList1.SelectedValue);    
      
         DataTable DT = DA.GetAllSudentExams(int.Parse(DropDownList1.SelectedValue));
           
            for (int i = 0; i < (DT.Rows.Count) ; i++)
            {
                int SID = int.Parse(DT.Rows[i]["StudentId"].ToString());
              
         

                if ( SID==a )
                {
                  
                    Label1.Text = "The Student is Added before";
               
                }

                if (!(SID == a))   //always execute
                {
                    DA.AddStudentExams(ExamId, a);
                    Label1.Text = "The Student Added succesfully"; 
                  
                }
           }
        }

     

     

    can any one help

  • Re: always get false

    05-08-2008, 8:58 AM
    • Loading...
    • mgodoy_desenv
    • Joined on 02-22-2007, 7:12 PM
    • São Paulo - Brazil
    • Posts 405

    Change that block to

        if (SID != a) //always execute
        {
            DA.AddStudentExams(ExamId, a);
            Label1.Text = "The Student Added succesfully";
        }

  • Re: always get false

    05-08-2008, 9:06 AM
    • Loading...
    • SGWellens
    • Joined on 01-02-2007, 9:27 PM
    • MN, USA
    • Posts 2,286
    • Moderator
      TrustedFriends-MVPs

    sara1:
    i compare two integers but always the second condition is execute and first conditin never execute

    That is because the two numbers are never equal.

     


    You should clarify your code:

            if (SID == a)
            {
                Label1.Text = "The Student is Added before";
            }
            else
            {
                DA.AddStudentExams(ExamId, a);
                Label1.Text = "The Student Added succesfully";
            }

     
    Steve Wellens
  • Re: always get false

    05-08-2008, 9:50 AM
    • Loading...
    • sara1
    • Joined on 05-04-2008, 4:35 PM
    • Posts 10

    i try your suggestion but still have same problem

  • Re: always get false

    05-08-2008, 9:59 AM
    • Loading...
    • bullpit
    • Joined on 06-29-2006, 3:59 PM
    • Posts 2,708

    I may be wrong but don't you think the first condition needs a break;

    [bullpit^-^]
    If you can read this, you are too close to the screen!!!
  • Re: always get false

    05-08-2008, 10:04 AM
    • Loading...
    • mgodoy_desenv
    • Joined on 02-22-2007, 7:12 PM
    • São Paulo - Brazil
    • Posts 405

    Did you try that:

    Change that block to

    if (SID != a) //always execute
    {
        DA.AddStudentExams(ExamId, a);
        Label1.Text = "The Student Added succesfully";
    }

  • Re: always get false

    05-08-2008, 10:10 AM
    • Loading...
    • che3358
    • Joined on 09-25-2003, 10:23 AM
    • Cleveland, OH
    • Posts 700

    Becasue you put your labek in the loop, the lable always displays the last one in the loop. Re-think your logic. Some thing like:

         int a = int.Parse(TextBox1.Text.Trim());
         int ExamId=   int.Parse(DropDownList1.SelectedValue);   

         bool flag = false;   
       
         DataTable DT = DA.GetAllSudentExams(int.Parse(DropDownList1.SelectedValue));
           
            for (int i = 0; i < (DT.Rows.Count) ; i++)
            {
                int SID = int.Parse(DT.Rows[i]["StudentId"].ToString());
               
                if ( SID==a )
                {
                   
                     flag = true;

                     break;            
                }

                else

               {

                   //Do what you want to do;

               }

            }
        }

       if(flag)

          Label1.Text = "The Student is Added before";
       else

          Label1.Text = "The Student Added succesfully"; 

        

          

     

  • Re: always get false

    05-08-2008, 10:59 AM
    Answer
    • Loading...
    • Svante
    • Joined on 02-12-2007, 12:15 PM
    • Stockholm, Sweden
    • Posts 1,581
    • Moderator

     

    int newStudentId = int.Parse(TextBox1.Text);
    int examId = int.Parse(DropDownList1.SelectedValue);     
       
    DataTable dt = DA.GetAllSudentExams(examId);
            
    bool isNew = true;   
    for (int i = 0; i < DT.Rows.Count; ++i)
    {
        int StudentID = int.Parse(dt.Rows[i]["StudentId"].ToString());
        if (studentID == newStudentId)
        {
             isNew = false;
             break;
        }
    }
    if (isNew)
    {              
        DA.AddStudentExams(examId, newStudentId);
        Label1.Text = "The Student Added succesfully";  
    }
    else
    {
        Label1.Text = "The Student is Added before";
    }
    
     
    Svante
    AxCrypt - Free Open Source File Encryption & Online Password Manager - http://www.axantum.com
    [Disclaimer: Code snippets usually uncompiled, beware typos.]
    ______
    Don't forget to click "Mark as Answer" on the post(s) that helped you.
  • Re: always get false

    05-08-2008, 12:15 PM
    • Loading...
    • sara1
    • Joined on 05-04-2008, 4:35 PM
    • Posts 10

    Thanks all

    Last solution work

      but i still do not know what's problem with my code

  • Re: always get false

    05-08-2008, 12:27 PM
    • Loading...
    • Svante
    • Joined on 02-12-2007, 12:15 PM
    • Stockholm, Sweden
    • Posts 1,581
    • Moderator

    sara1:
    i still do not know what's problem with my code

    The problem is with your logic in the code. In words it reads:

    "For all existing students of previously added for a given exam, test each and every one for the following conditions: If the exam is registered for the proposed new student, say "error". If the exam is not registered, do and say "added".".

    The problem is that since you check each and every one for the "not registered", the only case where this test will not evaluate to true at least once in the loop is if the only previously added student/exam is the same as you try again with.

    If you have a group of students, and you ask each and every one of them "are you Dave", you'll find that at most one will actually be Dave - but all but at most one will not be! (Provided the student population only has one Dave, but since the assumption here is that the student id is unique, we'll asssume that there's only one Dave for this example.

    The code proposed will ask each student until either "Dave" is found (already added), or all have been asked, and none was "Dave". Only when you have asked all, and got a negative answer for each and every one do you know that the one you're looking for is not there - but as soon as you find one that answers to "Dave", you know that you found (someone already added).

    Hope this makes it clearer.

    Svante
    AxCrypt - Free Open Source File Encryption & Online Password Manager - http://www.axantum.com
    [Disclaimer: Code snippets usually uncompiled, beware typos.]
    ______
    Don't forget to click "Mark as Answer" on the post(s) that helped you.
Page 1 of 1 (10 items)