Reportviewer

时间:2018-01-18 17:21:28

标签: c#

我正在努力制作类似银行对账单申请的东西,但我有一个问题。

现在它获取数据源和所有数据源,但不会从数据库中获取值并显示在报表查看器上。显然我错过了一些东西,因此我决定把它带到这里

代码看起来像这样:

namespace TmpZ
{
    public partial class BalanceSheet : Form
    {
        string constring = ConfigurationManager.ConnectionStrings["ConnData"].ConnectionString;
        public BalanceSheet()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (accountNo1.Text == "")
            {
                MessageBox.Show("Please Enter Account Number");
            }
            else
            {
                DataTable dtb = new DataTable();
                dtb = GenerateBankStatement(dtb);
                reportViewer1.LocalReport.DataSources.Clear();
                ReportDataSource rpd = new ReportDataSource("DataSet1", dtb);
                reportViewer1.LocalReport.DataSources.Add(rpd);
                reportViewer1.RefreshReport();
            }
        }

        private DataTable GenerateBankStatement(DataTable dt)
        {
            using (SqlConnection cn = new SqlConnection(constring))
            {
                try
                {
                    SqlDataAdapter da = new SqlDataAdapter("SELECT [id] as id, [transaction_desc] as transaction_desc,[credit] as credit, [debit] as debit, [balance] as balance, [transaction_date] as transaction_date FROM transactions WHERE(accountNo1 = '" + accountNo1.Text + "') AND(transaction_date BETWEEN '" + dateFrom.Text + "' AND '" + dateTo.Text + "')", cn);
                    da.Fill(dt);
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
            return dt;
        }
    }
}

我错过了什么?它在reportviewer中的Box上显示空数据。

1 个答案:

答案 0 :(得分:0)

好的,我终于修好了。这是日期格式的问题。我必须在这一点上使它们成为字符串和代码。对于那些希望在以后工作的人来说,代码现在看起来像这样

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;

namespace TwpZ
{
    public partial class BalanceSheet : Form
    {
        string constring = ConfigurationManager.ConnectionStrings["ConnData"].ConnectionString;
        public BalanceSheet()
        {
            InitializeComponent();
        }

        private void BalanceSheet_Load(object sender, EventArgs e)
        {

        }

        private void reportViewer1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (accountNo1.Text == "")
            {
                MessageBox.Show("Please Enter Account Number");
            }
            else
            {
                DataTable dtb = new DataTable();
                dtb = GenerateBankStatement(dtb);
                reportViewer1.LocalReport.DataSources.Clear();
                ReportDataSource rpd = new ReportDataSource("DataSet1", dtb);
                reportViewer1.LocalReport.DataSources.Add(rpd);
                reportViewer1.RefreshReport();
            }
        }

        private DataTable GenerateBankStatement(DataTable dt)
        {
            using (SqlConnection cn = new SqlConnection(constring))
            {
                try
                {
                    string dateF = Convert.ToDateTime(dateFrom.Text).ToString("dd-MM-yyyy");
                    string dateT = Convert.ToDateTime(dateTo.Text).ToString("dd-MM-yyyy");
                    SqlDataAdapter da = new SqlDataAdapter("SELECT [id] as id, [transaction_desc] as transaction_desc,[credit] as credit, [debit] as debit, [balance] as balance, [transaction_date] as transaction_date FROM transactions WHERE(accountNo1 = '" + accountNo1.Text + "') AND(transaction_date BETWEEN '" + dateF + "' AND '" + dateT + "')", cn);
                    //SqlDataAdapter da = new SqlDataAdapter("SELECT [id] as id, [transaction_desc] as transaction_desc,[credit] as credit, [debit] as debit, [balance] as balance, [transaction_date] as transaction_date FROM transactions WHERE(accountNo1 = '" + accountNo1.Text + "')", cn);
                    da.Fill(dt);
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
            return dt;
        }
    }
}