Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In table record as follows


SQL
CourseCode    CoruseName     Studentid    City
   RM          Remunration     45555        1
   REO         Revalidation    88787        1
   CTF         Tanker          98889        1
   ECDIS       Practical       88877        1
   AMOS        Manual          78798        1
   DFC         Familarazation  87899        2



i am displaying the above data in to Gridview. code as follows

C#
string Con= "Server=(local);initial catalog=Testing;Trusted_Connection=True";
SqlConnection sqlcon= new SqlConnection(Con);
SqlCommand cmd = new SqlCommand();
SqlDataReader dr;
cmd.CommandText = "select * from Course";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlcon;
             sqlcon.Open();
             dr= cmd.ExecuteReader();
             if (dr.HasRows)
             {
                 if (reader.Read())
                 {
                     string City = dr[6].ToString();
                     if (City== "1") ;
                    {
                      Griview.Datasource = dr;
                      Gridview.databind();
                    }
                  }
             }





i am checking if City 1 means. that city 1 all record to be displayed in gridview.

When i execute the above code in Gridview Output as follows

SQL
CourseCode    CoruseName     Studentid    City

  RM          Remunration     45555        1


in the above gridview only first record only displayed. But not all the records in table is not displaying.


But i want the output in Gridview as follows


SQL
CourseCode    CoruseName     Studentid    City
    RM          Remunration     45555        1
    REO         Revalidation    88787        1
    CTF         Tanker          98889        1
    ECDIS       Practical       88877        1
    AMOS        Manual          78798        1


please help me what is the problem in my code
Posted
Updated 15-Aug-16 2:51am
v2
Comments
F-ES Sitecore 15-Aug-16 8:01am    
Rather than use ExecuteReader to get a SqlDataReader you're probably better to use a DataSet instead (google how to populate a dataset from sqlcommand). That way you can examine the first row's value via the dataset and if it is 1 set the datasource to be the dataset and it'll have all the values. Your existing logic is flawed as it is processing the data line by line and doing the same thing each time, where you only want to do this once.
Richard MacCutchan 15-Aug-16 8:47am    
That is because a DataReader only returns a single record, not an enumeration.

1 solution

Try this code to get required data:

C#
string Con= "Server=(local);initial catalog=Testing;Trusted_Connection=True";
SqlConnection sqlcon= new SqlConnection(Con);
SqlCommand cmd = new SqlCommand();
SqlDataReader dr;
cmd.CommandText = "select * from Course where City=1";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlcon;
             sqlcon.Open();
             dr= cmd.ExecuteReader();
             if (dr.HasRows)
             {
                 if (reader.Read())
                 {  
                    Griview.Datasource = dr;
                    Gridview.databind();
                    
                  }
             }
 
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