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 System.Data.OleDb;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source='login.accdb'");

        public Form2()
        {
            InitializeComponent();
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form1 frm1 = new Form1();
            frm1.Show();
            this.Hide();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sqlSearch = "SELECT * FROM reg WHERE password='" + textBox1.Text + "'";
            OleDbCommand cmd = new OleDbCommand(sqlSearch, con);
            con.Open();
            OleDbDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                if (textBox2.Text == textBox3.Text)
                {

                    string sqlUpdate = "UPDATE reg SET WHERE password='" + textBox1.Text + "";
                    OleDbCommand cmdd = new OleDbCommand(sqlUpdate, con);
                    con.Open();
                    cmdd.ExecuteNonQuery();
                    con.Close();
                    MessageBox.Show("successfully updated");
                }
                else 
                {
                    MessageBox.Show("retype your new password correctly");
                }
            }

            else
            {
                MessageBox.Show("No Record Found!");
            }
            con.Close();

        }
    }
}

Edited 2 Years Ago by pathum_1

What error do you get and on which line does it occur?

It is difficult to help when you provide us with minimal information regarding the error.

line 48

con.Open();

a error appear sying The connection was not closed. The connection's current state is open..

Edited 2 Years Ago by pathum_1

Well it's exactly what it says. You're trying to open a connection that's already open. Remove line 48 and you'll be fine.

As Ketsuekiame said, you already open the connection on line 39 and don't shut it then, hence the exception you see being thrown.

A good practice here is to use [code lifted off net, hence using different connection, easy to change to yours]

    using (SqlConnection connection = new SqlConnection(connectionString))  
    {  
        SqlCommand command = connection.CreateCommand();  

        command.CommandText = "mysp_GetValue";  
        command.CommandType = CommandType.StoredProcedure;  

        connection.Open();  
        object ret = command.ExecuteScalar();  
    }  

when you are going to databases, this will mean you don't have to worry about closing the connection as it will be done for you when the end of the using is reached.

Edited 2 Years Ago by Mike Askew

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