Click here to Skip to main content
15,904,024 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
NEW: I removed the second open connection but now i am getting another error which says "There is already an open DataReader associated with this Command which must be closed first"

Now what???


Hello everyone.

I want to update my password in my registration form. Here are some details:-

1) I have a registration form with only 2 textbox, 1) OldPassword 2) NewPassword. No username
2) I have a login screen with only Password. No username
3) What i want is that in my registration form i should be able to update my old password to a new one
4) Theres already a password in the database inserted manually.
5) Database is called NumberPlate, table is called LoginUser, And theres only one raw in the table called Password

My problem is that iam getting an error saying "The connection was not closed. The connection's current state is open"

Now i dont understand were the problem is so heres all my code. Dont forget to read the last part.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;

namespace Number_Plate
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        SqlDataAdapter da = new SqlDataAdapter();
        SqlConnection sc = new SqlConnection("Data Source=MAAZA-PC;Initial Catalog=NumberPlates;Integrated Security=True");

        private void BtnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                sc.Open();
            }
            catch (Exception)
            {
                MessageBox.Show("Did not connect");
            }
            SqlCommand cmd = new SqlCommand("SELECT * FROM LoginUser", sc);
            cmd.Connection = sc;
            SqlDataReader reader = null;
            reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                if (TxtOldPassword.Text == (reader["Password"].ToString()) && TxtPassword.Text.ToString().Length > 0)
                {
                    int x;

                    da.UpdateCommand = new SqlCommand("UPDATE LoginUser SET PASSWORD=@Password", sc);
                    da.UpdateCommand.Parameters.Add("@Password", SqlDbType.VarChar).Value = TxtPassword.Text;
                    //sc.Open();
                    x = da.UpdateCommand.ExecuteNonQuery();
                    sc.Close();

                    if (x >= 1)
                    MessageBox.Show("Password reset");
                }
                else
                {
                    MessageBox.Show("Password not reset");
                }
            }
        }
    }
}


Please
1) Show me how to solve the problem. It would be easy for me if you use code examples
2) Tell me is this code going to work
3) Iam open to all improvements
Posted
Updated 9-Dec-11 23:08pm
v3
Comments
palraj001 10-Dec-11 4:52am    
sc.Open();// remove this and try
x = da.UpdateCommand.ExecuteNonQuery();
palraj001 10-Dec-11 4:52am    
bcos already in try block connection is opened .

1 solution

You are opening the connection twice.
Once
C#
try
{
    sc.Open();
}

And then
sc.Open();


Only one open connection statement is required.

Or else you can check if your connection is already open - If sc.State = adStateOpen.
 
Share this answer
 
v2
Comments
Maazatron 10-Dec-11 5:08am    
I removed the second open connection but now i am getting another error which says "There is already an open DataReader associated with this Command which must be closed first"

Now what???
Abhinav S 10-Dec-11 7:14am    
Comment out sc.Close(); as well.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900