How to fill in data from dataset to crystal report?
I create a blank crystal report
then use the following code, there is nothing to see after running.
is it need to add field object into crytsl report corresponding to the field in data set. But i do not know how to add in this situation which is not connected through crystal report.
try
{
string _connectionString = ConfigurationManager.ConnectionStrings["CarParkConnectionString"].ConnectionString;
OleDbConnection connection = null;
try
{
using (connection = new OleDbConnection(_connectionString))
{
//OleDbCommand command = connection.CreateCommand();
string selectsql = "SELECT a.Transaction_Date, a.Card_no, a.Company, a.Credit_Fee, a.Non_Credit_Fee FROM [SELECT Transaction_Date, Card_no, Company, Fee as Credit_Fee, 0 as Non_credit_fee FROM CarPark where IsCredit = true union all SELECT Transaction_Date, Card_no, Company, 0 as Credit_Fee, Fee as Non_credit_fee FROM CarPark where IsCredit = false]. AS a where a.Transaction_Date >= " + Calendar1.SelectedDate.ToShortDateString() + " and a.Transaction_Date <= " + Calendar2.SelectedDate.ToShortDateString();
//command.CommandText = selectsql;
//SetCommandParametersForInsertUpdateTo(carpark, command, error);
connection.Open();
OleDbDataAdapter dataAdapter1 = new OleDbDataAdapter(selectsql, connection);
DataSet ds = new DataSet();
dataAdapter1.Fill(ds, "CarPark");
dataAdapter1.Dispose();
CrystalReport1 objRpt = new CrystalReport1();
objRpt.SetDataSource(ds.Tables[0]);
DailyReport_Crys开发者_开发百科talReportViewer.EnableParameterPrompt = false;
DailyReport_CrystalReportViewer.ReportSource = objRpt;
DailyReport_CrystalReportViewer.RefreshReport();
}
}
catch (Exception ex)
{
connection.Close();
Error_Label.Text = Error_Label.Text + " " + ex.Message;
}
finally
{
connection.Close();
}
Here are the steps you need to create a crystal Report using dataset:
First this method is called PUSH method.
1- Create a Dataset through visual studio Go to Right click on your project --> Add new Item --> Datset Call it Dataset1
2- Create a Table in your Dataset call it (Table1)
3- Add Columns to your table specifying the type of every column lets say you have 2 columns ( ID type of int ) , (Name type of string)
4- then In your Report You want to Choose a datasource for it , so In your Field Explorer on the left you'll find the Database fields, right click on it and select Database expert
5- When you do that open project data and then ADO.NET Datasets Select your Dataset (Dataset1) and then your Table (Table1)
6- You will find the Database fields in the Field Explorer is now populated with the table and the 2 columns id and name.
7- Drage and drop these two fields inside the details section of the report..
Now the Report is ready to be viewed but not yet as you need to populate the fields of the ID and Name through a Dataset
8- Sample code to Populate the dataset
Dataset ds=new DataSet();
DataTable dt=new DataTable("Table1"); // Be sure to call this table as your Table's name
// in the Dataset
dt.Columns.Add("ID", typeof(System.Integer)); //Same name of your ID column
dt.Columns.Add("Name", typeof(System.String));
Datarow dr=dt.NewRow();
dr["ID"]=1;
dr["Name"]="Test";
dt.Rows.Add(dr);
ds.Tables.Add(dt);
ReportDocument rpt = new ReportDocument();
rpt.Load(Server.MapPath("Your Report's Name"));
rpt.SetDataSource(ds);
DailyReport_CrystalReportViewer.ReportSource = rpt;
DailyReport_CrystalReportViewer.DataBind();
Here it is all the steps you need,
Hope that helps.
This might be helpful a stepwise process of generating a crystal report from asp.Net Dataset:
http://www.c-sharpcorner.com/UploadFile/rsubhajit/CrystalReportwithDataSet03012006060655AM/CrystalReportwithDataSet.aspx
精彩评论