I am new in Programming line please give me the code to connect Mysql Database with C# in windows Form.
I made a program of login and i create a Mysql databse but when i goes to connect the form with database& written whole code i encounter a problem That "error with database connection" and it show exception in code line

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

namespace Login
{
    public partial class Form1 : Form
    {
        private MySqlConnection connection;
        private string server;
        private string database;
        private string uid;
        private string password;

        public Form1()
        {
            InitializeComponent();
        }
        private void Initialize()
        {
            server = "localhost";
            database = "diary";
            uid = "root";
            password = "12345";
            string connectionString;
            connectionString = "SERVER=" + server + ";" + "DATABASE=" +
            database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";

            connection = new MySqlConnection(connectionString);
        }


        private void button1_Click(object sender, EventArgs e)
        {
           try
            {
                connection.Open();
                string qry1 = "Select * from login where Password=@Password and Username=@Username";
                MySqlCommand com = new MySqlCommand(qry1, connection);
                com.Parameters.AddWithValue("@Username", this.textBox1.Text);
                com.Parameters.AddWithValue("@Password", this.textBox2.Text);
                MySqlDataReader dr = com.ExecuteReader();// this line show the exception....

                while (dr.Read())
                {
                    if (dr.HasRows == true)
                    {
                        MessageBox.Show("Login Successfull", "Login Information");
                        this.Hide();
                        home h = new home();
                        h.Show();
                    }
                }
                if (dr.HasRows == false)
                {
                    MessageBox.Show("Access Denied", "Login Information");
                    this.Close();

                }
                connection.Close();
            }
            catch (Exception)
            {
                MessageBox.Show("Error with the database connection");
            }


        }    

      private void label3_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        } 

    }
}

NullReferenceException was unhandled, Object Reference not set to an instance of an object

Check you database connection state i.e connection is open or not...
or you can code this way if connection is closed then open it.best practices in c#

if(connection.state==connection.closed)
{
    connection.Open();
}

I believe it's your Connection String. I did a quick search for the format of a MySQL connection string

Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;

Look at your password part. You did "PASSWORD=", when it should be "Pwd="

That would explain the error you are getting to (it never even makes a connection)

I doubt the OP is even watching this thread considering he hasn't posted for a couple of years :)

This article has been dead for over six months. Start a new discussion instead.