简体   繁体   中英

ASP.NET C# Validating username and password

Im trying to validate username and password from an MySql server. Login validation is working, but I can't for the life of me figure out why the "Create new user" validation isn't working.

Here are the code for registering new user. What happens is;

catch (Exception)
    {
        Label1.Text = "Brukernavnet er allerede i bruk";
    } 

Seems like this part ^ is ruining it for me somehow, whenever i test run this code I get this message.

protected void newBtn_Click(object sender, EventArgs e)
    {
        String cs = "Database=trafikkskole; User=user; Password=password";
        MySqlConnection dbconnect = new MySqlConnection(cs);

        try
        {

            dbconnect.Open();
            cmd.CommandText = "INSERT INTO user (username, password) VALUES (@un, @pw)";
            cmd.Parameters.AddWithValue("@un", inputUser.Text);
            cmd.Parameters.AddWithValue("@pw", inputPw.Text);
            cmd.Connection = dbconnect;
            int a = cmd.ExecuteNonQuery();

            if (a > 0)
            {
                Label1.Text = "Gratulerer! Du har nå laget en bruker!";
            }

            else
            {
                Label1.Text = "ERROR";
            }

        }

        catch (Exception)
        {
            Label1.Text = "Brukernavnet er allerede i bruk";
        }

        finally
        {
            dbconnect.Close();
        }
    }

}

EDIT:

If I try it like this:

protected void newBtn_Click(object sender, EventArgs e)
    {
        String cs = "Database=trafikkskole; User=root; Password=root";
        MySqlConnection dbconnect = new MySqlConnection(cs);

        String sql = "SELECT * FROM user";
        MySqlCommand cmd = new MySqlCommand(sql, dbconnect);
        da = new MySqlDataAdapter(cmd);
        MySqlCommandBuilder cb = new MySqlCommandBuilder(da);
        ds = new DataSet("TEST");
        da.Fill(ds, "user");
        Response.Write(ds.Tables["user"].Rows.Count);     

        try
        {

            dbconnect.Open();
            cmd.CommandText = "INSERT INTO user (username, password) VALUES (@un, @pw)";
            cmd.Parameters.AddWithValue("@un", inputUser.Text);
            cmd.Parameters.AddWithValue("@pw", inputPw.Text);
            cmd.Connection = dbconnect;
            int a = cmd.ExecuteNonQuery();

            if (a > 0)
            {
                Label1.Text = "Gratulerer! Du har nå laget en bruker!";
            }

            else
            {
                Label1.Text = "ERROR";
            }

        }



        catch (Exception Exception)
        {
            Label1.Text = "Brukernavnet er allerede i bruk";

        }

        finally
        {
            dbconnect.Close();
        }
    }

}

This ends up with the possibility of making a user without username or password.

There are a number of things that could be going wrong. You should examine the exception.message to get insights as to what it could be.

For example, put a break point in the catch statement and see if the exception thrown for things like... does the username already exist and SQL is throwing an error. ... or are the username/password null, too long, etc...

Regardless, change the catch statement to catch(Exception exception) and see what the exception is.

I want to thank everyone for trying, found a working solution, will post it here for future reference.

protected void newBtn_Click(object sender, EventArgs e)

        {
        String cs = "Database=trafikkskole; User=root; Password=root";
        MySqlConnection dbconnect = new MySqlConnection(cs);

        try
        {

            if (!string.IsNullOrWhiteSpace(inputUser.Text) && !string.IsNullOrWhiteSpace(inputPw.Text))
            {
                dbconnect.Open();
                Label1.Text = "Gratulerer! Du har nå laget en bruker!";
                string qry = "INSERT INTO user(username, password) VALUES (@un, @pw)";
                cmd = new MySqlCommand(qry, dbconnect);
                cmd.Parameters.AddWithValue("@un", inputUser.Text);
                cmd.Parameters.AddWithValue("@pw", inputPw.Text);
                cmd.Connection = dbconnect;
                cmd.ExecuteNonQuery();
            }

            else
            {
                Label1.Text = "ERROR";
            }

        }

        catch (Exception)
        {
            Label1.Text = "Brukernavnet er allerede i bruk";

        }


        finally
        {
            dbconnect.Close();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM