Click here to Skip to main content
16,014,650 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void button1_Click(object sender, EventArgs e)
        {
            using (MySqlConnection con = new MySqlConnection("Server=localhost;UID=root;Database=db_cignal"))
            {
                con.Open();
                MySqlCommand command = new MySqlCommand();
                command.Connection = con;
                command.CommandText = "SELECT * FROM Admin";
                command.Prepare();
                MySqlDataReader read = command.ExecuteReader();

                while (read.Read())
                {

                    if (txtName.Text.Equals(read["Username"].ToString()) && txtPass.Text.Equals(read["Password"].ToString()))
                    {
                        MessageBox.Show("***Login Successfully***");
                        this.Hide();
                        Administrator form2 = new Administrator();
                        form2.Closed += (s, args) => this.Close();
                        form2.Show();
                    }
                    else
                    {
                        MessageBox.Show("Please input the correct username or password..");
                        txtName.Text = "";

                        txtPass.Text = "";
                    }

                }
                read.Close();
                con.Close();
                }
            }
Posted
Updated 22-Mar-15 23:10pm
v2

1 solution

The most likely reason is the connection string: fixed strings in your code often give problems. You don't say what you were doing before when it worked, or what has changed since then, but this kind of thing is often because you specify "localhost" as the server, and then move to a different computer - where the DB server is not installed.

I'd suggest that you move your connection string to a config file of some description so you can alter it without recompilation, and look at the environment in which you are trying to run the application. Specifically, look at the DB servers installed, and see if you need a different connection string - a different server name, an actual login, and so forth.

We can't do that for you - we can't access your systems! :laugh:

BTW: Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^]
 
Share this answer
 
v2
Comments
_bluRe_ 23-Mar-15 5:51am    
thanks griff. can u suggest a best way to put a connection string?
OriginalGriff 23-Mar-15 6:14am    
It looks like a WinForms app? If so, then just use the Setting.Settings file: you'll find it in VS under the Project...Properties... branch in the Solution Explorer pane.
Create a new ConnectionString setting, and access it:

string strConnect = Properties.Settings.Default.ConnectionString;

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