I work in c# windows form vs 2015 selecting data from excel
problem i face it
supose i have in excel file
user id dateprint
1001 22/02/2017

if try add this user 1001 again with date 23/02/2017
it must give me message this user found before
but this is not happen and this is actually my problem
my code

bool c = QRC.UserExists(textBox1.Text);  

bool b = UserExistsToday();  

if (c == true)  

{  

if (b == true)  

{  

label8.Text = "User added today";  

}  

else  

{  

label8.Text = "User added before";  

}

Dani AI

Generated

Problem summary: checks QRC.UserExists(...) and then calls UserExistsToday(), but the date branch does not behave as expected. The symptom usually means the date-comparison (or the way Excel dates are read) is wrong, not that the C# else keyword is broken.

Good suggestions already in thread: 's advice to break on the suspect line and inspect variables is exactly the right first step. 's cleanup (remove needless booleans and keep the ifs direct) makes the logic easier to follow and debug.

Likely culprits and quick diagnostics

  • Date value vs string or OLE value: Excel dates sometimes come through as doubles (OLE automation) or as strings in dd/MM/yyyy vs MM/dd/yyyy. Log the raw cell value and its CLR type.
  • Time component: if the stored DateTime has a time, comparing full DateTime will fail. Compare only DateTime.Date.
  • Wrong scope/state: UserExistsToday might not be using the same user id or the freshly-added row (race/order). Make the date-check method accept the user id and the date to check.
  • Parsing failures silently returning false: use TryParse/TryParseExact and fall back to DateTime.FromOADate when needed.

Example of a robust check (adjust GetRowsForUser to match the Excel reader used):

// using System.Globalization;
private bool UserExistsForDate(string userId, DateTime dateToCheck)
{
    foreach (var row in GetRowsForUser(userId))
    {
        object raw = row["dateprint"];
        DateTime rowDate;
        if (raw is double) rowDate = DateTime.FromOADate((double)raw);
        else if (!DateTime.TryParseExact(raw?.ToString(), new[] { "d/M/yyyy","dd/MM/yyyy","M/d/yyyy","MM/dd/yyyy" },
                 CultureInfo.InvariantCulture, DateTimeStyles.None, out rowDate))
        {
            if (!DateTime.TryParse(raw?.ToString(), CultureInfo.CurrentCulture, DateTimeStyles.None, out rowDate))
                continue; // skip unparsable values
        }

        if (rowDate.Date == dateToCheck.Date) return true;
    }
    return false;
}

Final notes: set a breakpoint and inspect the actual values/types (per ). Prefer passing user id and date into the check to avoid hidden state, and use DateTime.Date comparisons. After these checks the conditional branches will reflect the real data rather than a parsing/formatting bug.

Recommended Answers

All 2 Replies

While I wonder why the == true is there, in Visual Studio for over a decade I can break on the suspect line to examine the variables and see what's what. Try that.

I don't have a suggestion for a solution but I'd like to comment on the code. I've taken the above code and rewritten it as

if (QRC.UserExists(textBox1.Text))  
{  
    if (UserExistsToday())  
    {  
        label8.Text = "User added today";  
    }  
    else  
    {  
        label8.Text = "User added before";  
    }

without getting into a discussion as to the preference for same-line or separate-line brace brackets, can you see how removing some white space and the unnecessary b and c variables (which are not named to be descriptive), the code is now much easier to read?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.