Results 1 to 5 of 5

Thread: Mysql

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    63

    Mysql

    Code:
    public bool LoginAccount(string username, string password)
            {
                string query = "SELECT * FROM login where userid = '" + username + "'";
                string _dbReader;
                bool _check = false;
                //Open connection
                if (this.OpenConnection() == true)
                {
                    //Create Command
                    MySqlCommand cmd = new MySqlCommand(query, connection);
                    //Create a data reader and Execute the command
                    MySqlDataReader dataReader = cmd.ExecuteReader();
    
                    //Read the data and store them in the list
                    if (dataReader.HasRows)
                    {
                        if (dataReader.Read())
                        {
                            _dbReader = Convert.ToString(dataReader["user_pass"]);
                            _accountid = Convert.ToInt32(dataReader["account_id"]);
                            if (_dbReader == password)
                            {
                                _check = true;
                            }
                            else
                            {
                                MessageBox.Show("Incorrect password.");
                            }
                        }
                    }
                    else
                    {
                        _check = false;
                        MessageBox.Show("Account not found", username, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    //close Data Reader
                    dataReader.Close();
                    //close Connection
                    this.CloseConnection();
                }
                else
                {
                    _check = false;
                    MessageBox.Show("Server is closed.");
                }
                return _check;
            }
    
            //Create Character
            public bool CreateCharacter(string name, string job)
            {
                bool _check = false;
                try
                {
                    string query = "INSERT INTO characters (account_id, name, class) VALUES('" + _accountid + "' ,'" + name + "' ,'" + job + "')";
                    //open connection
                    if (this.OpenConnection() == true)
                    {
                        //create command and assign the query and connection from the constructor
                        MySqlCommand cmd = new MySqlCommand(query, connection);
                        //Execute command
                        cmd.ExecuteNonQuery();
                        MessageBox.Show("Create Successful.");
                        _check = true;
                    }
                }
                catch (Exception ex)
                {
                    _check = false;
                    if (ex.Message.IndexOf("name") >= 0)
                        MessageBox.Show("That Character already exists. Please change the Character Name.");
                    else
                        MessageBox.Show(ex.Message);
                }
                finally
                {
                    //close connection
                    this.CloseConnection();
                }
                return _check;
            }
    im confus about insert _account_id it's always 0 when passing value
    Last edited by solid2005; Jul 25th, 2010 at 05:58 AM.

  2. #2
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: Mysql

    Where is _account_id declared?
    Delete it. They just clutter threads anyway.

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    63

    Re: Mysql

    i declare int _accountid;
    Code:
    //Read the data and store them in the list
                    if (dataReader.HasRows)
                    {
                        if (dataReader.Read())
                        {
                            _dbReader = Convert.ToString(dataReader["user_pass"]);
                     >>>>>>>>>>>>>>>>>       _accountid = Convert.ToInt32(dataReader["account_id"]);
                            if (_dbReader == password)
                            {
                                _check = true;
                            }
                            else
                            {
                                MessageBox.Show("Incorrect password.");
                            }
                        }
                    }
    the value always 0 i don't know why.

  4. #4
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: Mysql

    That is not declaring a variable.

    Try this
    Code:
    string _dbReader = Convert.ToString(dataReader["user_pass"]);
    int _accountid = Convert.ToInt32(dataReader["account_id"]);
    Delete it. They just clutter threads anyway.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Mysql

    Follow the CodeBank link in my signature and check out my thread entitled "Validate Credentials Against User Record in Database".
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width