[英]List of classes in C#
我的代碼C#有問題。
在數據庫“ Users
”表中,我的用戶具有兩個定價計划(multitariff)
。
我需要使用從數據庫提取到單個用戶的平面值填充我的Label plane
。
我考慮過要創建一個list of classes
其中存儲了plane的值。
我嘗試使用此解決方案沒有成功,因為對於multitariff user
,輸出僅是plane的第一個值,而不是平面的所有值。
我將非常感謝您在解決此問題方面可以給我的任何幫助。
這是我的代碼:
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 />";
}
}
}
}
您每次都會覆蓋標簽的值:
foreach (string plane in planeList)
{
plane.Text = "User plane: " + plane.ToString().Trim() + "<br />";
}
而不是覆蓋它,您應該連接字符串:
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)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.