connection c # with postgres


I have a problem with this connection to pgsql. I do not know what is happening. I placed a breakpoint in each line of code and it enters all the lines but the user does not recognize me, I do not know if I'm doing it wrong or I'm missing something.

I already put the using npgsql; and I made a class with the connection string to call only the string and nothing. My conf of pgsql is exact, I already tried everything my knowledge has. I am using a WPF project and I also have the mahapps installed to give me metro and npgsql styles so I can download it from Nuget, I do not know if that helps to determine what is happening.

Here I leave the code fragment.

private void btnaceptar_Click(object sender, RoutedEventArgs e)
    bool blnfound = false;

    NpgsqlConnection conn = new NpgsqlConnection("Server=localhost;Port=5432; User Id=postgres;Password=1234;Database = systemBD");
    NpgsqlCommand cmd = new NpgsqlCommand("Select * from usuario where cod_usu = '" + txt1 + "' and con_usu = '" + txt2 + "' ", conn);
    NpgsqlDataReader dr = cmd.ExecuteReader();
    if (dr.Read())
        blnfound = true;
        modulos form = new modulos();
    if (blnfound == false)
        MessageBox.Show("Usuario o Contraseña Incorrecta", "Mensaje de Alerta", MessageBoxButton.OK);
asked by Gilberto Asuaje 04.04.2016 в 20:03

4 answers


It has been a long time but this problem was solved by changing the version of VS xpress to the complete one, but from the community .. thanks to all.saludos

answered by 17.07.2017 / 07:14

As I understand you, the application does not get any connection error. It's just not validating the user of your user table. If this is correct, check this line of code

NpgsqlCommand cmd = new NpgsqlCommand("Select * from usuario where cod_usu = '" + txt1 + "' and con_usu = '" + txt2 + "' ", conn);

Here you are validating the cod_usu column twice with txt1 and txt2. What I think you should do is validate cod_usu = txt1 and password = txt2.

Now, the way you're concatenating the command

"Select * from usuario where cod_usu = '" + txt1 + "' and con_usu = '" + txt2 + "' ", conn);

is not very recommendable because your application is very exposed to an SQL injection attack, so I recommend you read this post link that is SQL Server but it will still be useful for what I am saying.

answered by 04.04.2016 в 23:00

Notice if it works in the following way, as I have it configured instead of Server , use HOST and a couple more data:

 NpgsqlConnection conn = new NpgsqlConnection("HOST=;Port=5432; User Id=postgres;Password=1234;Database = systemBD;TIMEOUT=15;POOLING=True;MINPOOLSIZE=1;MAXPOOLSIZE=20;COMMANDTIMEOUT=20");
answered by 04.04.2016 в 20:16

Check this, it's similar:

using System;
using Npgsql; //Npgsql .NET Data Provider for PostgreSQL

class Sample
    static void Main(string[] args)
        // Specify connection options and open an connection
        NpgsqlConnection conn = new NpgsqlConnection(
            "Server=;User Id=postgres;" + 

        // Define a query
        NpgsqlCommand cmd = new NpgsqlCommand("select city from cities", conn);

        // Execute a query
        NpgsqlDataReader dr = cmd.ExecuteReader();

        // Read all rows and output the first column in each row
        while (dr.Read())
        Console.Write("{0}\n", dr[0]);

        // Close connection
answered by 17.10.2016 в 19:31