Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. HI ANYBODY PLS HELP ME !

HI ANYBODY PLS HELP ME !

Scheduled Pinned Locked Moved C#
helpcsharp
17 Posts 9 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mbjino
    wrote on last edited by
    #1

    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;
        }
    }
    
    B W OriginalGriffO L L 7 Replies Last reply
    0
    • M mbjino

      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;
          }
      }
      
      B Offline
      B Offline
      Bernhard Hiller
      wrote on last edited by
      #2

      User is a reserved word, it must be escaped:

      ...where [user].username = @username and [user].password = @passwd

      Do not forget to close your reader, a good place for that is in a finally clause:

      try
      {
      //some code
      }
      catch(Exception ex)
      {
      //do something with the exception
      }
      finally
      {
      if (reader!=null)
      reader.Close()
      }

      M 1 Reply Last reply
      0
      • B Bernhard Hiller

        User is a reserved word, it must be escaped:

        ...where [user].username = @username and [user].password = @passwd

        Do not forget to close your reader, a good place for that is in a finally clause:

        try
        {
        //some code
        }
        catch(Exception ex)
        {
        //do something with the exception
        }
        finally
        {
        if (reader!=null)
        reader.Close()
        }

        M Offline
        M Offline
        mbjino
        wrote on last edited by
        #3

        i tried it but the prob is the if part is not getting executed insted its going to catch part!

        B 1 Reply Last reply
        0
        • M mbjino

          i tried it but the prob is the if part is not getting executed insted its going to catch part!

          B Offline
          B Offline
          Bernhard Hiller
          wrote on last edited by
          #4

          And what's the error message?

          M 1 Reply Last reply
          0
          • B Bernhard Hiller

            And what's the error message?

            M Offline
            M Offline
            mbjino
            wrote on last edited by
            #5

            no error message thats making it more bad :( :( :(

            P B 2 Replies Last reply
            0
            • M mbjino

              no error message thats making it more bad :( :( :(

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              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

              M 1 Reply Last reply
              0
              • P Pete OHanlon

                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

                M Offline
                M Offline
                mbjino
                wrote on last edited by
                #7

                s but instead of exception message i have given to return false result ;

                P 1 Reply Last reply
                0
                • M mbjino

                  s but instead of exception message i have given to return false result ;

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • M mbjino

                    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;
                        }
                    }
                    
                    W Offline
                    W Offline
                    Wayne Gaylard
                    wrote on last edited by
                    #9

                    Can you give an example of your table structure, because the query string doesn't seem right. Is the table you are querying register or user. 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

                    1 Reply Last reply
                    0
                    • M mbjino

                      no error message thats making it more bad :( :( :(

                      B Offline
                      B Offline
                      Bernhard Hiller
                      wrote on last edited by
                      #10

                      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...

                      1 Reply Last reply
                      0
                      • M mbjino

                        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;
                            }
                        }
                        
                        OriginalGriffO Offline
                        OriginalGriffO Offline
                        OriginalGriff
                        wrote on last edited by
                        #11

                        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

                        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                        1 Reply Last reply
                        0
                        • M mbjino

                          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;
                              }
                          }
                          
                          L Offline
                          L Offline
                          Luc Pattyn
                          wrote on last edited by
                          #12

                          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

                          J 1 Reply Last reply
                          0
                          • M mbjino

                            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;
                                }
                            }
                            
                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #13

                            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.

                            1 Reply Last reply
                            0
                            • M mbjino

                              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;
                                  }
                              }
                              
                              P Offline
                              P Offline
                              PIEBALDconsult
                              wrote on last edited by
                              #14

                              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 ) ) ;
                              }

                              1 Reply Last reply
                              0
                              • L Luc Pattyn

                                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

                                J Offline
                                J Offline
                                jschell
                                wrote on last edited by
                                #15

                                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.

                                L 1 Reply Last reply
                                0
                                • M mbjino

                                  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;
                                      }
                                  }
                                  
                                  J Offline
                                  J Offline
                                  jschell
                                  wrote on last edited by
                                  #16

                                  You need to display the exception in your catch block. Something like.... System.Console.WriteLine("Exception=" + e); That will tell you what the error is and what line it shows up on.

                                  1 Reply Last reply
                                  0
                                  • J jschell

                                    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.

                                    L Offline
                                    L Offline
                                    Luc Pattyn
                                    wrote on last edited by
                                    #17

                                    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

                                    1 Reply Last reply
                                    0
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • World
                                    • Users
                                    • Groups