logo
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
payrollproject  
#1 Posted : Tuesday, January 7, 2020 4:45:57 PM(UTC)
payrollproject

Rank: Member

Groups: Registered
Joined: 1/3/2020(UTC)
Posts: 11

Thanks: 2 times
"RedWingSoftware.CenterPoint.DataAccessFailureException: 'The ConnectionString property has not been initialized.'"

I am getting this runtime error when attempting to run the example below that is pulled straight from the API Help documentation which got shipped with CenterPoint.

Is there a previous connection step that the example is not showing?

Code:

// Example of using the EmployeePresenter class
using System;
using RedWingSoftware.CenterPoint.Api;

class Sample
{
  public static void Main()
  {
     var presenter = new EmployeePresenter();

     // Get a list of employees
     var employeeList = presenter.GetList();

     // Retrieve data for the first employee in the list
     var id = employeeList.First().Id;
     var employee = presenter.LoadEmployee(id);

     Console.WriteLine("Employee name before change: {0}", employee.FirstLast());

     employee.LastName = "Smith";

     Console.WriteLine("Employee name after change: {0}", employee.FirstLast());

     presenter.SaveEmployee(employee);

     // Reload the employee
     employee = presenter.LoadEmployee(id);

     Console.WriteLine("Employee name after reload: {0}", employee.FirstLast());
  }
}

Edited by user Tuesday, January 7, 2020 4:47:41 PM(UTC)  | Reason: Not specified

payrollproject  
#2 Posted : Tuesday, January 7, 2020 7:57:31 PM(UTC)
payrollproject

Rank: Member

Groups: Registered
Joined: 1/3/2020(UTC)
Posts: 11

Thanks: 2 times
Do I have to configure Visual Studio project to connect to SQL server somehow before running code?
tony  
#3 Posted : Wednesday, January 8, 2020 12:23:18 AM(UTC)
tony

Rank: Advanced Member

Groups: Registered
Joined: 2/28/2018(UTC)
Posts: 65
United States
Location: Pennsylvani mostly

Was thanked: 9 time(s) in 9 post(s)
The sample code is assuming an open database - and I probably should change that example code to at least acknowledge that fact.

So you need to open a database. To open a database you need a reference to RedWingSoftware.ExternalAPI.L0.V1.dll and call the OpenDatabaseByName method as shown below:

Code:

         // Initialize api and Open Database
         var api = new CenterPointExternalAPI();
         api.Initialize();
         api.OpenDatabaseByName("PayrollSample"); // There's an overloaded method in case your database is password-protected for which you would need to 



There's also a CloseDatabase() method (as you'd expect).

Edited by user Wednesday, January 8, 2020 12:24:01 AM(UTC)  | Reason: Not specified

thanks 1 user thanked tony for this useful post.
payrollproject on 1/8/2020(UTC)
payrollproject  
#4 Posted : Wednesday, January 8, 2020 7:54:38 PM(UTC)
payrollproject

Rank: Member

Groups: Registered
Joined: 1/3/2020(UTC)
Posts: 11

Thanks: 2 times
This was able to clear my original error.
payrollproject  
#5 Posted : Wednesday, January 8, 2020 8:41:22 PM(UTC)
payrollproject

Rank: Member

Groups: Registered
Joined: 1/3/2020(UTC)
Posts: 11

Thanks: 2 times
Update: the code does not connect to the database using the databaseName, username, and password provided. I know I entered the correct information in my code because I'm able to connect in In SSMS (Microsoft SQL Server Management Studio) as well as Navicat.

Is there a server name which must be specified somewhere in the code as well? For example, in SSMS I use "localhost/RWS" as my server name.

I configured the database in Visual Studio as well (perhaps incorrectly). PayrollSample.dbo is under my database objects.

Code:

A connection to the database could not be established, the database or your connection to the server may no longer be valid.

Please check your database and server configuration then try again

Error: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.

Edited by user Wednesday, January 8, 2020 8:51:59 PM(UTC)  | Reason: Added output

aarony  
#6 Posted : Wednesday, January 8, 2020 8:51:07 PM(UTC)
aarony

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)
The Username and Password parameters of the "OpenDatabaseByName" method refer to CenterPoint Usernames and Passwords, not SQL Server Usernames and Passwords.

Your connection to the SQL database should already be established through CenterPoint.

So the API uses the "OpenDatabaseByName" method to open a Database that has already been configured for use by CenterPoint.
If you are not using the Security features in CenterPoint then a Username and Password is not required.

Regards,

- Aaron.
payrollproject  
#7 Posted : Thursday, January 9, 2020 5:26:33 PM(UTC)
payrollproject

Rank: Member

Groups: Registered
Joined: 1/3/2020(UTC)
Posts: 11

Thanks: 2 times
I'm getting a runtime error "PayrollSample" cannot be opened. I left off the username/password, and I'm just using the code provided verbatim.

I am able to browse the PayrollSample database from within Visual Studio (I added it as a Data Connection under Server Explorer). Could it be a configuration setting I'm missing somewhere? Maybe the code is trying to connect to a different server? I am on my local host.

Output:
Code:

Parameter name: databaseName</Message><StackTrace>   at RedWingSoftware.CenterPoint.ExternalAPI.BaseExternalAPI.OpenDatabaseByName(String databaseName, String userName, String password)
   at RedWingSoftware.CenterPoint.ExternalAPI.BaseExternalAPI.OpenDatabaseByName(String databaseName)
   at PayrollScript.Program.Main(String[] args) in C:\Users\aharbison\Desktop\PayrollScript\PayrollScript\Program.cs:line 18</StackTrace><ExceptionString>System.ArgumentException: The database PayrollSample could not be opened.
Parameter name: databaseName
   at RedWingSoftware.CenterPoint.ExternalAPI.BaseExternalAPI.OpenDatabaseByName(String databaseName, String userName, String password)
   at RedWingSoftware.CenterPoint.ExternalAPI.BaseExternalAPI.OpenDatabaseByName(String databaseName)
   at PayrollScript.Program.Main(String[] args) in C:\Users\aharbison\Desktop\PayrollScript\PayrollScript\Program.cs:line 18</ExceptionString></Exception></TraceRecord>
An unhandled exception of type 'System.ArgumentException' occurred in RedWingSoftware.ExternalAPI.L0.V1.dll
The database PayrollSample could not be opened.

'PayrollScript.exe' (CLR v4.0.30319: PayrollScript.exe): Loaded 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\PrivateAssemblies\Runtime\Microsoft.VisualStudio.Debugger.Runtime.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
The program '[12088] PayrollScript.exe' has exited with code 0 (0x0).


The program:
Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RedWingSoftware.CenterPoint.Api;
using RedWingSoftware.CenterPoint.ExternalAPI;

namespace PayrollScript
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize api and Open Database
            var api = new CenterPointExternalAPI();
            api.Initialize();
            api.OpenDatabaseByName("PayrollSample");

            var presenter = new EmployeePresenter();

            // Get a list of employees
            var employeeList = presenter.GetList();

            // Retrieve data for the first employee in the list
            var id = employeeList.First().Id;
            var employee = presenter.LoadEmployee(id);

            Console.WriteLine("Employee name before change: {0}", employee.FirstLast());

            employee.LastName = "Smith";

            Console.WriteLine("Employee name after change: {0}", employee.FirstLast());

            presenter.SaveEmployee(employee);

            // Reload the employee
            employee = presenter.LoadEmployee(id);

            Console.WriteLine("Employee name after reload: {0}", employee.FirstLast());

            api.CloseDatabase();
        }
    }
}

Edited by user Thursday, January 9, 2020 5:29:46 PM(UTC)  | Reason: Not specified

aarony  
#8 Posted : Thursday, January 9, 2020 5:47:20 PM(UTC)
aarony

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)
The code you have posted should work fine.
Verify that you can access the "PayrollSample" database with the CenterPoint application, it sounds like there is an issue there.
payrollproject  
#9 Posted : Friday, January 10, 2020 5:26:36 PM(UTC)
payrollproject

Rank: Member

Groups: Registered
Joined: 1/3/2020(UTC)
Posts: 11

Thanks: 2 times
I'm able to access the sample database using the CenterPoint application so everything is ok there.

In code, I've been able to successfully connect to the database in the past; however, it is using the DataAccessAPI class. For example, the following code (which was auto-generated from your tools) works for me. But the examples I've listed earlier do not.

Code:

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 DataSet GetReportData()
      {
         string databaseName = "PayrollSample";
         string reportKey = "968465a0-1b3c-47b8-9b80-915ad2888037";

         // Initialize the API
         DataAccessAPI api = new DataAccessAPI();

         // Open the database
         api.OpenDatabase(databaseName); // 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)


         // 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;
      }
   }
}

Edited by user Friday, January 10, 2020 5:35:15 PM(UTC)  | Reason: Not specified

payrollproject  
#10 Posted : Friday, January 10, 2020 6:22:13 PM(UTC)
payrollproject

Rank: Member

Groups: Registered
Joined: 1/3/2020(UTC)
Posts: 11

Thanks: 2 times
I am going to try to reset my Dev environment and reinstall center point, then report back.
Users browsing this topic
Guest (3)
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.