2013-12-12 25 views
0

我只想在設計中的標籤中的通用列表中顯示學生的詳細信息。顯示標籤中的通用列表項目

我在foreach循環中收到錯誤x is not in the current context

namespace gentask 
{ 
    public partial class WebForm2 : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      SqlConnection conn = new SqlConnection(); 
      conn.ConnectionString = @"Data Source=Sadiq;Initial Catalog=rafi;Integrated Security=True"; 
      conn.Open(); 
      SqlCommand cmd = new SqlCommand(); 
      cmd.Connection = conn; 
      cmd.CommandText = "select * from student"; 
      SqlDataAdapter da = new SqlDataAdapter(cmd); 
      DataSet ds = new DataSet(); 
      da.Fill(ds); 
     } 
    } 
    public class student 
    { 
     student std; 
     List<student> stud = new List<student>(); 
     public void load() 
     { 
      foreach (DataRow dr in x) 
      { 
       std = new stud(); 
       std.id = x[0].Tostring(); 
       std.name = x[1].Tostring(); 
       std.age = x[2].Tostring(); 
       std.school = x[3].Tostring(); 
       std.clas = x[4].Tostring(); 
       std.marks = x[5].Tostring(); 
       std.grade = x[6].Tostring(); 
       stud.Add(std); 
      } 
     } 
     public void show() 
     { 
      foreach (student std in stud) 
      { 
       std.id = label.text; 
       std.name = label1.text; 
       std.age = label2.text; 
       std.school = label3.text; 
       std.clas = label4.text; 
       std.marks = label5.text; 
       std.grade = Label6.Text; 
      } 
     } 
    } 
} 
+4

'的foreach(在X的DataRow博士)' - 這裏是'x'定義 – Tim

+0

你真的應該開始更慢你知道如何在實驗室中顯示_anything_?埃爾?像數字「42」?先嚐試一下。 –

回答

3

錯誤信息似乎很清楚 - 你想在x迭代未在任何地方定義的方法,可以訪問:

foreach (DataRow dr in x) // what is x? 

你也存在這些問題:

  • 您正在嘗試引用Student.Show()中的所有文本框。
  • 正在嘗試創建一個沒有定義任何地方,我可以看到的stud一個實例:

    std = new stud(); 
    
  • 您正試圖將「stud」添加到StudentList一個S(我假設這stud不從Student繼承
+0

那麼如何以及在哪裏定義x? – user3045190

+0

@ user3045190,你從哪裏得到名字X?這個變量持有什麼?你可以找出在哪裏定義它的唯一方法是弄清楚它會做什麼。 – gunr2171