.net Repeater删除重复数据

发布于 2024-12-29 19:20:49 字数 2677 浏览 0 评论 0原文

我有一个中继器,我试图根据 sql 语句显示数据。但是,由于某种原因,如果我在转发器中有多行相同的条目,它只显示一个。例如:

数据库(informix)

  • 工资扣除250.00
  • 工资扣除250.00
  • 工资扣除250.00
  • 工资扣除250.00
  • 学生健康保险1100.00

在复读机中显示:

  • 工资扣除250.00
  • 学生健康保险 1100.00

知道为什么会这样吗?当我在数据库中运行它时,我在代码(如下)中使用的 SQL 运行良好。

private DataTable GetCreditData ( string id, string sessyr )
    {
        OdbcConnection conn = new OdbcConnection ( );
        conn.ConnectionString = cxConnStr;

        // Define our SQL
        String sql = "select  ABS(t1.amt) as amt , t2.txt from subtr_rec t1, subt_table t2 where t1.subs = 'S/A' and t1.tot_prd = ? and t1.subs_no=? and t1.amt < 0  and t1.tot_code = t2.tot_code and t1.subs = t2.subs UNION select t1.amt, t2.txt from aid_rec t1 join  aid_table t2 on t1.aid=t2.aid where t1.sess =? and t1.yr = ? and t1.id = ? and  ((stat='A' and amt_stat='AA') or (stat='I' or amt_stat='AA'))";

        // Command
        OdbcCommand command = new OdbcCommand ( );
        command.Connection = conn;
        command.CommandText = sql;

        command.Parameters.Add ( new OdbcParameter ( "sess", sessyr ) );
        command.Parameters.Add ( new OdbcParameter ( "id", id ) );
        command.Parameters.Add ( new OdbcParameter ( "sess2", CurrSess ) );
        command.Parameters.Add ( new OdbcParameter ( "yr", CurrentYr ) );
        command.Parameters.Add ( new OdbcParameter ( "id2", id ) );

        // Create a DataTable to store our Cached results.
        DataTable dt = new DataTable ( );

        // Create a DataAdapter used to fill the DataTable
        OdbcDataAdapter dataAdapter = new OdbcDataAdapter ( );

        // Associate the DataAdapter and select command created above
        dataAdapter.SelectCommand = command;

        try
        {
            // Open Database.
            conn.Open ( );

            // Fill DataTable.
            dataAdapter.Fill ( dt );
        }
        catch ( Exception ex )
        {
            edException.Error = ex;
            this.ParentPortlet.ShowFeedback ( FeedbackType.Message, "There was an error looking up term credits." );
        }
        finally
        {
            if ( conn != null && conn.State == ConnectionState.Open )
            {
                // Release our resources (close db connection)
                conn.Close ( );
            }
        }
        return dt;

    }

这里是数据表绑定到中继器的地方(page_load):

DataTable creditdata = GetCreditData ( Host, CurrSess + CurrentYr.Remove ( 0, 2 ) );
TermCredits.DataSource = creditdata;
TermCredits.DataBind ( );

I have a repeater that I am trying to show data based on an sql statement. However for some reason if I have multiple rows of the same entry in the repeater, it only shows one. For example:

In the database(informix):

  • Payroll Deduction 250.00
  • Payroll Deduction 250.00
  • Payroll Deduction 250.00
  • Payroll Deduction 250.00
  • Student Health Insurance 1100.00

Displayed in the repeater:

  • Payroll Deduction 250.00
  • Student Health Insurance 1100.00

Any idea why this might be? The SQL I use in the code (below) works fine when I run it in the database.

private DataTable GetCreditData ( string id, string sessyr )
    {
        OdbcConnection conn = new OdbcConnection ( );
        conn.ConnectionString = cxConnStr;

        // Define our SQL
        String sql = "select  ABS(t1.amt) as amt , t2.txt from subtr_rec t1, subt_table t2 where t1.subs = 'S/A' and t1.tot_prd = ? and t1.subs_no=? and t1.amt < 0  and t1.tot_code = t2.tot_code and t1.subs = t2.subs UNION select t1.amt, t2.txt from aid_rec t1 join  aid_table t2 on t1.aid=t2.aid where t1.sess =? and t1.yr = ? and t1.id = ? and  ((stat='A' and amt_stat='AA') or (stat='I' or amt_stat='AA'))";

        // Command
        OdbcCommand command = new OdbcCommand ( );
        command.Connection = conn;
        command.CommandText = sql;

        command.Parameters.Add ( new OdbcParameter ( "sess", sessyr ) );
        command.Parameters.Add ( new OdbcParameter ( "id", id ) );
        command.Parameters.Add ( new OdbcParameter ( "sess2", CurrSess ) );
        command.Parameters.Add ( new OdbcParameter ( "yr", CurrentYr ) );
        command.Parameters.Add ( new OdbcParameter ( "id2", id ) );

        // Create a DataTable to store our Cached results.
        DataTable dt = new DataTable ( );

        // Create a DataAdapter used to fill the DataTable
        OdbcDataAdapter dataAdapter = new OdbcDataAdapter ( );

        // Associate the DataAdapter and select command created above
        dataAdapter.SelectCommand = command;

        try
        {
            // Open Database.
            conn.Open ( );

            // Fill DataTable.
            dataAdapter.Fill ( dt );
        }
        catch ( Exception ex )
        {
            edException.Error = ex;
            this.ParentPortlet.ShowFeedback ( FeedbackType.Message, "There was an error looking up term credits." );
        }
        finally
        {
            if ( conn != null && conn.State == ConnectionState.Open )
            {
                // Release our resources (close db connection)
                conn.Close ( );
            }
        }
        return dt;

    }

Here is where the data table is bound to the repeater (page_load):

DataTable creditdata = GetCreditData ( Host, CurrSess + CurrentYr.Remove ( 0, 2 ) );
TermCredits.DataSource = creditdata;
TermCredits.DataBind ( );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

合约呢 2025-01-05 19:20:49

@NLarkin - 希望你不介意我在事后将其放入答案中。我正在尝试建立一些投资组合,并希望增强我的在线形象。

因此,回顾一下我的评论 - 检查 SQL 生成的结果。由于这里有多个“一个移动部件”,这将使您能够隔离问题。如果 SQL 产生您期望的输出,那么您就知道它是转发器,显然,如果没有,您就知道它是 SQL。 :)

一般来说 - (希望我不是在教你吸鸡蛋)当你进行这些层分离时,从下到上检查每个“层”是否产生了你期望的结果总是一个好主意。

希望这有用,编码愉快!!
干杯,
克里斯.

@NLarkin - hope you don't mind me putting this in an answer after the fact so to speak. I'm trying to build up a bit of a portfolio and would like to bolster my online presence so to speak.

So, recapping my comment - check the results the SQL produces. As there is more than 'one moving part' here, this will allow you to isolate the problem. If the SQL produces the output you expected, then you know it's the repeater and obviously, if it doesn't, you know it's the SQL. :)

In general - (and hoping I'm not teaching you to suck eggs) when you have these layer separations, it's always a good idea to check from the bottom up that each 'layer' is producing the results you expect.

Hope this was useful, and happy coding!!
Cheers,
Chris.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文