Reputation: 43
I was generating the Crystal Report viewer using WPF and I was able to load data in the report viewer. But when I was trying to create an event to refresh the report dynamically, the data could not be refreshed.
Basically what I have done is creating a dataSet connecting with the MSSQL Server
Then generate a Crystal Report file:
And then trying to import data into the Crystal report viewer in the MainWindow. Here is the code:
public partial class MainWindow: Window
{
DataSet1TableAdapters.BordierSheetTableAdapter TA = new DataSet1TableAdapters.BordierSheetTableAdapter();
ReportDocument reportDoc = new ReportDocument();
public MainWindow()
{
InitializeComponent();
Data_Binding();
}
public void button2_Click(object sender, RoutedEventArgs e)
{
reportDoc.Refresh();
crystalReportsViewer1.ViewerCore.RefreshReport();
}
void Data_Binding()
{
DataSet empDataSet = new DataSet();
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = "Data Source=YATONGHOU\\SQLEXPRESS;Initial Catalog=messagedb;Integrated Security=True";
conn.Open();
string sqlStr = "select * from BordierSheet";
SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlStr,conn);
dataAdapter.Fill(empDataSet, "BordierSheet");
}
string reportDataSource = @"C:\\Users\\yatong\\Work\\LogAnalytics\\BordierSheetReport\\BordierSheetReport\\CrystalReport1.rpt";
reportDoc.Load(reportDataSource);
reportDoc.SetDataSource(empDataSet);
this.crystalReportsViewer1.ViewerCore.ReportSource = reportDoc;
}
}
When I updated my data base and then click the button in the window (which triggers the button2_Click(object sender, RoutedEventArgs e) function), no data has been updated. So could anyone help me with this please? Thank you very much.
Upvotes: 1
Views: 15754
Reputation: 3400
If your crystal report data is not refreshed after passing the new dataset to the crystal report setdatasource (crRpt.SetDataSource(ds)
) method, you are doing a silly mistake.
try instead of above setdatasource (crRpt.SetDataSource(ds.Tables[0])
).
Sample Code
crReceipt crRpt = new crReceipt();
string qry = "";
DataSet ds = new DataSet();
qry = "SELECT L.LID, L.LName, " +
" R.RDate, R.RAmount, R.RRemark " +
"FROM (Ledger AS L " +
"LEFT OUTER JOIN Receipt AS R ON L.LID = R.LID) " +
"WHERE L.LName = 'Pankaj' ";
ds = cn.getDataSet(qry);
crRpt.SetDataSource(ds.Tables[0]);
crv.ReportSource = crRpt; //crv = crystal report viewer
Upvotes: 0
Reputation: 35
Try this..this is working!
Go to Crystal Report Design > File > Uncheck the Save Data with Report.
That's it then save again.. it will work.
Upvotes: 2
Reputation: 2362
Remove the datasource and add it again , such as:
if ( reportViewer1.LocalReport.DataSources.Count > 0)
reportViewer1.LocalReport.DataSources.RemoveAt(0);
reportViewer1.LocalReport.DataSources.Add(new ReportDataSource()
{
Name = "ds",
Value = MyDataSource
});
Upvotes: 0
Reputation: 462
try this:
reportDoc.Load(reportDataSource);
reportDoc.SetDataSource(empDataSet);
this.crystalReportsViewer1.ViewerCore.ReportSource = reportDoc;
crystalReportsViewer1.RefreshReport();
Upvotes: 3
Reputation: 10604
Have you tried doing :
reportDoc.Refresh()
after doing the data load?
Upvotes: 2