problem in C# & Mysql login form using phpmyadmin | 'System.Collections.Generic.KeyNotFoundException'

0
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using MySql.Data.MySqlClient;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace restaurants_management_1
{



    public partial class login : Form
    {


        int i;
        public login()
        {
            InitializeComponent();

        }




        private void login_Load(object sender, EventArgs e)
        {
            //InitializeComponent();
        }


        signup sign = new signup();


/*
        private void bunifuCustomLabel3_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

 */

        private void timer1_Tick(object sender, EventArgs e)
        {
            sign.Left += 10;
            if(sign.Left >= 830)
            {
                timer1.Stop();
                this.TopMost = false;
                sign.TopMost = true;
                timer2.Start();

            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {

            sign.Left -= 10;
            if(sign.Left <= 600)
            {
                timer2.Stop();

                Thread th;
                this.Close();
                th = new Thread(opennewform);
                th.SetApartmentState(ApartmentState.STA);
                th.Start();

            }




        }

        private void bunifuCustomLabel2_Click(object sender, EventArgs e)
        {
            // MessageBox.Show("test");

            timer1.Start();
           // bunifuFormFadeTransition1.Dispose();

            sign.Show();



        }

        private void opennewform()
        {
            Application.Run(new signup());
        }



        private void bunifuFlatButton1_Click(object sender, EventArgs e)
        {
            //  adapter = new MySqlDataAdapter("SELECT `username`, `password` FROM `users` WHERE 
              `username` = '" + textBox_username.Text + "' AND `password` = '" + 
               textBox_password.Text + "'", connection);


             DB db = new DB();     

            //  String name = bunifuMaterialTextbox1.Text;
            //  String password = bunifuMaterialTextbox2.Text;


            i = 0;
            MySqlConnection con;
            con = db.getConnection();
            db.openConnection();
            MySqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select * from signup where name = '" + bunifuMaterialTextbox1.Text + 
            "' and password = '" + bunifuMaterialTextbox2.Text + "'";
            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            MySqlDataAdapter da = new MySqlDataAdapter(cmd);
            da.Fill(dt);

            i = Convert.ToInt32(dt.Rows.Count.ToString());

            if (i == 0)
            {
                MessageBox.Show("invalid username or password");
            }
            else
            {

                MessageBox.Show("login success");

            }


            db.closeConnection(); 

          //  MySqlDataAdapter adapter = new MySqlDataAdapter();

            //DataSet table = new DataSet();

            //  DataTable table = new DataTable();

           // DataSet table = new DataSet();

            // MySqlCommand command = new MySqlCommand("SELECT name , password FROM signup WHERE name = @name and password = @password", db.getConnection());

            // command.Parameters.Add("@name", MySqlDbType.VarChar).Value = name;
            // command.Parameters.Add("@password", MySqlDbType.VarChar).Value = password;

            // adapter.SelectCommand = command;

            // adapter = new MySqlDataAdapter("SELECT `name`, `password` FROM `signup` WHERE `name` = '" + bunifuMaterialTextbox1.Text + "' AND `password` = '" + bunifuMaterialTextbox2 .Text + "'", db.getConnection());

            //  int r= adapter.Fill(table);

            //adapter.SelectCommand = new MySqlCommand("SELECT `name`, `password` FROM `signup` WHERE `name` = '" + bunifuMaterialTextbox1.Text + "' AND `password` = '" + bunifuMaterialTextbox2.Text + "'", db.getConnection());

            // adapter.Fill(table);

           /* DB db = new DB();

              String name = bunifuMaterialTextbox1.Text;
              String password = bunifuMaterialTextbox2.Text;

            DataTable table = new DataTable();

            MySqlDataAdapter adapter = new MySqlDataAdapter();

            MySqlCommand command = new MySqlCommand("SELECT * FROM signup WHERE name = @usn and password = @pass", db.getConnection());

            command.Parameters.Add("@usn", MySqlDbType.VarChar).Value = name;
            command.Parameters.Add("@pass", MySqlDbType.VarChar).Value = password;

            adapter.SelectCommand = command;

            adapter.Fill(table);



            if (table.Rows.Count > 0)
            {
                // this.Hide();
                // MainForm mainform = new MainForm();
                // mainform.Show();

                MessageBox.Show("login success");
            }
            else
            {
                // check if the username field is empty
                if (name.Trim().Equals(""))
                {
                    MessageBox.Show("Enter Your Username To Login", "Empty Username", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                // check if the password field is empty
                else if (password.Trim().Equals(""))
                {
                    MessageBox.Show("Enter Your Password To Login", "Empty Password", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            */


                /* if (table.Rows.Count > 0)
                 {
                     MessageBox.Show("login success");
                 }
                 else
                 {
                     MessageBox.Show("login failed");
                 }

                 db.closeConnection();

                   */
            }


    }
    }

The Problem is coming in bunifuFlatButton1_Click in execute non query System.Collections.Generic.KeyNotFoundException it also shows in adapter.fill(table) . I have tried various different connection strings different adapter method and dataset / datatable method but none of the is seem to be working . connection is success because i am able to send the data from signup from to the phpmyadmin mysql

below is the code for db.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Threading.Tasks;

namespace restaurants_management_1
{

    class DB
    {
        // the connection
         MySqlConnection connection = new MySqlConnection("Server=localhost;Database=rms;Uid='root';Pwd='';CharSet=utf8;");


        // create a function to open the connection
        public void openConnection()
        {

                connection.Open();

        }

        // create a function to close the connection
        public void closeConnection()
        {            
                connection.Close();

        }

        // create a function to return the connection
        public MySqlConnection getConnection()
        {
            return connection;
        }
    }
}

error code that is coming

***
System.Collections.Generic.KeyNotFoundException
  HResult=0x80131577
  Message=The given key was not present in the dictionary.
  Source=mscorlib
  StackTrace:
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at MySql.Data.MySqlClient.CharSetMap.GetCharacterSet(DBVersion version, String CharSetName)
   at MySql.Data.MySqlClient.NativeDriver.GetFieldMetaData41()
   at MySql.Data.MySqlClient.NativeDriver.GetFieldMetaData()
   at MySql.Data.MySqlClient.NativeDriver.ReadColumnMetadata(Int32 count)
   at MySql.Data.MySqlClient.ResultSet.LoadMetaData()
   at MySql.Data.MySqlClient.ResultSet.NextResult()
   at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
   at restaurants_management_1.login.bunifuFlatButton1_Click(Object sender, EventArgs e) in C:\Users\danda\source\repos\restaurants management 1\restaurants management 1\login.cs:line 124
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at Bunifu.Framework.UI.BunifuFlatButton.c9aef0b7bd903708ce7f60385a0f201bf(Object c1de11ede54b55a91240ac3b9926ac670, EventArgs cdd39ed5f7e629ab12aa220afe0f969c6)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.Label.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at restaurants_management_1.signup.opennewform() in C:\Users\danda\source\repos\restaurants management 1\restaurants management 1\signup.cs:line 49
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

***
c#
mysql
xampp
asked on Stack Overflow Dec 23, 2019 by Aditya Dand • edited Jan 18, 2020 by Isaac Bennetch

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0