title: C# Visual Studio - Outputting SQL table data to Datagridview
body: <p>Apologies if this has been covered. The examples I seemed to find were more complicated than I need it to be</p>
  
  <p>I'm trying to output the entire contents of an SQL table to a DatagridView when a button "Display All Records" is clicked</p>
  
  <p>I'm having a bit of trouble, here is what I have so far</p>
  
  <pre><code>  private void Button_Click_2(object sender, RoutedEventArgs e)
          {
              {
  
                  string query = "select * from student";
                  SqlCommand cmd = new SqlCommand(query, con);
                  con.Open();
  
  
                  SqlDataAdapter da = new SqlDataAdapter(cmd);
  
                  da.Fill(DataGridView);
                  con.Close();
                  da.Dispose();
              }
  </code></pre>
  
  <p>Here is all my code if you need to refer</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  using System.Data.SqlClient;
  using System.Text.RegularExpressions;
  
  namespace WpfApplication4
  {
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
      public partial class MainWindow : Window
      {
          SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True");
  
  
  
          public MainWindow()
          {
              InitializeComponent();
              establishConnection();
  
  
          }
  
          private void Button_Click(object sender, RoutedEventArgs e)
          {
  
  
              Boolean postSuccess = false;
  
              if (validation() == true)
              {
  
                  SqlCommand details = new SqlCommand("INSERT INTO Student (Firstname, LastName, MatriculationNo, GradeOne, GradeTwo, GradeThree) VALUES (@FirstName, @LastName, @MatriculationNo, @GradeOne, @GradeTwo, @GradeThree)", con);
                  details.Parameters.AddWithValue("@FirstName", firstnameTextbox.Text);
                  details.Parameters.AddWithValue("@LastName", lastnameTextbox.Text);
                  details.Parameters.AddWithValue("@MatriculationNo", matriculationnoTextbox.Text);
                  details.Parameters.AddWithValue("@GradeOne", Component1Textbox.Text);
                  details.Parameters.AddWithValue("@GradeTwo", Component2Textbox.Text);
                  details.Parameters.AddWithValue("@GradeThree", Component3Textbox.Text);
                  con.Open();
                  details.ExecuteNonQuery();
                  postSuccess = true;
  
                  if (postSuccess)
                  {
                      MessageBox.Show("Details entered succesfully!");
                      firstnameTextbox.Clear();
                      lastnameTextbox.Clear();
                      matriculationnoTextbox.Clear();
                      Component1Textbox.Clear();
                      Component2Textbox.Clear();
                      Component3Textbox.Clear();
  
                  }
  
                  else
                  {
                      MessageBox.Show("Data not entered succesfully, please check DB connection");
                  }
                  con.Close();
              }
  
          }
          private void establishConnection()
          {
              try
              {
                  con.Open();
                  System.Diagnostics.Debug.Print("Connected to SQL database");
  
                  connectionLabel.Content = "Connected to SQL Database";
  
                  con.Close();
              }
              catch (Exception)
              {
  
                  connectionLabel.Content = "not connected to sql database";
              }
          }
  
          private void Button_Click_1(object sender, RoutedEventArgs e)
          {
              firstnameTextbox.Clear();  //Clears the first name textbox
              lastnameTextbox.Clear();   //Clears the last name textbox
              matriculationnoTextbox.Clear(); //Clears the Matriculation Number textbox
              Component1Textbox.Clear(); //Clears the component one textbox
              Component2Textbox.Clear(); //Clears the component two textbox
              Component3Textbox.Clear(); //Clears the component three textbox
          }
  
  
          private Boolean validation()
          {
              if (!Regex.Match(firstnameTextbox.Text, "^[A-Z][a-zA-Z]*$").Success)
              {
                  MessageBox.Show("Please Enter a valid First Name");
                  firstnameTextbox.Clear();
                  return false;
              }
              else
              {
                  return true;
              }
  
          }
  
          private void Button_Click_3(object sender, RoutedEventArgs e)
          {
              con.Open(); 
              string sql = @"DELETE FROM Student;"; //Deleting all from Student table
              SqlCommand purge = new SqlCommand(sql, con);
              MessageBox.Show("Are you sure you want to purge the entire contents of the database?"); //Prompting user to make sure they want to delete
              purge.ExecuteNonQuery(); //Execute purge query
  
  
          }
  
          private void Button_Click_2(object sender, RoutedEventArgs e)
          {
              {
  
                  string query = "select * from student";
                  SqlCommand cmd = new SqlCommand(query, con);
                  con.Open();
  
                  // create data adapter
                  SqlDataAdapter da = new SqlDataAdapter(cmd);
                  // this will query your database and return the result to your datatable
                  da.Fill(DataGridView);
                  con.Close();
                  da.Dispose();
              }
  
  
  
  
  
  
  
  
  
          }
  
  
  
  
  
      }
  }
  </code></pre>
  
  <p>Any help would be much appreciated, thank you</p>