Author |
Message |
Capt_Ron
Newbie
Joined: 01 Dec 2006
Online Status: Offline
Posts: 7
|
Topic: creating a basic report in VS 2005 Posted: 01 Dec 2006 at 12:35pm |
I'm trying to generate a basic report (no sub reports or anything special) in VS 2005.
I need to pass a parameter when selecting the data for the report. (the Where clause) That parameter is located in the query string of the web page.
What do I use as a datasource for the report? I tried creating a SQL datasource but that was not a source option when I created the report.
I'm trying to do this with the built in objects in VS 2005. When I used to work in VS 2003 I did everything in code. I created an XML datasource dynamically, attached it to the report and displayed it. I'm trying not to do that this time.
Thanks
Ron
|
IP Logged |
|
squeaky
Newbie
Joined: 29 Nov 2006
Location: United States
Online Status: Offline
Posts: 6
|
Posted: 05 Dec 2006 at 6:51pm |
Hello:
here's the way I handle filling all of my reports. It should handle a parameter from the query string quite nicely.
"(the Where clause) That parameter is located in the query string of the web page."
First, I create a class to handle all database activity.
using System.Data;
using System.Data.OleDb;
using System.Runtime.Serialization.Formatters;
public class Reports
{
private OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.jet.oledb.4.0; Data Source = DataBase.mdb");
private OleDbDataAdapter myDataAdapter;
private DataSet myDataSet;
public OleDbConnection GetConnection
{
get{return myConnection;}
set{myConnection = value;}
}
public OleDbDataAdapter GetDataAdapter
{
get{return myDataAdapter;}
set{myDataAdapter = value;}
}
public DataSet GetDataSet
{
get{return myDataSet;}
set{myDataSet = value;}
}
public string GetTable
{
get{return table;}
set{table = value;}
}
public OleDbConnection SendConnection()
{
return myConnection;
}
public DataSet SendDataSet()
{
return myDataSet;
}
public Reports(string aTable)
{
}
public void FillRpt(string QueryString)
{
string sql = "SELECT * FROM TableName WHERE SomeFieldName = '" + QueryString + "'";
myDataAdapter = new OleDbDataAdapter(sql, myConnection);
myDataSet = new DataSet();
try
{
myDataAdapter.Fill(myDataSet, table);
}
catch (System.Exception exc)
{
MessageBox.Show(exc.Message.ToString());
}
Second, I simply instantiate the class giving me access to it's user-defined methods. The data access class that I created, accepts a string representing a table. If the sql statement may vary, I use a switch statement to determine what the sql statement should be. This can be determined by the table argument that's passed.
private void btnMessageList_Click(object sender, System.EventArgs e)
{
string msgList = "Messages";
Reports messageReport = new Reports(msgList);
MyCrystalReport myReport = new MyCrystalReport();
messageReport.SendConnection().Open();
messageReport.FillRpt();
myReport.SetDataSource(messageReport.SendDataSet());
myReportViewer.ReportSource = myReport;
messageReport.SendConnection().Close();
}
Simply stated, replace the string being passed above, msgList, with your own query string. The data access class will handle the rest. Just dress up the sql so that it fits your needs.
I'm not sure if this is what you were looking for. Maybe it will help. This code is written for an Access DB, but is easily configured to work with an SQL Sever DB.
Good luck.
Edited by squeaky - 05 Dec 2006 at 6:53pm
|
IP Logged |
|
Capt_Ron
Newbie
Joined: 01 Dec 2006
Online Status: Offline
Posts: 7
|
Posted: 06 Dec 2006 at 6:07am |
Thank you.
I'm having a problem with declaring the report object.
I created a blank report (for coding purposes) called CR1.RPT
I'm not able to declare it in code.
Referencing your line: MyCrystalReport myReport = new MyCrystalReport()
My line: CR1 myReport = new CR1()
Do I need to add any references? using CrystalDecisions.....?
I already have a DLL that I created to do all my SQL stuff so that is not a problem.
Also the report that you create, It's just a blank report that you add unbound elements to that get bound when you attach the datasource? right?
can you send a ZIP file of a sample project to me so I can look at the whole thing?
email: monbleau(AT)palmbeach(DOT)K12(DOT)fl(DOT)us
Thank you for your help!!!
Ron
|
IP Logged |
|
Capt_Ron
Newbie
Joined: 01 Dec 2006
Online Status: Offline
Posts: 7
|
Posted: 06 Dec 2006 at 6:15am |
I just noticed something interesting.
I opened a blank report.
In the Field Explorer section the Database Fields were blank. (expected)
Just out of curiosity, I Right Clicked and chose Database Expert. In the Data Section I expanded Project Data then .NET Objects and noticed that a Class that I created in my project to hold data was listed.
I chose the Class and it now shows all my properties of the class as fields I can use in the report.
Can I just bind the Class to the report directly as the datasource? That would be real cool!!!
I still have the problem that I cant reference the report in code though.
I'll try binding to the empty class and see what happens.
Ron
|
IP Logged |
|
squeaky
Newbie
Joined: 29 Nov 2006
Location: United States
Online Status: Offline
Posts: 6
|
Posted: 07 Dec 2006 at 2:41am |
Hello:
Have you tried dragging an instance of a dataAdapter onto your form? When I'm designing a report, that's the first thing I do. The DataAdapter Wizard should start up. From there, you should be able to choose the datasource you want to use. Also, it has a Query builder, so that you can build and test your sql string all from within the DataAdapter itself.
Once you're satisfied that you're pulling the expected results, right-click on the DataAdapter (in the components tray) and there should exist an option to generate a DataSet. It's the DataSet itself, that you'll want to access from the Field Explorer. You should only need to, as you discovered, right-click on the DataBase fields icon, and choose Add/Remove DataBase. Select the DataSet you generated, and you'll notice that the DataBase fields icon, now has an expand button (+ plus sign). Once you expand, you should find your DataSet within the Project Data folder.
Once my report is just the way I want it, I can delete the DataAdapter, Connection, and DataSet from the components tray. I usually leave them there until I'm finished with the entire application...just in case I decide I want to change something later.
Edited by squeaky - 07 Dec 2006 at 2:43am
|
IP Logged |
|
squeaky
Newbie
Joined: 29 Nov 2006
Location: United States
Online Status: Offline
Posts: 6
|
Posted: 07 Dec 2006 at 3:13am |
Hello:
as requested a sample is on it's way. I hope you find it useful.
|
IP Logged |
|
squeaky
Newbie
Joined: 29 Nov 2006
Location: United States
Online Status: Offline
Posts: 6
|
Posted: 07 Dec 2006 at 3:22am |
Originally posted by Capt_Ron
I still have the problem that I cant reference the report in code though.
Seems to me that you were on the right track. The key to referencing your report in code is to create an instance of your report. When you create a report, VS creates a class...surprisingly enough, this class has the same name as the report you created. Like any other class, you simply use the new keyword:
MyCrystalReport newReport = new MyCrystalReport();
Reference the report instance with its name: "newReport"
|
IP Logged |
|
Capt_Ron
Newbie
Joined: 01 Dec 2006
Online Status: Offline
Posts: 7
|
Posted: 07 Dec 2006 at 5:07am |
Squeaky,
I got it working.
This is cool...
I created a blank report.
I went into Database Expert and chose my class as the source
I added a few fields to the report for testing
I created an arraylist (because I couldn't bind the class itself to the report)
I filled the arraylist with 1 instance of the class with data
I set the arraylist as the datasource of the report
I set the report as the datasource of the viewer
Loaded it in the INIT and everything worked.
Now I'm having an issue, but I haven't troubleshooted it too much yet.
I'll let you know if it keeps happening. weird thing.
Ron
|
IP Logged |
|
Capt_Ron
Newbie
Joined: 01 Dec 2006
Online Status: Offline
Posts: 7
|
Posted: 07 Dec 2006 at 5:32am |
It seems to be working fine now. I was having an issue with it not finding the report file.
On another note: How do I enable the Print and Export functions.
The buttons are there but I get errors when I try and use them.
Ron
|
IP Logged |
|
|