从数据库检索所有数据到html表

时间:2014-04-10 09:58:55

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

请问您如何从数据库中检索数据到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;
    }
}

从DB中读取数据:

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í\项目\ C#\ StreamingSite \ StreamingSite \顺序\ hry.aspx.cs:32       rad3k_eu.order.hry.Page_Load(Object sender,EventArgs e)在g:\Programování\ Projects \ C#\ StreamingSite \ StreamingSite \ order \ hry.aspx.cs:18       System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender,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仍有默认名称而没有我的更改。

1 个答案:

答案 0 :(得分:0)

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

gameList.Add(tran.selectOrderGame());

gameList = tran.selectOrderGame();

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

labelOutput.Text = sb.ToString();

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