2016-07-11 16 views
0

Sie müssen ein kleiner Fehler irgendwo in meinem Code sein, dass ich nicht sehe, warum auf meiner Webseite die Gridview nur eine Zeile ausgibt, wenn ich die Abfrage kenne Werke/ASP.NET Gridview gibt nicht alle Daten für eine bestimmte Abfrage aus

Both rows from management studio

Only one row in ASP page

herer mein C#

string AdminID = Request.QueryString["ID"]; 
string AdminsCurrentLocation = Request.QueryString["Location"]; 
//retrieve admin location 

    try 
    { 
     string connectionString = "Data Source=SQL5027.HostBuddy.com;Initial Catalog=DB_A05369_WATERQ;User Id=DB_A05369_WATERQ_admin;Password=waterqws1"; 
     // 
     // Create new SqlConnection object. 
     // 
     using (SqlConnection connection5 = new SqlConnection(connectionString)) 
     { 


      connection5.Open(); 

      using (SqlCommand command = new SqlCommand("SELECT * FROM [DB_A05369_WATERQ].[dbo].[S_CONTROL] WHERE LOGIN = '" + AdminID + "'", connection5)) 
      { 
       // 
       // Invoke ExecuteReader method. 
       // 
       using (SqlDataReader reader2 = command.ExecuteReader()) 
       { 
        reader2.Read(); 
        TempLocationIdbox.Text = (reader2["ALL_LOCATION_ACCESS"].ToString()); 
       } 

      } 
      connection5.Close(); 
     } 
    } 
    catch (Exception ex) { } 

    if(TempLocationIdbox.Text == "Y") 
    { 
     string strSQLconnection = "Data Source=SQL5027.HostBuddy.com;Initial Catalog=DB_A05369_WATERQ;User Id=DB_A05369_WATERQ_admin;Password=waterqws1"; 
     SqlConnection sqlConnection = new SqlConnection(strSQLconnection); 
     SqlCommand sqlCommand = new SqlCommand("SELECT DISTINCT ACT.ROW_ID , ACT.CREATED , MEM.FIRST_NAME , MEM.LAST_NAME , LOC.NAME , CAT.NAME , SER.NAME , EMP.FIRST_NAME , EMP.LAST_NAME , SER.DURATION , ACT.CASH , COS.NAME , ACT.COMMENTS FROM " + 
               "S_ACTIVITY ACT, S_LOCATION LOC, S_CATEGORY CAT, S_EMPLOYEE EMP, S_SERVICE SER, S_COST_CODE COS, S_MEMBER MEM " + 
               "WHERE ACT.EMPLOYEE_ID = EMP.ROW_ID AND ACT.SERVICE_ID = SER.ROW_ID AND ACT.CATEGORY_ID = CAT.ROW_ID AND ACT.COST_CODE_ID = COS.ROW_ID AND ACT.LOCATION_ID = LOC.ROW_ID AND ACT.MEMBER_ID = MEM.ROW_ID", sqlConnection); 
     sqlConnection.Open(); 

     SqlDataReader reader = sqlCommand.ExecuteReader(); 

     //collect rowID 
     string retrievedROWID = ""; 

     if (reader.HasRows) 
     { 
      reader.Read(); 
      string temp1 = reader["ROW_ID"].ToString(); 
      retrievedROWID = temp1; 
      GridView1.DataSource = reader; 
      GridView1.DataBind(); 
     } 

     sqlConnection.Close(); 
    } 
    else if (TempLocationIdbox.Text == "N") 
    { 
     string strSQLconnection1 = "Data Source=SQL5027.HostBuddy.com;Initial Catalog=DB_A05369_WATERQ;User Id=DB_A05369_WATERQ_admin;Password=waterqws1"; 
     SqlConnection sqlConnection1 = new SqlConnection(strSQLconnection1); 
     SqlCommand sqlCommand1 = new SqlCommand("SELECT * FROM S_LOCATION WHERE NAME = '" + AdminsCurrentLocation + "'", sqlConnection1); 
     sqlConnection1.Open(); 

     string locationrowID = ""; 
     SqlDataReader reader12 = sqlCommand1.ExecuteReader(); 

     if (reader12.HasRows) 
     { 
      reader12.Read(); 
      locationrowID = reader12["ROW_ID"].ToString(); 
     } 

     sqlConnection1.Close(); 

     string strSQLconnection = "Data Source=SQL5027.HostBuddy.com;Initial Catalog=DB_A05369_WATERQ;User Id=DB_A05369_WATERQ_admin;Password=waterqws1"; 
     SqlConnection sqlConnection = new SqlConnection(strSQLconnection); 
     SqlCommand sqlCommand = new SqlCommand("SELECT DISTINCT ACT.ROW_ID , ACT.CREATED , MEM.FIRST_NAME , MEM.LAST_NAME , LOC.NAME , CAT.NAME , SER.NAME , EMP.FIRST_NAME , EMP.LAST_NAME , SER.DURATION , ACT.CASH , COS.NAME , ACT.COMMENTS FROM "+ 
           "S_ACTIVITY ACT, S_LOCATION LOC, S_CATEGORY CAT, S_EMPLOYEE EMP, S_SERVICE SER, S_COST_CODE COS, S_MEMBER MEM "+ 
           "WHERE ACT.EMPLOYEE_ID = EMP.ROW_ID AND ACT.SERVICE_ID = SER.ROW_ID AND ACT.CATEGORY_ID = CAT.ROW_ID AND ACT.COST_CODE_ID = COS.ROW_ID AND "+ 
           "ACT.LOCATION_ID = '"+ locationrowID + "' AND ACT.MEMBER_ID = MEM.ROW_ID AND LOC.NAME = '"+ AdminsCurrentLocation + "'", sqlConnection); 
     sqlConnection.Open(); 

     SqlDataReader reader = sqlCommand.ExecuteReader(); 

     //collect rowID 
     string retrievedROWID = ""; 

     if (reader.HasRows) 
     { 
      reader.Read(); 
      string temp1 = reader["ROW_ID"].ToString(); 
      retrievedROWID = temp1; 
      GridView1.DataSource = reader; 
      GridView1.DataBind(); 
     } 

     sqlConnection.Close(); 
    } 

Welche Stümpfe ich bin, dass meine andere Abfrage funktioniert gut für den Fall, wo TempLocationIdbox.Text == "Y" ... Danke, jede Hilfe würde sehr geschätzt werden.

+0

Sie lesen nur eine Zeile von Ihren Lesern. Sie benötigen wahrscheinlich eine while-Schleife, um mehrere Zeilen zu bearbeiten. – mason

Antwort

0

Statt if (reader.HasRows) verwenden Sie benötigen while (reader.Read()) .Auch zu verwenden, eine Art Sammlung erstellt beispielsweise List<T> und füllen Sie es mit den Ergebnissen in der while loop.Then außerhalb der Schleife die DataSource der GridView Kontrolle Punkt auf List<T> .Hier ist ein komplettes Beispiel:

public class Activity 
{ 
    public int RowID { get; set; } 
    public DateTime Created { get; set; } 
    public string FirstName { get; set; } 
} 

public partial class UsingSqlDataReader : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if(!Page.IsPostBack) 
     { 
      this.GetData(); 
     } 
    } 

    private void GetData() 
    { 
     string cs = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString; 
     var activities = new List<Activity>(); 
     using (var con = new SqlConnection(cs)) 
     { 
      using(var cmd = new SqlCommand("SELECT ACT.ROW_ID,ACT.CREATED,ACT.FIRST_NAME FROM [dbo].[S_ACTIVITY] ACT", con)) 
      { 
       cmd.CommandType = System.Data.CommandType.Text; 
       con.Open(); 
       using (SqlDataReader reader = cmd.ExecuteReader()) 
       { 
        while (reader.Read()) 
        { 
         var activity = new Activity(); 
         activity.RowID = Convert.ToInt32(reader["ROW_ID"]); 
         activity.Created = DateTime.Parse(reader["CREATED"].ToString()); 
         activity.FirstName = Convert.ToString(reader["FIRST_NAME"]); 
         activities.Add(activity); 
        } 
       } 
      } 
     } 
     GridView1.DataSource = activities; 
     GridView1.DataBind(); 
    } 
} 

Ausgang:

Populating GridView using SqlDataReader

+0

Bekam es mit einer ähnlichen Mode, tyvm :) – Rolthar

0

Eine bessere Lösung wäre, Ihre In-Line-Abfrage in einem gespeicherten Prozess zu platzieren und den gespeicherten Prozess auszuführen.

Sie könnten es auch in ein Dataset bringen und binden Sie die Gridview damit. aber das ist völlig deine Entscheidung.

In-line-Abfragen sind sehr fehleranfällig, und die Ergebnisse könnten unvorhersehbar sein.

+0

* In-line-Abfragen sind sehr fehleranfällig und die Ergebnisse könnten unvorhersehbar sein ... * ist es wirklich? Was ist, wenn Sie das im gespeicherten Proc auch vermissen? Art der Abfrage OP ausgeführt wird, hat nichts mit gespeicherten Proc und besser einer Inline-Abfrage zu tun. Wie denkst du, dass das tatsächlich die Frage beantwortet? – Rahul