Click here to Skip to main content
15,914,066 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
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;  
}


What I have tried:

why else statement for function UserExistsToday not work and how to solve it
Posted
Updated 22-Feb-17 14:58pm

Probably because you have mismatch braces all over the place. If this is how your code is really formatted it's no wonder your code doesn't do what you want.

Also, learn to the use the debugger. It would have helped you solve this problem very quickly.
 
Share this answer
 
Your code is difficult to read, maybe you didn't copy paste it correctly. Anyway, you mentioned

"supose i have in excel file
user id dateprint
1001 22/02/2017"

Then in the UserExistsToday() method, you have select UserNo instead of [user id] and I hope dateprint = DatePrint , maybe you type it out all lower case.

C#
string str = $"select * from [Sheet1$] WHERE UserNo='{textBox1.Text}' AND DatePrint = '{label6.Text}'";
 
Share this answer
 
First indent your code
C#
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";
	}
} // the if close here
}
else
{
	label8.Text = "User added first time";
}

There is too much closing brackets.
Proper indentation help to spot this kind of problem.
Otherwise, the debugger will show you what the code is doing. When you see your code not respecting an else clause, the bracket problem is probably the guilty.
-----
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900