【问题标题】:Retrieve all data from database to html table检索数据库中的所有数据到 html 表
【发布时间】:2014-05-23 23:05:22
【问题描述】:

请您帮助我如何将数据从数据库检索到 html 表?我已经为它编写了一个脚本,但我对函数 foreach 有疑问。

类:

public class games
{
    public string typeTicket { get; set; }
    public string typeMethod { get; set; }
    public string message { get; set; }
    public string typeName { get; set; }
    public string value { get; set; }
    public string status { get; set; }

    public games(string typeTicket, string typeMethod, string message, string typeName, string value, string status)
    {
        this.typeTicket = typeTicket;
        this.typeMethod = typeMethod;
        this.message = message;
        this.typeName = typeName;
        this.value = value;
        this.status = status;
    }
}

从数据库中读取数据:

public ArrayList selectOrderGame()
{
    ArrayList hry = new ArrayList();
    commandS = new SqlCommand("SELECT typeTicket, typeMethod, message, typeName, value, status FROM dbo.tickets WHERE typeTicket LIKE 'Objednávka' AND typeMethod LIKE 'Hry'", conn);

    try
    {
        conn.Open();
        SqlDataReader reader = commandS.ExecuteReader();

        while (reader.Read())
        {
            string typeTicket = reader.GetString(0);
            string typeMethod = reader.GetString(1);
            string message = reader.GetString(2);
            string typeName = reader.GetString(3);
            string value = reader.GetString(4);
            string status = reader.GetString(5);

            games game = new games(typeTicket, typeMethod, message, typeName, value, status);
            hry.Add(game);
        }

        return hry;
    }
    finally
    {
        conn.Close();
    }
}

这里是转换为 html 表格的脚本:

protected void FillPage()
{
    transaction tran = new transaction();
    ArrayList gameList = new ArrayList();
    if (IsPostBack)
        labelOutput.Text = "Při načítaní došlo k chybě";
    else
        gameList.Add(tran.selectOrderGame());

    StringBuilder sb = new StringBuilder();

    foreach (games game in gameList)
    {
        sb.Append(string.Format(@"<table>
        <tr>
            <th rowspan='6' width='150px'></th>
            <th width='50px'>Name: </th>
        </tr>
        <tr>
            <th>Type: </th>
            <th>{0}</th>
        </tr>
        <tr>
            <th>Type: </th>
            <th>{1}</th>
        </tr>
        <tr>
            <th>Type: </th>
            <th>{2}</th>
        </tr>
        <tr>
            <th>Type: </th>
            <th>{3}</th>
        </tr>
        <tr>
            <th>Type: </th>
            <th>{4}</th>
        </tr>
        <tr>
            <th>Type: </th>
            <th>{5}</th>
        </tr>
        <tr><td colspan='2'>{6}</td></tr>
        </table>", game.typeTicket, game.typeMethod, game.message, game.typeName, game.value, game.status));

        labelOutput.Text = sb.ToString();
    }
}

错误:

无法将“System.Collections.ArrayList”类型的对象转换为“rad3k_eu.order.classes.games”类型。
说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.InvalidCastException:无法将“System.Collections.ArrayList”类型的对象转换为“rad3k_eu.order.classes.games”类型。

来源错误:

在执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常来源和位置的信息。

堆栈跟踪:

[InvalidCastException:无法将“System.Collections.ArrayList”类型的对象转换为“rad3k_eu.order.classes.games”类型。] rad3k_eu.order.hry.FillPage() in
g:\Programování\Projects\C#\StreamingSite\StreamingSite\order\hry.aspx.cs:32 rad3k_eu.order.hry.Page_Load(Object sender, EventArgs e) in g:\Programování\Projects\C#\StreamingSite\StreamingSite\order\hry.aspx.cs:18 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(对象发送者,EventArgs e)+51 System.Web.UI.Control.OnLoad(EventArgs e) +92 System.Web.UI.Control.LoadRecursive() +54 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean inc ludeStagesAfterAsyncPoint) +772

当我将此部分代码 gameList.Add(tran.selectOrderGame()); 更改为 gameList = tran.selectOrderGame(); 时,错误已修复,但转换为表格仍然不起作用,因为 labelOutput 仍然具有默认名称,无需我进行更改。

【问题讨论】:

  • ArrayList 中的一项是一个对象。您将列表的元素读取为对象本身。所以你的foreach 代码应该是foreach(object o in gameList)。您必须将其投射到 foreach 内的目标 game 对象。

标签: asp.net html arraylist foreach html-table


【解决方案1】:

你的 selectOrderGame() 方法返回一个 ArrayList,所以我认为你应该改变

gameList.Add(tran.selectOrderGame());

进入

gameList = tran.selectOrderGame();

而且你需要将这一行移出 foreach 循环:

labelOutput.Text = sb.ToString();

否则标签文本将始终具有上次迭代的值

【讨论】:

  • 我把我的评论改了。是的,我有它并且错误已修复。现在我遇到了 gameList 计数为 0 的问题。也许是 DB 的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
  • 2020-07-09
  • 2017-01-18
相关资源
最近更新 更多