Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8230473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:08:55+00:00 2026-06-07T17:08:55+00:00

Else statement not working properly in foreach loop? Here’s my code. If something’s wrong

  • 0

Else statement not working properly in foreach loop? Here’s my code. If something’s wrong with my code explanations would be nice.

protected void getdata_Click(object sender, EventArgs e)
{
    using (var db2 = new cftzClassDataContext())
    {
        var username = (from p in db2.cftzAccounts
                        where p.username.Equals(getdata2.Text)
                        select p);
        foreach (var p in username)
        {
            if (getdata2.Text == p.username)
            {
                displayMSG.Text = "Is this correct ";
                displayData.Text = p.username;
                displayQuestionMark.Text = "?";
            }
            else
            {
                displayMSG.Text = "No User Found.";
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T17:08:59+00:00Added an answer on June 7, 2026 at 5:08 pm

    The if statement is entirely redundant, as you have already filtered your list to only include accounts where the username equals getData2.Text. Therefore, your if test will always evaluate to true.

    You can rewrite it as this, with the same effect

        using (var db2 = new cftzClassDataContext())
        {
            var username = (from p in db2.cftzAccounts.Take(100)
                            where p.username.Equals(getdata2.Text)
                            select p);
    
            foreach (var p in username)
            {
                displayMSG.Text = "Is this correct ";
                displayData.Text = p.username;
                displayQuestionMark.Text = "?";
            }
        }
    

    However, as Hans points out, your loop is unnecessary as well, as all the usernames will be the same. Your code could be as simple as:

        using (var db2 = new cftzClassDataContext())
        {
            var user = db2.cftzAccounts.
                  FirstOrDefault(account => account.username.Equals(getdata2.Text));
    
            if (user != null)
            {
                displayMSG.Text = "Is this correct ";
                displayData.Text = user.username;
                displayQuestionMark.Text = "?";
            }
            else 
            {
                displayMSG.Text = "No User Found.";
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hello its really weird that a if and else statement is not working in
I have a if-else statement which isn't quite working as I would want it
I have an if/else statement that is not working. while (rdr.Read()) { string permission
I'm kind of stuck on something... my else statement doesn't seem to be working
JSfiddle (showing the non-working code) here: http://jsfiddle.net/ts2Rr/ I have three boxes that a user
Not to sure about why this code is not working as intended and hoping
Stored procedure IF statement is not working @manuel varchar(50), @tour int, @tourname varchar(50) OUTPUT
This is driving me crazy - basically this if statement is not working. The
Hello guys! I have been trying to create a if/else statement code in jQuery
Hi guys my if statement is not working. I want to basically do nothing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.