Issues with the c# datarow

user2721466

I have a small problem with the c# DataRow Command in my code. Because when im trying to loop trough my mysql database and i have the foreach like this:

foreach (DataRow row in _login._database)

    {

        //And search for Username and Pass that match

        if (row.ItemArray[0].Equals(username) && row.ItemArray[1].Equals(password))

        {

            _usernameTextBox.Text = String.Empty;

            _passwordTextBox.Text = String.Empty;

            MessageBox.Show("Login Success");

           break;

        }

        //If not, then show this message.

        else

        {

            MessageBox.Show("Username/Password incorrect");

            break;

        }

    }

This error will come up:

Error   1   Cannot convert type 'char' to 'System.Data.DataRow'

Can someone help/explain to me what im doeing wrong.

This is the rest of the code:

namespace Chat
{
    public partial class StartupForm : Form
    {
        private LoginConnect _login = new LoginConnect();

        public StartupForm()
        {
            InitializeComponent();
            _login.OpenConnection();
        }

        private void _loginButton_Click(object sender, EventArgs e)
        {
            string username = _usernameTextBox.Text;
            string password = HashPass(_passwordTextBox.Text);

            //Loop through database

    foreach (DataRow row in _login._database)

    {

        //And search for Username and Pass that match

        if (row.ItemArray[0].Equals(username) && row.ItemArray[1].Equals(password))

        {

            _usernameTextBox.Text = String.Empty;

            _passwordTextBox.Text = String.Empty;

            MessageBox.Show("Login Success");

           break;

        }

        //If not, then show this message.

        else

        {

            MessageBox.Show("Username/Password incorrect");

            break;

        }

    }



            _login.LoginQuery(username, password);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var register = new RegisterForm();
            register.ShowDialog();
        }

        public string HashPass(string password)
        {
            MD5 mdvijf = new MD5CryptoServiceProvider();

            //compute hash from the bytes of text

            mdvijf.ComputeHash(ASCIIEncoding.ASCII.GetBytes(password));

            //get hash result after compute it

            byte[] result = mdvijf.Hash;

            StringBuilder strBuilder = new StringBuilder();

            for (int i = 0; i < result.Length; i++)
            {
                //change it into 2 hexadecimal digits

                //for each byte

                strBuilder.Append(result[i].ToString("x2"));
            }

            return strBuilder.ToString();


        }

    }
}

LoginCOnnect.cs:

namespace Chat
{


    class LoginConnect
    {
        private MySqlConnection _connection = new MySqlConnection();
        private string _server;
        public string _database;
        private string _uid;
        private string _password;
        //public String MessageRecieved;
        private StringList _messagelist = new StringList();
        //private string _table = "logingegevens";
        private string _port;
        //private bool succes = false;


        public LoginConnect()
        {
            Initialize();
        }


        public void Initialize()
        {
            _server = "localhost";
            _port = "3307";
            _database = "testlogin";
            _uid = "root";
            _password = "usbw";

            string connectionString = "Server=" + _server + ";" + "Port=" + _port + ";" + "Database=" +
                               _database + ";" + "Uid=" + _uid + ";" + "Pwd=" + _password + ";";

            _connection = new MySqlConnection(connectionString);
        }

        public bool OpenConnection()
        {
            try
            {
                _connection.Open();
                return true;
            }
            catch (MySqlException ex)
            {
                switch (ex.Number)
                {
                    case 0:
                        MessageBox.Show("Cannot connect to server");
                        break;

                    case 1042:
                        MessageBox.Show("Unable to connect to any of the specified MySQL hosts");
                        break;

                    case 1045:
                        MessageBox.Show("Invalid username/password");
                        break;
                }
                return false;

            }


        }

        public void LoginQuery(string username, string password)
        {
            string loginquery = "SELECT * FROM logingegevens WHERE Username='" + username + "'AND Password='" + password + "';";

            try
            {

                MySqlCommand cmd = new MySqlCommand(loginquery, _connection);

                MySqlDataReader dataReader = cmd.ExecuteReader();
                int count = 0;
                while (dataReader.Read())
                {
                    count = count + 1;
                }
                if (count == 1)
                {
                    MessageBox.Show("Login Succesfull");
                }
                else if (count > 1)
                {
                    MessageBox.Show("Acces denied");
                }
                else
                {
                    MessageBox.Show("Username or passowrd is not correct.");
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

    }
}
Tobia Zambon

The problem is that in this statement

foreach (DataRow row in _login._database)

you are looping through a string, and so the enumeration of _login._database is an IEnumerable of chars and not of DataRow, so row variable would be a char and not a DataRow.

I suggest you to retrieve the data into an internal DataTable, your LoginConnect code would be like this:

namespace Chat
{    
    class LoginConnect
    {
        private MySqlConnection _connection = new MySqlConnection();
        private string _server;
        public string _database;
        private string _uid;
        private string _password;
        private StringList _messagelist = new StringList();
        private string _port;
        private DataTable _dataTable;

        public LoginConnect()
        {
            Initialize();
        }

        public DataTable Data
        { get { return _dataTable; } }

        public void Initialize()
        {
            _server = "localhost";
            _port = "3307";
            _database = "testlogin";
            _uid = "root";
            _password = "usbw";

            string connectionString = "Server=" + _server + ";" + "Port=" + _port + ";" + "Database=" +
                               _database + ";" + "Uid=" + _uid + ";" + "Pwd=" + _password + ";";

            _connection = new MySqlConnection(connectionString);
        }

        public bool OpenConnection()
        {
            try
            {
                _connection.Open();
                return true;
            }
            catch (MySqlException ex)
            {
                switch (ex.Number)
                {
                    case 0:
                        MessageBox.Show("Cannot connect to server");
                        break;

                    case 1042:
                        MessageBox.Show("Unable to connect to any of the specified MySQL hosts");
                        break;

                    case 1045:
                        MessageBox.Show("Invalid username/password");
                        break;
                }
                return false;
            }
        }

        public void LoginQuery(string username, string password)
        {
            string loginquery = "SELECT * FROM logingegevens WHERE Username='" + username + "'AND Password='" + password + "';";
            try
            {
                 MySqlCommand cmd = new MySqlCommand(loginquery, _connection);
                 MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
                 _dataTable = new DataTable();
                 adp.Fill(_dataTable);
                 var count = _dataTable.Rows.Count;   
            } 
            catch{}//your handling   
        }
    }
}

So you can loop through Data:

 foreach (DataRow row in _login.Data.AsEnumerable())

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related