Lucas Silva
Lucas Silva

Reputation: 13

Trying to update column on C# using Npgsql

I'm trying to update the value of a column using Npgsql (PostgreSQL) and Visual Studio 2015. This is a "Forgot your Password?" form.

The user needs to enter two variables in order to accomplish the reset (an if condition): 1. enter the SSN (aka RG in Brazil); and 2. enter a user-specific code that is unchangeable.

What happens is: when I try to change the value (using the command below), nothing happens.

NpgsqlCommand command = new NpgsqlCommand("UPDATE dbschema.dbtable SET senha = '" + textBox2.Text + "' WHERE rg = '" + textBox2.Text + "'");

The if condition executes perfectly (I tied the if command to a Message box, where it shows that the password has been successfully reseted - a sign that the if condition was indeed executed) - the only thing that doesn't happen right now is the password update (which might be a minor feature in the release, as I'd prefer to reset myself on the database). I tried several changes in the code, with no success yet.

Thanks in advance for the help.

Lucas

Upvotes: 1

Views: 1903

Answers (2)

Mhd
Mhd

Reputation: 2978

Had you tried to execute NpgsqlCommand?

Also, You better use parametrized queries.

See this example: INSERT data from Textbox to Postgres SQL

Upvotes: 0

Owen Pauling
Owen Pauling

Reputation: 11871

You have to actually open a database connection and execute the command, e.g.:

using (var conn = new NpgsqlConnection(<connection string>)) {
    var command = new NpgsqlCommand("UPDATE dbschema.dbtable SET senha = '" + textBox2.Text + "' WHERE rg = '" + textBox2.Text + "'", conn);
    conn.Open();
    command.ExecuteNonQuery();
}

Note that the way you pass parameters is also open to SQL injection attacks. You should look at parameterized queries.

Upvotes: 1

Related Questions