显示标签中的通用列表项

本文关键字:列表 标签 显示 | 更新日期: 2024-09-24 19:14:50

我只想在设计中的标签中显示通用列表中的学生详细信息。

我在 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;
            }
        }
    }
}

显示标签中的通用列表项

错误消息似乎很清楚 - 您正在尝试迭代未在方法可以访问的任何地方定义x

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

您还有这些问题:

  • 您正在尝试引用 Student.Show() 中表单中的所有文本框。
  • 您正在尝试创建一个未在我可以看到的任何地方定义的stud实例:

    std = new stud();
    
  • 您正在尝试将" stud "添加到 Student s 的List(我假设stud不会从Student继承。