I am trying to make my form contain all the rows of a table of a Postgres database, for this I am using a DataGridView with 2 columns (name and password), and searching the database for the values. But I'm new to this, and i don't know how to take db data and put it in the DataGrid.
Basically I wanted to make the same pgAdmin visual table be shown in my C# application form.
Postgre.cs:
public static NpgsqlDataReader listUsers()
{
string sqlCommand = "SELECT name, senha FROM users";
NpgsqlCommand cmd = new NpgsqlCommand(sqlCommand, Connect());
NpgsqlDataReader reader = cmd.ExecuteReader();
return reader;
}
frmAdmin:
public partial class frmAdmin : Form
{
public frmAdmin()
{
dataGridView1.DataSource = Postgre.listUsers();
InitializeComponent();
}
}
I took part of the code from here
I think it should work, but when I run I get a System.StackOverflowException
.
Edit1: I solved the overflow problem, but now the problem is "System.NullReferenceException", in the dataGridView1.DataSource = Postgre.listUsers()
. I updated the code.
try it:
dataGridView1.DataSource = reader;