Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populate a datagridview with sql query results

I'm trying to present query results, but I keep getting a blank data grid. It's like the data itself is not visible

Here is my code:

 private void Employee_Report_Load(object sender, EventArgs e)  {      string select = "SELECT * FROM tblEmployee";      Connection c = new Connection();      SqlDataAdapter dataAdapter = new SqlDataAdapter(select, c.con); //c.con is the connection string      SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);       DataTable table = new DataTable();      table.Locale = System.Globalization.CultureInfo.InvariantCulture;      dataAdapter.Fill(table);      bindingSource1.DataSource = table;       dataGridView1.ReadOnly = true;              dataGridView1.DataSource = bindingSource1; } 

What's wrong with this code?

like image 388
user2023203 Avatar asked Aug 07 '13 20:08

user2023203


2 Answers

Here's your code fixed up. Next forget bindingsource

 var select = "SELECT * FROM tblEmployee";  var c = new SqlConnection(yourConnectionString); // Your Connection String here  var dataAdapter = new SqlDataAdapter(select, c);    var commandBuilder = new SqlCommandBuilder(dataAdapter);  var ds = new DataSet();  dataAdapter.Fill(ds);  dataGridView1.ReadOnly = true;   dataGridView1.DataSource = ds.Tables[0]; 
like image 78
Don Thomas Boyle Avatar answered Oct 06 '22 00:10

Don Thomas Boyle


String strConnection = Properties.Settings.Default.BooksConnectionString; SqlConnection con = new SqlConnection(strConnection);  SqlCommand sqlCmd = new SqlCommand(); sqlCmd.Connection = con; sqlCmd.CommandType = CommandType.Text; sqlCmd.CommandText = "Select * from titles"; SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);  DataTable dtRecord = new DataTable(); sqlDataAdap.Fill(dtRecord); dataGridView1.DataSource = dtRecord; 
like image 24
sayed hashim Avatar answered Oct 06 '22 00:10

sayed hashim