回答済み LINQ To SQL Update Beginner Question

  • Tuesday, March 05, 2013 4:16 PM
     
      Has Code
    I am having trouble getting LINQ to update my DB.  I have tried to follow MSDN recommendation, but my code does not update the DB_Table and it's driving me crazy!

    My code is listed below, but I get a warning on var that says "Unreachable code detected."  Any help will be appreciated!

                    var query =
                        from masterTbl in dc.MasterUser_Tbls
                        where masterTbl.email == emailString
                        select masterTbl;
    
                    foreach (MasterUser_Tbl row in query)
                    {
                        row.password_recover_code = randNum.ToString();
                    }
    
                    dc.SubmitChanges();


    Brett The Jet

All Replies

  • Tuesday, March 05, 2013 5:18 PM
     
     Answered

    Hi Brett;

    Can you please post the complete function / method this code is in.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked As Answer by brmcdani44 Tuesday, March 05, 2013 6:26 PM
    •  
  • Tuesday, March 05, 2013 6:28 PM
     
      Has Code

    Fernando after some adjustments the warning has disappeared.  I just arranged all my queries one after the other.  Here is the complete function.  Is this okay?

                    Random random = new Random();
                    string randomNum = random.Next(9000).ToString();
    
                    var userNameString = (from a in dc.MasterUser_Tbls where a.email == email select a.username).FirstOrDefault();
                    var emailString = (from a in dc.MasterUser_Tbls where a.email == email select a.email).FirstOrDefault();
    
                    var query =
                        from masterTbl in dc.MasterUser_Tbls
                        where masterTbl.email == emailString
                        select masterTbl;
    
                    foreach (MasterUser_Tbl ord in query)
                    {
                        ord.password_recover_code = randomNum;
                    }
    
                    dc.SubmitChanges();
    
                    //create the mail message  
                    MailMessage mail = new MailMessage();
    
                    //set the addresses  
                    mail.From = new MailAddress("postmaster@@@@@@.com");
                    mail.To.Add(emailString);
    
                    //set the content  
                    mail.Subject = "Account Recovery";
                    mail.Body = "Your username is: " + userNameString + Environment.NewLine + Environment.NewLine + "Your password recovery code is: " + randomNum.ToString() + Environment.NewLine + Environment.NewLine + "Enter this number into your app to reset your password.";
                    return "Valid";


    Brett The Jet

  • Tuesday, March 05, 2013 6:50 PM
     
     Answered Has Code

    Hi brmcdani44;

    As far as the layout of your code I see nothing wrong with it. As for the warning you was getting let me show you what the compiler was trying to say and the reason why I wanted to see the complete function / method. Given the following function :

    public string MyFunction(string param1, ... , int paramN)
    {
        // ... Some code lines here
        
        return myReturnString;
        
        // Any code found here after the above return will be flaged by the compiler as
        // "Unreachable code detected." because the code that follows will never be
        // executed because it has already returned to the calling code.
        // ... One or more code lines here.
    }

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked As Answer by brmcdani44 Tuesday, March 05, 2013 7:21 PM
    •