Login details Code Error

Mohamed Rafi N 106 Reputation points
2022-03-22T02:35:57.187+00:00

Sir, I have studied and tried to put login page but it shows code error please tell me the correction sir
It shows error: System.InvalidOperationException: 'The connection is already and also code error
private void button14_Click(object sender, EventArgs e)
{
if (textBox9.Text != "" && textBox10.Text != "")
{
string connectionString;
MySqlConnection cnn;
connectionString = @"Data Source=localhost;Initial Catalog=testDB;User ID=root;Password=mysql";
cnn = new MySqlConnection(connectionString);
cnn.Open();
string id = textBox9.Text;
string password = textBox10.Text;
textBox9.Text = "";
textBox10.Text = "";
string query = "select * from login where userid=@userid,password=@Lee ,confirmpassword=@confirmpassword where loginid=@loginid is same";
//string query = "update employee set employee_name=@employee_name,employee_salary=@employee_salary where employee_id=@employee_id";
using (MySqlCommand cmd = new MySqlCommand(query))
{
cmd.Parameters.AddWithValue("@userid", id);
//cmd.Parameters.AddWithValue("@employee_id", Convert.ToInt32(id));
cmd.Parameters.AddWithValue("@Lee ", password);
//cmd.Parameters.AddWithValue("@confirmpassword", confirmpassword);
cmd.Connection = cnn;
cnn.Open();
cmd.ExecuteNonQuery();
DialogResult dr = MessageBox.Show("Are you sure to Login now?", "Confirmation", MessageBoxButtons.YesNo);
if (dr == DialogResult.Yes)
{
MessageBox.Show("Login Successfully");
cnn.Close();
this.Hide();
Form2 f2 = new Form2();
f2.ShowDialog();
}
else if (dr == DialogResult.No)
{
MessageBox.Show("Please Enter Correct Login details");
}
}
}
else
{
MessageBox.Show("Please Enter details to Login");
}
}
}

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,149 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,249 questions
Azure Database for MySQL
Azure Database for MySQL
An Azure managed MySQL database service for app development and deployment.
713 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,223 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Anurag Sharma 17,571 Reputation points
    2022-03-22T04:42:58.16+00:00

    Hi @Mohamed Rafi N , welcome to Microsoft Q&A forum.

    It seems the code snippet you provided have multiple issues.

    First issue is related to 'The connection is already open'. This is because you are using cnn.Open() at multiple places in the code. Please remove it from top of code and have it at 2nd place.

    Second issue is related to below line:

    string query = "select * from login where userid=@userid,password=@password,confirmpassword=@confirmpassword where loginid=@loginid is same";  
    

    Please use below corrected line:

    string query = "select * from login where userid=@userid and password=@password and confirmpassword=@confirmpassword and loginid=@loginid";  
    

    Please try this once and let me know if you face any other issue.

    ----------

    If answer helps, please mark it 'Accept Answer'