1

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".

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<Athlete> atheletes = new List<Athlete>();

                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();
    }

Any suggestion?

3
  • 2
    it might help to display or tell us what the error is that you are getting Commented Sep 14, 2012 at 21:23
  • try looking at the following 2 links msdn.microsoft.com/en-us/library/… connectionstrings.com/sql-server-2005 Commented Sep 14, 2012 at 21:27
  • 2
    I see you create a gridview, where did you add it to your form. Also tag your question with ASP.Net or Winforms Commented Sep 14, 2012 at 21:27

2 Answers 2

2

You need to write

atheleteDataGRidView.DataSource = atheletes;
atheleteDataGRidView.DataBind();

in private void seeAthletesButton_Click(object sender, EventArgs e)

Sign up to request clarification or add additional context in comments.

Comments

0

Apart from what Oedum has said, you are accessing the ID with SqlDataReader.GetString which throws an InvalidCastException when it can not be casted to a string. But you are commenting yourself that it's DataType is int.

No conversions are performed; therefore, the data retrieved must already be a string.

So this is probably better:

string atheleteId = reader.GetInt32(2).ToString(); // Athlete_ID int

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.