title: Connecting to Database using GridView in C#
body: <p>I want to display the result in GridView and I am unable to do so. I am trying to connect my C# windows form application to the database that I created in SQL. Please tell me if there is anything else I need to put in my class to fix it. I am not getting any errors but I am its still not getting any result when  I click "See all Athletes".</p>
  
  <pre><code>namespace Olympics
  {
  
    public partial class Form1 : Form
    {
      private string connectionString;
  
      public Form1()
      {
          InitializeComponent();
          connectionString = "Data Source=ddb04;Initial Catalog=Vikram;Integrated Security=SSPI;";
      }
  
      private void label1_Click(object sender, EventArgs e)
      {
  
      }
  
      private void Form1_Load(object sender, EventArgs e)
      {
  
      }
  
  
      public class Athlete
      {
          private string Athlete_Firstname;
          private string Athlete_Lastname;
          private string Athlete_ID;
          private string countryname;
  
          public Athlete()
          {
  
          }
  
  
          public Athlete(string firstname, string lastname, string ID, string country)
          {
              Athlete_Firstname = firstname;
              Athlete_Lastname = lastname;
              Athlete_ID = ID;
              countryname = country;
          }
  
          public string firestname
          {
              get { return Athlete_Firstname; }
              set
              {
                  Athlete_Firstname = value;
  
              }
          }
  
          public string lastname
          {
              get { return Athlete_Lastname; }
              set
              {
                  Athlete_Lastname = value;
  
              }
          }
  
          public string ID
          {
              get { return Athlete_ID; }
              set
              {
                  Athlete_ID = value;
  
              }
          }
  
          public string country
          {
              get { return countryname; }
              set
              {
                  countryname = value;
  
              }
          }
  
      }
  
  
  
  
  
  
      private void seeAthletesButton_Click(object sender, EventArgs e)
      {
          //here I access my project's connectionm string...
  
          //string sql = "select Athlete_Firstname, Athlete_Lastname, Athlete_ID, Country from Athlete;";
  
          //here I create new SQLConnection Object...
  
          using (SqlConnection connection = new SqlConnection(connectionString))
          {
  
              connection.Open();
              //here I create new SQLCommand Object.
  
              using (SqlCommand command = new SqlCommand("select Athlete_Firstname, Athlete_Lastname, Athlete_ID, Country from Athlete;", connection))
              {
                  //command.Open();
                  SqlDataReader reader = command.ExecuteReader();
                  List&lt;Athlete&gt; atheletes = new List&lt;Athlete&gt;();
  
                  while (reader.Read())
                  {
  
  
                      string atheleteFirstName = reader.GetString(0);    // Athlete_Firstname string
                      string atheleteLastName = reader.GetString(1);  // Athlete_Lastname string
                      string atheleteId = reader.GetString(2); // Athlete_ID int
                      string atheleteCountry = reader.GetString(3);
  
                      Athlete athelete = new Athlete();
                      athelete.firestname = atheleteFirstName;
                      athelete.lastname = atheleteLastName;
                      athelete.ID = atheleteId;
                      athelete.country = atheleteCountry;
  
                      atheletes.Add(athelete); // List of Athelete objects populated with values from database
                  }
                  DataGridView atheleteDataGRidView = new DataGridView();
  
                  atheleteDataGRidView.DataSource = atheletes;
  
                }
          }
      }
  
  
  
  
  
  
  
      private void label4_Click(object sender, EventArgs e)
      {
  
      }
  
      private void button1_Click(object sender, EventArgs e)
      {
  
      }
  
      private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
      {
  
      }
  
  
  
  
  
      private void saveButton_Click(object sender, EventArgs e)
      {
  
          SqlConnection connection = new SqlConnection(connectionString);
  
          connection.Open();
  
          string sql = "insert into Athlete (Athlete_Firstname, Athlete_Lastname, Athlete_ID, Country) values (@firstName, @lastName, @athleteId, @country);";
  
  
          SqlCommand command = new SqlCommand(sql, connection);
  
          command.Parameters.Add("@firstName", SqlDbType.VarChar);
          command.Parameters["@firstName"].Value = firstName.Text;
  
          command.Parameters.Add("@lastName", SqlDbType.VarChar);
          command.Parameters["@lastName"].Value = lastName.Text;
  
          command.Parameters.Add("@athleteId", SqlDbType.VarChar);
          command.Parameters["@athleteId"].Value = athleteId.Text;
  
          command.Parameters.Add("@country", SqlDbType.VarChar);
          command.Parameters["@country"].Value = countryName.Text;
  
          command.ExecuteNonQuery();
  
  
          connection.Close();
      }
  </code></pre>
  
  <p>Any suggestion?</p>