I'm trying to make a C # application with PostgreSQL SGDB, but whenever I close the connection it gives the following error:
Object reference not set to an instance of an object.
I'm using mono.security
and npgsql2
but I could not add the current one.
//Inserir registros
public void ExecutarSQL(string sql)
{
try
{
using (NpgsqlConnection pgsqlConnection = new NpgsqlConnection(connString))
{
//Abra a conexão com o PgSQL
pgsqlConnection.Open();
string cmdInserir = sql;
using (NpgsqlCommand pgsqlcommand = new NpgsqlCommand(cmdInserir, pgsqlConnection))
{
int i = pgsqlcommand.ExecuteNonQuery();
}
}
}
catch (NpgsqlException ex)
{
MessageBox.Show("npgsql");
}
catch (Exception ex)
{
MessageBox.Show("exp");
}
finally
{
MessageBox.Show("fecha");
pgsqlConnection.Close();
}
}
In the case in question I can pass an SQL to the function and it inserts the data however when it falls into finally
and arrives at pgsqlConnection.Close()
the above error appears.