C# .NET - Why else statement for function userexiststoday not work and how to solve it

Asked By ahmed elbarbary on 22-Feb-17 07:41 PM

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";  
  
  
}  
}  
  
}  
  
else  
  
{  
  
label8.Text = "User added first time";  
  
  
}  
  
my functions  
  
label6 represent datetoday  
  
  
public bool UserExistsToday()  
{  
OleDbConnection cn = newOleDbConnection(connection);  
  
string str = $"select * from [Sheet1$] WHERE UserNo='{textBox1.Text}' AND DatePrint = '{label6.Text}'";  
  
OleDbCommand cmd = newOleDbCommand(str, cn);  
  
cn.Open();  
  
var reader = cmd.ExecuteReader();  
  
return reader.HasRows;  
  
}  
  
public bool UserExists(string UserNo)  
  
{  
  
OleDbConnection cn = newOleDbConnection(connection);  
  
string str = "SELECT UserNo FROM [Sheet1$] WHERE UserNo = @UserNo";  
  
OleDbCommand cmd = newOleDbCommand(str, cn);  
  
cmd.Parameters.AddWithValue("@UserNo", UserNo);  
  
cn.Open();  
  
var reader = cmd.ExecuteReader();  
  
return reader.HasRows;  
} 
Robbe Morris replied to ahmed elbarbary on 23-Feb-17 08:46 AM
The method UserExistsToday will never return a record.  Not sure why you are expecting UserNo='{textBox1.Text}' to be automatically populated with the value of textBox1.Text.  You have no code here to replace that string with the value of the textbox.