user3045190
user3045190

Reputation: 11

Display the generic list items in the labels

I just want to display the student details from the generic list in the labels which is in the design.

I'm getting the error in the foreach loop that 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;
            }
        }
    }
}

Upvotes: 0

Views: 742

Answers (1)

D Stanley
D Stanley

Reputation: 152521

The error message seems pretty clear - you are trying to iterate over x which is not defined anywhere that the method can access:

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

You also have these problems :

  • You are trying to reference all of the textboxes from the form in Student.Show().
  • You are trying to create an instance of stud that is not defined anywhere that I can see:

    std = new stud();
    
  • You are trying to add the "stud" to a List of Students (I'm assuming that stud does not inherit from Student.

Upvotes: 3

Related Questions