1
votes

After i declare this, this is the error i get

protected void Page_Load(object sender, EventArgs e)
{
        List<String> LabelTextList = new List<String>();
         dr = cmd.ExecuteReader();
        while (dr.Read())
        {
        LabelTextList.add(dr[0].ToString());
        }
 }

Error 1 'MasterPage_Profile' does not contain a definition for 'LabelTextList' and no extension method 'LabelTextList' accepting a first argument of type 'MasterPage_Profile' could be found (are you missing a using directive or an assembly reference?)

[UPDATE] Now it says :

'System.Collections.Generic.List' does not contain a definition for 'add' and no extension method 'add' accepting a first argument of type 'System.Collections.Generic.List' could be found (are you missing a using directive or an assembly reference?)

2
For a start List<String> LabelTextList = new List<String>(); should be List<String> labelTextList = new List<String>(); And this.LabelTextList.add(dr[0].ToString()); should be labelTextList.add(dr[0].ToString()); - Paul Zahra
unless you've declared a field called LabelTextList then you're not going to get to it using the this keyword. You've scoped it at method level so this isn't required and isn't applicable - owen79
@PaulZahra I have changed it . The name 'labelTextList' does not exist in the current context. - user3021598
It looks like you have the casing of the variable name wrong. The case must match exactly. LabelTextList is different than labelTextList - JaredPar

2 Answers

5
votes

Remove this - LabelTextList is a local variable.

protected void Page_Load(object sender, EventArgs e)
{
        List<String> LabelTextList = new List<String>();
         dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            LabelTextList.add(dr[0].ToString());
        }
 }
2
votes

To fix this change it to the following

LabelTextList.Add(dr[0].ToString());

The LabelTextList value is a local variable definition. When you prefix an expression with this. it tells the compiler to look for members of the value, not locals.

Here is a counter example with a field named LabelTextList that works with this.

List<String> LabelTextList = new List<String>();
protected void Page_Load(object sender, EventArgs e)
{
   dr = cmd.ExecuteReader();
   while (dr.Read())
   {
      this.LabelTextList.Add(dr[0].ToString());
   }
}

Also if you keep the value as a local the standard naming pattern would be labelTextList instead of LabelTextList. This isn't required by the language but is the preferred style.