Quick DataGridView Quiz

A buddy of mine recently struggled with one of those "I can't beleive I didn't see it" issues. It went something like this ...


  • you have a C# Windows Form application
  • a WinForm
  • a DataGridView
  • a button called button1
  • a valid connection string
  • a valid sql statement

What's wrong with this code, if anything?:

    1         private void button1_Click(object sender, EventArgs e)

    2         {

    3             SqlConnection cn = new SqlConnection(@"server=VSTSB2;database=northwind;integrated security=true;");

    4             SqlCommand cmd = new SqlCommand("select * from customers", cn);

    5             SqlDataAdapter adpt = new SqlDataAdapter(cmd);

    6             DataSet ds = new DataSet();

    7             adpt.Fill(ds);

    8             dataGridView1.DataSource = ds.Tables[0];

    9         }


Tweet Post Share Update RSS

Hi, I'm John Papa. I author this blog, create courses for Pluralsight and am a Google Developer Expert and Microsoft Regional Director. I travel speaking at events and train technology thought leaders