Rank: Newbie
Groups: Registered
Joined: 10/5/2021(UTC) Posts: 9 Location: Bakersfield
|
I managed to pull CenterPoint Data using the Data Browser and API using the C# sample tutorial. I get output into the Console just for those records with state of "MN".
So I am wondering how to get all the data without any filter from my report and get it downloaded into a csv file.
My two files are below; I have tried different methods without success, what would I need to change on my code to make this happened.
Note: I know on the CenterPoint DataBrowser section I would have to uncheck the State/Province box. I am specifically talking about the code. Ideally, I would want my csv file to look like the one pop up on the csv file when the export is done within the CenterPoint DataBrowser "Get Data" button.
PROGAM.CS FILE;
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using CenterPointDataBrowser; using System.IO;
namespace DataPull2CSV { class Program { static void Main(string[] args)
{
// While each report generates a DataSet, the DataSet will
// only contain one table.
DataSet ds = ReportDataExample.GetCustomerListData("MN");
// Sorting by City and customer name
List<DataRow> rows = ds.Tables[0].Select("", "City, Customer").ToList();
// Output the city and customer to the standard output window
foreach (DataRow row in rows)
Console.WriteLine("{0}: {1}", row["City"], row["Customer"]);
// Wait for key press
/Console.ReadKey();
} } }
REPORTDATAEXAMPLE.CS FILE;
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using CenterPointDataBrowser;
namespace CenterPointDataBrowser { public class ReportDataExample { public static DataSet GetCustomerListData(string stateAbbreviation) { string databaseName = "OURDATABASE NAME"; string reportKey = "df93cd71-1691-4925-9526-6d1bd69bc0c5"; string userName = "*******"; string password = "*******";
// Initialize the API DataAccessAPI api = new DataAccessAPI();
// Open the database api.OpenDatabase(databaseName,userName,password); // there is an over-loaded method if you need to supply a user/name password
// Retrieve the report Report report = api.GetReport(reportKey);
// Uncomment the following line of code line if you do not want any filters // If commented out, then the filters set in CP are applied. // report.ClearFilters();
// Set filters (optional) report.SetFilterText("Filter_State", TextOperators.Equals, stateAbbreviation);
// Uncomment the following line of code line if you do not want any options // If commented out, then the options set in CP are used // report.ClearOptions();
// Set options (optional)
// Gather the report's data // // Note: Some reports only return the data needed to produce the report. If you want // to return all data, then change GetReportData(false) to GetReportData(true) DataSet ds = report.GetReportData(false);
// Close the database api.CloseDatabase(); return ds; } } }
Thank you for the support
|
|
|
|
Rank: Advanced Member
Groups: Registered
Joined: 2/28/2018(UTC) Posts: 65 Location: Pennsylvani mostly Was thanked: 9 time(s) in 9 post(s)
|
If I'm reading this right, I'd change GetCustomerListData as follows:
1. Uncomment this line
// report.ClearFilters()
2. Get rid this line:
report.SetFilterText("Filter_State", TextOperators.Equals, stateAbbreviation);
|
|
|
|
Rank: Newbie
Groups: Registered
Joined: 10/5/2021(UTC) Posts: 9 Location: Bakersfield
|
I made those changes, but no luck. I saw those instructions on the manual, I guess I am more concern on how my code should look like on the PROGAM.CS FILE.
thank you for the support
|
|
|
|
Rank: Administration
Groups: Administrators, Moderator, Registered Joined: 2/28/2018(UTC) Posts: 53 Location: Red Wing, MN
Was thanked: 4 time(s) in 4 post(s)
|
OK, so what you really want to know is how to take the data you're getting back and output that to a CSV file. The simplest solution is to use the DataAccessAPI.OutputFile() method, ex. Code:api.OutputFile(report, ds, "c:\\files\\data_browser_example.csv");
Regards, - Aaron.
|
|
|
|
Rank: Newbie
Groups: Registered
Joined: 10/5/2021(UTC) Posts: 9 Location: Bakersfield
|
Thank you, so I managed to print all Cities and Customer to the console without restricting only to 'MN' state;
How could I modify so I can get all columns(Not just City and Customer) to output? Also, I added the line of code to get file output into a csv but can not seem to get it going.
Thank you for the support.
Below are the changes I made to my PROGRAM.CS FILE:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using CenterPointDataBrowser; using System.IO;
namespace DataPull2CSV { class Program { static void Main(string[] args)
{
// While each report generates a DataSet, the DataSet will
// only contain one table.
DataSet ds = ReportDataExample.GetReportData();
// Sorting by City and customer name
List<DataRow> rows = ds.Tables[0].Select().ToList();
// Output the city and customer to the standard output window
foreach (DataRow row in rows)
Console.WriteLine("{0}: {1}", row["City"], row["Customer"]);
// Wait for key press DataAccessAPI.OutputFile(Report,ds, "C:\\Users\\..\\data_browser_example.csv"); Console.ReadKey();
|
|
|
|
Rank: Advanced Member
Groups: Registered
Joined: 2/28/2018(UTC) Posts: 65 Location: Pennsylvani mostly Was thanked: 9 time(s) in 9 post(s)
|
After you Output the file, you could read all the data into a text field like so (replace C:\\USERS\\ETC\\ with the path you are using in the OutputFile. Code:
// Output data to console
var text = File.ReadAllText("C:\\USERS\\ETC\\data_browser_example.csv");
Console.WriteLine(text);
There is probably a lot more information there than you are interested in though so you could examine ds.Tables[0].Columns to see what columns are returned and just add them to the existing WriteLine call too. In the example below, I've added the customer abbreviation, state, and zip code to the WriteLine. Of course, you have to get the column names right or you will get an error, but you can get the column names from ds.Tables[0].Columns: Code:
foreach (DataRow row in rows)
Console.WriteLine($"{row["Abbreviation"]}, {row["Customer"]}, {row["City"]}, {row["State"]}, {row["Zip"]}");
|
|
|
|
Rank: Newbie
Groups: Registered
Joined: 10/5/2021(UTC) Posts: 9 Location: Bakersfield
|
Thank you, I know how to print now any field I would want from within any report, that worked.
I am still having some issues in the saving into a csv file time. Below are the changes I made;
ERROR I KEEP GETTING; An unhandled exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll Could not find file 'C:\Users\Documents\CenterPointDownloads\data_browser_example.csv'.
Changes Made to Code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using CenterPointDataBrowser; using System.IO;
namespace DataPull2CSV { class Program { static void Main(string[] args)
{
// While each report generates a DataSet, the DataSet will
// only contain one table.
DataSet ds = ReportDataExample.GetReportData();
// Sorting by City and customer name
List<DataRow> rows = ds.Tables[0].Select().ToList();
// Output the city and customer to the standard output window
foreach (DataRow row in rows) Console.WriteLine($"{row["Abbreviation"]}, {row["Customer"]}, {row["City"]}, {row["State"]}, {row["Zip"]}"); //Console.ReadKey(); // Output data to console string text = File.ReadAllText("C:\\Users\\Documents\\CenterPointDownloads\\data_browser_example.csv"); Console.WriteLine(text);
Also, how do I mark my code on a block the way you did on the last reply Thank you for the support
|
|
|
|
Rank: Advanced Member
Groups: Registered
Joined: 2/28/2018(UTC) Posts: 65 Location: Pennsylvani mostly Was thanked: 9 time(s) in 9 post(s)
|
If you put CODE in square brackets at the top of your code snippet and /CODE in square brackets at the bottom, it'll keep the formatting.
For the ReadAllText to work, the file has to exist - I don't see the OutputFile in your code anymore. You'd need to put this line before the File.ReadAllText to create the CSV file before you can read it in.
api.OutputFile(report, ds, ""C:\\Users\\Documents\\CenterPointDownloads\\data_browser_example.csv"");
And, of course, C:\Users\Documents\CenterPointDownloads would have to be a valid path on your PC.
|
|
|
|
Rank: Newbie
Groups: Registered
Joined: 10/5/2021(UTC) Posts: 9 Location: Bakersfield
|
EDIT; I forgot to add the error message: Error CS0119 'Report' is a type, which is not valid in the given context
I see, I apologized I made the changes, and below is the code I have now. I am sure you can tell I am a bit new to C#, so thank you for the patience here. So, I don't know what variable "Report" is representing on the following line; Code:
DataAccessAPI.OutputFile(Report,ds, "C:\\Users\\Documents\\CenterPointDownloads\\data_browser_example.csv");
Below is current script for my PROGRAM.CS FILE; Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using CenterPointDataBrowser;
using System.IO;
namespace DataPull2CSV
{
class Program
{
static void Main(string[] args)
{
// While each report generates a DataSet, the DataSet will
// only contain one table.
DataSet ds = ReportDataExample.GetReportData();
// Sorting by City and customer name
List<DataRow> rows = ds.Tables[0].Select().ToList();
// Output the city and customer to the standard output window
foreach (DataRow row in rows)
Console.WriteLine($"{row["Abbreviation"]}, {row["Customer"]}, {row["City"]}, {row["State"]}, {row["Zip"]}");
// Output data to console
DataAccessAPI.OutputFile(Report,ds, "C:\\Users\\Documents\\CenterPointDownloads\\data_browser_example.csv");
string text = File.ReadAllText("C:\\Users\\Documents\\CenterPointDownloads\\data_browser_example.csv");
Console.WriteLine(text);
}
}
}
Thank you Edited by user Thursday, October 7, 2021 12:39:36 AM(UTC)
| Reason: Forgot to add the new error I was getting, now is there
|
|
|
|
Rank: Advanced Member
Groups: Registered
Joined: 2/28/2018(UTC) Posts: 65 Location: Pennsylvani mostly Was thanked: 9 time(s) in 9 post(s)
|
No problem. Anyway, I was wondering what "Report" was referring to because I don't see it defined in the code. I'm guessing your code isn't compiling right now? That first parameter is supposed to be a CenterPointDataBrowser Report object. It gets set with a line of code like: Report report = api.GetReport(reportKey); // Where api is a DataAccessAPI variable So the setting of the Report variable and the api variable is probably all happening in ReportDataExample.GetReportData() - and your Main routine can't see those variables. You can bring all the code into your Main routine. You'd have to change the database name, report key, and probably provide a user name / password to the OpenDatabase, etc but you can see the general idea from the code I wrote to test answers: Code:
class Program
{
static void Main(string[] args)
{
string databaseName = "BusinessSample"; // change this
string reportKey = "998cabc1-f47c-401a-90aa-633d40136341"; // change this to the report key you want
// Initialize the API
DataAccessAPI api = new DataAccessAPI();
// Open the database
api.OpenDatabase(databaseName); // supply a user/name password if you need to
// Retrieve the report
Report report = api.GetReport(reportKey);
// Get Data
DataSet ds = report.GetReportData();
// Output Data to CSV
var outputFileName = "D:\\TestData\\data_browser_example.csv"l // change this to the file name you want
api.OutputFile(report, ds, outputFileName);
// Output data to console (only select fields)
List<DataRow> rows = ds.Tables[0].Select("", "City, Customer").ToList();
foreach (DataRow row in rows)
Console.WriteLine($"{row["Abbreviation"]}, {row["Customer"]}, {row["City"]}, {row["State"]}, {row["Zip"]}");
// Output to console - everything that is getting sent to the CSV file
string text = File.ReadAllText(outputFileName); // read the entire file in as one big string
Console.WriteLine(text); // output the text to the console
// Wait for key press
Console.ReadKey();
}
}
|
|
|
|
Rank: Newbie
Groups: Registered
Joined: 10/5/2021(UTC) Posts: 9 Location: Bakersfield
|
Thank you so much. I got the csv output I was looking for after making the below changes to my code and reading all the notes on the last message you provided.
Now I can go home and sleep comfortably. I might follow up tomorrow for any further questions if you don't mind.
Again, thanks a ton for the support, means a lot.
|
|
|
|
Rank: Newbie
Groups: Registered
Joined: 10/5/2021(UTC) Posts: 9 Location: Bakersfield
|
I wanted to follow up on this thread, questions I asked a couple of weeks ago. Does any of you know how to run a c# file from within the command line? I am trying to automate a job to run the report pull scrip on a daily basis, but I have not gotten any luck in being able to run this script from the command line. Below is what I tried: https://www.geeksforgeek...ram-on-cmd-command-line/was able to get the csc command successfully running, but when I run the file on the command folder and its respective location I get the below error; "CenterPoint_GeneralLedger_LastMonthPull.cs(7,7): error CS0246: The type or namespace name 'CenterPointDataBrowser' could not be found (are you missing a using directive or an assembly reference?)" Not sure what is going on because when I run the script manually on visual studio code works just fine. Thanks for the support.
|
|
|
|
Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.
Important Information:
The Red Wing Software Developer Forum uses cookies. By continuing to browse this site, you are agreeing to our use of cookies.
More Details
Close