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?
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];
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With