I’m starting the development of a C#/ASP.net web app, within which I use the Entity Framework (Microsoft ORM).
My problem is very simple :
In my default.aspx.cs I have this :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Projet___TestConnaissances
{
public partial class _Default : System.Web.UI.Page
{
protected List<Theme> lt = new List<Theme>();
protected List<int> li = new List<int>();
protected Theme th = new Theme();
protected String test = "teeeest";
protected int v = 1;
protected void Page_Load(object sender, EventArgs e)
{
DataSourceContainer bdd = new DataSourceContainer();
var requete = from Theme in bdd.ThemeSet select Theme;
List<Theme> lt = requete.ToList(); // gets a list of themes
v = lt.Count(); // puts in v the number of themes in lt
th = lt.First(); // variable containing a unique theme (first of lt)
test = "Ceci est un test";
li.Add(1);
li.Add(2);
li.Add(3);
}
}
}
And in my default.aspx, I display this :
<p>
<br />test : <%= test %>
<br />v : <%= v %>
<br />th.libelle : <%= th.libelle %>
<br />lt.count : <%= lt.Count() %>
<br />li.count : <%= li.Count() %>
</p>
As a result, I have :
test : Ceci est un test
v : 3
th.libelle : Test ajout libelle
lt.count : 0
li.count : 3
As you can see, my List of Theme is inexplicably reinitialized before its displayed.
What is weird is that my List of int is well kept, as well as the variable containing a unique theme.
The Theme class as been created with the Entity Designer, maybe this is the difference with the List of int ?
Thanks in advance to those who will help me figure out what’s happening there.
Bye !
Your
Page_Loadmethod is declaring a new variable calledlt. It’s not assigning anything to the instance variable, because the local variable is shadowing the instance variable. So this:should probably be this:
I’d also suggest using more meaningful variable names 🙂