HI ANYBODY PLS HELP ME !
-
And what's the error message?
-
There must be. If you are going into an exception handler, there has to be an exception.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
There must be. If you are going into an exception handler, there has to be an exception.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
We get that. We still need to know what exception you are currently getting to help diagnose the problem.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
HI THIS IS MY C# PROGRAM I WANT TO GET A BOOLEAN RESULT TRUE OF FALSE I DIDNT FIND ANY ERROR....BUT THE PROB IS ONLY THE EXECPTION PART IS GETTING EXECUTED...ITS A LOGIN PAGE,....
public bool validateuser(User u)
{
bool result = true;
try
{
string validateq = "select count(*) from register where user.username = @username and user.password = @passwd ";
cmd = new SqlCommand(validateq, con);
cmd.Parameters.AddWithValue("@username", u.username);
cmd.Parameters.AddWithValue("@passwd", u.password);
con.Open();
reader = cmd.ExecuteReader();
reader.Read();
int r = Convert.ToInt32(reader[0]);
if (r > 0)
{
result = true;
}
else
{
result = false;
}
return result;} catch (Exception ex) { result = false; return result; } }
Can you give an example of your table structure, because the query string doesn't seem right. Is the table you are querying
register
oruser
. Personally I think you should be looking to do something like this :- (assumes table where users info is kept is called users, substitute register if that is the correct table)try
{
string validateq = "SELECT COUNT(userid) FROM users WHERE username = @username AND password = @passwd ";
SqlCommand cmd = new SqlCommand(validateq, con);
cmd.Parameters.AddWithValue("@username", u.UserName);
cmd.Parameters.AddWithValue("@passwd", u.Password);
con.Open();
return Convert.ToInt32(cmd.ExecuteScalar()) > 0;
}
catch (SqlException ex)
{
throw new Exception(ex.Message,ex) ;
}I hope this helps.
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
-
Come on! What's that
catch(Exception ex)
thing? There it is. Use the debugger, or show it in a MessageBox, or write it to a log file... -
HI THIS IS MY C# PROGRAM I WANT TO GET A BOOLEAN RESULT TRUE OF FALSE I DIDNT FIND ANY ERROR....BUT THE PROB IS ONLY THE EXECPTION PART IS GETTING EXECUTED...ITS A LOGIN PAGE,....
public bool validateuser(User u)
{
bool result = true;
try
{
string validateq = "select count(*) from register where user.username = @username and user.password = @passwd ";
cmd = new SqlCommand(validateq, con);
cmd.Parameters.AddWithValue("@username", u.username);
cmd.Parameters.AddWithValue("@passwd", u.password);
con.Open();
reader = cmd.ExecuteReader();
reader.Read();
int r = Convert.ToInt32(reader[0]);
if (r > 0)
{
result = true;
}
else
{
result = false;
}
return result;} catch (Exception ex) { result = false; return result; } }
Reason for my vote of one: DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalisation if you want to be taken seriously.
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
-
HI THIS IS MY C# PROGRAM I WANT TO GET A BOOLEAN RESULT TRUE OF FALSE I DIDNT FIND ANY ERROR....BUT THE PROB IS ONLY THE EXECPTION PART IS GETTING EXECUTED...ITS A LOGIN PAGE,....
public bool validateuser(User u)
{
bool result = true;
try
{
string validateq = "select count(*) from register where user.username = @username and user.password = @passwd ";
cmd = new SqlCommand(validateq, con);
cmd.Parameters.AddWithValue("@username", u.username);
cmd.Parameters.AddWithValue("@passwd", u.password);
con.Open();
reader = cmd.ExecuteReader();
reader.Read();
int r = Convert.ToInt32(reader[0]);
if (r > 0)
{
result = true;
}
else
{
result = false;
}
return result;} catch (Exception ex) { result = false; return result; } }
Another reason for a one-vote: one should NOT swallow exceptions, i.e. have a catch block that does not use the information contained in the exception. At least, log it and look at it, that should allow you to solve your problems yourself. :|
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
HI THIS IS MY C# PROGRAM I WANT TO GET A BOOLEAN RESULT TRUE OF FALSE I DIDNT FIND ANY ERROR....BUT THE PROB IS ONLY THE EXECPTION PART IS GETTING EXECUTED...ITS A LOGIN PAGE,....
public bool validateuser(User u)
{
bool result = true;
try
{
string validateq = "select count(*) from register where user.username = @username and user.password = @passwd ";
cmd = new SqlCommand(validateq, con);
cmd.Parameters.AddWithValue("@username", u.username);
cmd.Parameters.AddWithValue("@passwd", u.password);
con.Open();
reader = cmd.ExecuteReader();
reader.Read();
int r = Convert.ToInt32(reader[0]);
if (r > 0)
{
result = true;
}
else
{
result = false;
}
return result;} catch (Exception ex) { result = false; return result; } }
If I'm not wrong, your query is wrong and your database provider is throwing an exception that your code is swallowing.
select count(*) from register where user.username = @username and user.password = @passwd
must be rewritten as
select count(*) from register where username = @username and password = @passwd
And don't swallow exceptions, log it and/or display it.
-
HI THIS IS MY C# PROGRAM I WANT TO GET A BOOLEAN RESULT TRUE OF FALSE I DIDNT FIND ANY ERROR....BUT THE PROB IS ONLY THE EXECPTION PART IS GETTING EXECUTED...ITS A LOGIN PAGE,....
public bool validateuser(User u)
{
bool result = true;
try
{
string validateq = "select count(*) from register where user.username = @username and user.password = @passwd ";
cmd = new SqlCommand(validateq, con);
cmd.Parameters.AddWithValue("@username", u.username);
cmd.Parameters.AddWithValue("@passwd", u.password);
con.Open();
reader = cmd.ExecuteReader();
reader.Read();
int r = Convert.ToInt32(reader[0]);
if (r > 0)
{
result = true;
}
else
{
result = false;
}
return result;} catch (Exception ex) { result = false; return result; } }
Along with what the others say, I would use ExecuteScalar and simply cast the result.
return ( (int) cmd.ExecuteScalar() > 0 ) ;
And were I to catch the Exception I'd wrap it another Exception with more information. Something along the lines of:catch (Exception ex)
{
throw ( new System.Data.DataException ( "validateuser unable to find user " + u.username , ex ) ) ;
} -
Another reason for a one-vote: one should NOT swallow exceptions, i.e. have a catch block that does not use the information contained in the exception. At least, log it and look at it, that should allow you to solve your problems yourself. :|
Luc Pattyn [My Articles] Nil Volentibus Arduum
Luc Pattyn wrote:
Another reason for a one-vote: one should NOT swallow exceptions,
Can't disagree more with that. This forum is "C#", not "Advanced C# Only". So the fact someone doesn't know how to write exception handling correctly is a reason for correction not down voting.
-
HI THIS IS MY C# PROGRAM I WANT TO GET A BOOLEAN RESULT TRUE OF FALSE I DIDNT FIND ANY ERROR....BUT THE PROB IS ONLY THE EXECPTION PART IS GETTING EXECUTED...ITS A LOGIN PAGE,....
public bool validateuser(User u)
{
bool result = true;
try
{
string validateq = "select count(*) from register where user.username = @username and user.password = @passwd ";
cmd = new SqlCommand(validateq, con);
cmd.Parameters.AddWithValue("@username", u.username);
cmd.Parameters.AddWithValue("@passwd", u.password);
con.Open();
reader = cmd.ExecuteReader();
reader.Read();
int r = Convert.ToInt32(reader[0]);
if (r > 0)
{
result = true;
}
else
{
result = false;
}
return result;} catch (Exception ex) { result = false; return result; } }
-
Luc Pattyn wrote:
Another reason for a one-vote: one should NOT swallow exceptions,
Can't disagree more with that. This forum is "C#", not "Advanced C# Only". So the fact someone doesn't know how to write exception handling correctly is a reason for correction not down voting.
if he doesn't know how to deal with an exception, he should not use a try-catch block. The code shown very much resembles an
ON ERROR RESUME NEXT
statement. :)Luc Pattyn [My Articles] Nil Volentibus Arduum