1. Miraben Apani
  2. SnapDevelop
  3. Monday, 6 February 2023 18:42 PM UTC

Hi,

At PowerBuilder side, I am able to get two DWs with all (existing + modified)data in ls_json using JSON Package, at API side I am using DataPacker and DataUnpacker, I have  tried using Update() and SaveChanges() to save data of both .NET datastores- unable to Save changes in database.

When I debug the code both Datastores[one for each DW] at WEB API level comes with Column names only, no data exists.

Any suggestion on "how to" at API side,

1. I can retrieve all (existing + modified) data in SnapDevelop WEB API?

2. Data gets saved in database successfully in WEB API?

I am using below Code snippet in WEB API:

 public void UpdateAll()
        {
          
            
            var dataStore1 = new DataStore("DW_1", _dataContext);
             var dataStore2 = new DataStore("DW_2", _dataContext);
          
                         
            var dataPacker = new DataPacker();
            
            
            var key1 = nameof(dataStore1);
           var key2 = nameof(dataStore2);
       
             dataPacker.AddDataStore(key1, dataStore1, true);
          dataPacker.AddDataStore(key2, dataStore2, true);
           
 
      
            string text = dataPacker.GetTextString(DataFormat.Json);
            
            var dataUnpacker = new DataUnpacker(text, DataFormat.Json);
            
            // Gets the DataStore object from dataUnPacker according to the key.
            var ds1 = dataUnpacker.GetDataStore(key1,_dataContext);
            var ds2 = dataUnpacker.GetDataStore(key2, _dataContext);
            
            //ds1.SaveChanges();
            try
            {
                _dataContext.BeginTransaction();
                
                //  ds1.Update();
                //  ds2.Update();
                ds1.SaveChanges(_dataContext);
                ds2.SaveChanges(_dataContext);
              
                
                
            }
            catch (Exception)
            {
                _dataContext.Rollback();
            }
            
               }
        

Thanks!

 

Miraben Apani Accepted Answer Pending Moderation
  1. Wednesday, 8 February 2023 16:27 PM UTC
  2. SnapDevelop
  3. # 1

Logan,

 

Thanks very much for the example of controller code. it is working well.

I have a question I am getting this error "System.ArgumentNullException: Value cannot be null. (Parameter 'name')" and I am thinking it is because "datacontext" is missing in below code snippet: 

 IDataStore ds1= dataUnpacker.GetDataStore("dw_1");
            IDataStore ds2= dataUnpacker.GetDataStore("dw_2");

 

any documentation/ "how-to" define and use DataContext in Controller? in my Service Implementation I already have  datacontext defined.

Thanks !

Comment
There are no comments made yet.
Logan Liu @Appeon Accepted Answer Pending Moderation
  1. Tuesday, 7 February 2023 02:54 AM UTC
  2. SnapDevelop
  3. # 2

Hi Miraben,

I can't see your controller code. Your "public void UpdateAll()" method doesn't pass in any data. And both dataStore 1 and dataStore 2 in it never called the Retrieve() method to get any data.

If only for testing DataPacker in this method, please call dataStore1.Retrieve() to ensure the data row is not empty, then add it to datapacker.

 

My suggestions for receiving data by the Web API:

1) Verify the ConfigureServices method in StartUp.cs:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers(m =>
    {
        m.UseCoreIntegrated(); // It will support the IDataUnpacker type in Controller.
        m.UsePowerBuilderIntegrated();
    });
    
    ...
}

And verify the Configure method in StartUp.cs: there should be a line "app.UseDataWindow();" to map the datawindow object name to the .NET DataStore C# models.

 

2) Use the IDataUnpacker type parameter in the controller of the Web API to receive your JSON package data.

E.g.:

[HttpPost]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public ActionResult Modify(IDataUnpacker dataUnpacker)
{
    // Declare and initialize local .NET DataStore variables
    // By getting the Packaged DataStores
    IDataStore order = dataUnpacker.GetDataStore("order", _context);
    IDataStore orderItems = dataUnpacker.GetDataStore("orderitems", _context);

    // Check if the modification was successful
    if (_service.Modify(order, orderItems))
    {
        // Return Status Code 200
        return Ok();
    }
    else
    {
        // Return a Status Code 500 for Internal Server Error
        return StatusCode(StatusCodes.Status500InternalServerError);
    }
}

Regards, Logan
Comment
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.