2013-12-12 3 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(); 
    
  • 당신은 ListStudent의의에 "stud"를 추가하려고 (나는 믿고있어 stud 것을 Student에서 상속하지 않습니다
+0

어떻게 정의하면 x? – user3045190

+0

@ user3045190, 어디서 X라는 이름을 얻었습니까? 그 변수는 무엇을 유지합니까? 정의 할 위치를 파악할 수있는 유일한 방법은 그것이 무엇을 할 것인지를 결정하는 것입니다. – gunr2171