Click here to Skip to main content
15,907,497 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
dbCommand = new SqlCommand("SELECT Game.GameId,Location,Date,Time FROM Game,Results WHERE Game.GameId = Results.GameId AND CountryId ='" + cboCountryName.SelectedValue + "'", dbConnection);

            if (dbConnection.State = ConnectionState.Closed)
            {
                dbConnection.Open();
            }
            dbread = dbCommand.ExecuteReader();
            while (dbread.Read())
            {
              
            }
Posted
Updated 10-Oct-11 19:17pm
v2

Hi!
Don't use loop to iterate through SqlDataReader...
You should create a DataTable
You fill it though data reader and bind your table to grid
 DataTable table = new DataTable();

 table.Load( dbCommand.ExecuteReader());

dataGrid.DataSource = table;
 
Share this answer
 
C#
con.Open(); 
SqlDataReader reader = cmd.ExecuteReader();
GridView1.DataSource = reader;
GridView1.DataBind();
reader.Close();
con.Close();
 
Share this answer
 
C#
SqlConnection obj_Conn = new SqlConnection();
            obj_Conn.ConnectionString = strConn;

            obj_Conn.Open();
            SqlCommand obj_Cmd = new SqlCommand(strCmd, obj_Conn);
            SqlDataReader obj_Reader = obj_Cmd.ExecuteReader();

            DataTable dt = new DataTable();
            dt.Columns.Add("Id");
            dt.Columns.Add("FName");
            dt.Columns.Add("LName");

         
            while (obj_Reader.Read())
            {
                DataRow row = dt.NewRow();
                row["Id"] = obj_Reader["Id"];
                row["FName"] = obj_Reader["FName"];
                row["LName"] = obj_Reader["LName"];
                dt.Rows.Add(row);
            }
            dataGridView1.DataSource = dt;
 
Share this answer
 
DataTable dt = new DataTable();
      dt.Columns.Add("Id");
      dt.Columns.Add("Name");



      while (obj_Reader.Read())
      {
          DataRow dr= dt.NewRow();
          dr["Id"] = obj_Reader["Id"];
          dr["Name"] = obj_Reader["Name"];
          dt.Rows.Add(dr);
      }
      dataGridView1.DataSource = dt;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900