return hasrows OleDbDataReader

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • michaeldebruin
    New Member
    • Feb 2011
    • 134

    return hasrows OleDbDataReader

    Hello all,

    I have the following code in class SQL

    Code:
    public void ExecuteReadQuery(string ReadQuery)
            {
                if (dbconn == null)
                    return;
    
                OleDbDataReader dr;
                OleDbCommand cmd = new OleDbCommand();
                dr = cmd.ExecuteReader();
    
                if (dr.HasRows == true)
                    return;
    
                try
                {
                    dbconn.Open();
                }
                catch (Exception ex)
                {
                    dbconn.Close();
                    throw new CSMException(ex.Message, ex.Message);
                }
                finally
                {
                    dbconn.Close();
                }
            }
    But how can I make sure that if the "hasrows" actually returns rows. My form (which is calling on the sql class) gets some kind of a signal. And he knows that the login data is correct and he can send you to the main menu?
    The code of the login form is posted below:

    Code:
    SQL sql = new SQL();
                    sql.ExecuteReadQuery("SELECT Username, Password FROM Users WHERE Username = "+user + "AND pass = "+pass);
    Code:
    if ()
                    {
                        using (MainMenu MainMenu = new MainMenu())
                        {
                            Hide();
                            MainMenu.ShowDialog();
                            Environment.Exit(0);
                        }
                    }
    Both codes are placed in my login form.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    1.) Open the connection before creating the command on it.
    2.) Write the sql in the SQL class rather than in the GUI code.
    3.) Create a method called, say IsValidUser that takes the user named and password and returns a boolean value.
    4.) In the method use
    Code:
    return dr.HasRows;
    to indicate that you have rows and so the user is valid.
    5.) Your GUI code then does if(sql.IsValidU ser("someUser", "somePassword") ) { ....

    Comment

    Working...