Reputation: 2105
I have a problem with my code C#.
In the table of database Users
I have users with two tariff plans (multitariff)
.
I need populate my Label plane
with the values of plane extracted from the database to single user.
I thought about to create a list of classes
where stored the values of plane.
I have tried using this solution without success, because the output is only the first value of plane and not all the values of plane for multitariff user
.
I would greatly appreciate any help you can give me in working this problem.
Here is my code:
string plane;
List<string> planeList = new List<string>();
public class Lists
{
static void Main()
{
List<string> planeList = new List<string>();
}
}
protected void Users()
{
using (OdbcConnection cn =
new OdbcConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
{
sql = " SELECT * FROM Users WHERE Id = ? ";
using (OdbcCommand command =
new OdbcCommand(sql, cn))
{
try
{
command.Parameters.AddWithValue("param1", Server.UrlDecode(Request.Cookies["Id"].Value));
command.Connection.Open();
using (OdbcDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
plane = reader["plane"].ToString();
planeList.Add(plane.ToString());
}
}
}
catch (Exception ex)
{
throw new ApplicationException("operation failed!", ex);
}
finally
{
command.Connection.Close();
}
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Users();
if (Request.Cookies["Id"] != null)
{
foreach (string plane in planeList)
{
plane.Text = "User plane: " + plane.ToString().Trim() + "<br />";
}
}
}
}
Upvotes: 1
Views: 135
Reputation: 28747
You are overwriting the value of your label everytime:
foreach (string plane in planeList)
{
plane.Text = "User plane: " + plane.ToString().Trim() + "<br />";
}
Instead of overwriting it you should concatenate the string:
plane.Text = "User plane: ";
foreach (string item in planeList)
{
plane.Text += item.ToString().Trim() + "<br />"; // I suppose plane is a label here, so your foreach variable should be called different (item in this case)
}
Upvotes: 2