none
declaring variables within nested loops RRS feed

  • Question

  • Value of variable EPF does not assign within the netsd loop 

    Int k=0;

    Int g=0;

    String EPF;

    while(rd_1.Read())                       // read employee

           {      

           if ( rd_1["FileSize"].ToString() != "0") {

                            EPF = (rd_1["Email"].ToString());

                                           while(rd.Read())                            // read mydocs

                                         {      

                                        if ( rd["FileSize"].ToString() != "0"  ) {

                              

                                           if (rd["Email"]==EPF) {

                                                 Response.Write (   rd[“Comment"] ); 

                                                                            }

                                                                                 }

                                          g++;

                                         }

                     

                        

                                                 }                 

             k++;

                                       

           }

    Wednesday, January 16, 2019 9:41 AM

All replies


  • while(rd_1.Read())                       // read employee

           {      

           if ( rd_1["FileSize"].ToString() != "0") {

                            EPF = (rd_1["Email"].ToString());

                                           while(rd.Read())                            // read mydocs

                                         {      

                                        if ( rd["FileSize"].ToString() != "0"  ) {


    First, when you post code use the "Insert Code Block" button on the forum
    menu bar. It will do a better job of maintaining the format of the code than
    will just sticking it into the text of a post.

    Second, show the definitions/declarations of rd_1 and rd - don't make us try 
    to guess what kind of objects they are.

    - Wayne

    Wednesday, January 16, 2019 11:03 AM
  • In addition to what Wayne said, your comparison logic may provide inconsistent results. You are doing reference comparison. rd is a data reader? So rd[index] returns Object. When comparing object to string you can get false results even if they are the same value because of reference comparison instead of value comparison. At least for string the compiler is generally smart enough to fix strings but this may not be true for other types or in all cases. If you're running newer versions of C# then you'll get code analysis warnings letting you know this and providing fixes for you.

    Use the strongly typed field indexer (both data readers and DataRow support them) to work around these kinds of issues. For example if rd is a data reader then this should compare correctly.

    if (rd.GetFieldValue<string>(0) == EPF)
    
    //Or
    if (rd.GetString(0) == EPF)

    Unfortunately the generic version and the type-specific versions accept ordinals. You can get the ordinal from the name. Many people create extension methods to handle this to simplify the code.


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, January 16, 2019 4:32 PM
    Moderator
  • Hi cgsGUNASEKARA,

    Thank you for posting here.

    Based on your description, you want to figure out why Value of variable EPF does not assign within the netsd loop.

    It is best for to provide the information about rd_1 and rd. They may be instantiated from the SqlDataReader as usual.

    You could make the following changes:

    change

             if (rd["Email"]==EPF)

    into

         if (rd["Email"] .ToString()==EPF)

    If the problem still exists, please contact us in time.

    Hope my advice could be helpful.

    Best regards,

    Jack




    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by DerChris88 Friday, January 18, 2019 8:58 AM
    Thursday, January 17, 2019 9:43 AM
    Moderator
  •   problem solved, Thank you Jack

    best regards,

    CGS Gunasekara

    Friday, January 18, 2019 6:33 AM
  • Hi ,

    Thanks for your feedback.

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer , so that it will help other members to find solution quickly if they faces similar issue.

    Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 29, 2019 8:34 AM
    Moderator