question

MohamedRafiN-6400 avatar image
0 Votes"
MohamedRafiN-6400 asked AnuragSharma-08 commented

Login details Code Error

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=@password,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("@password", 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");
}
}
}

dotnet-csharpdotnet-aspnet-core-generaldotnet-aspnet-generalazure-database-mysqldotnet-aspnet-webforms
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Do you really have and need the confirmpassword things? And you must probably use ExecuteReader or ExecuteScalar (instead of ExecuteNonQuery) and check the results.


1 Vote 1 ·

No i don't need confirm password, but i got error like if i give wrong userid and password means it also login but i need if userid and password is not match it should be login error

0 Votes 0 ·

Sir, Its not coming sir, if i remove connection string from other button means it how will it work for other buttons? please suggest me the corrections




0 Votes 0 ·

1 Answer

AnuragSharma-08 avatar image
0 Votes"
AnuragSharma-08 answered MohamedRafiN-6400 commented

Hi @MohamedRafiN-6400, 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'



· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MySql.Data.MySqlClient.MySqlException: 'Fatal error encountered during command execution.'

MySqlException: Parameter '@loginid' must be defined.


0 Votes 0 ·

This error is because you are not passing the value of loginid. if you check the code you are passing other values like userid and password.

 cmd.Parameters.AddWithValue("@userid", id);
 //cmd.Parameters.AddWithValue("@employee_id", Convert.ToInt32(id));
 cmd.Parameters.AddWithValue("@password", password);
 //cmd.Parameters.AddWithValue("@confirmpassword", confirmpassword);


Same way you need to map the loginid as well with some textbox value or remove it from the below query:

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

Ok sir, how and what i have to modify in my code? Pls give correct code

0 Votes 0 ·
Show more comments