object data null after get request in Dotnet webapi project - c#

I have created two model classes Organisation.cs and Employee.cs
public class Organisation
{
private static string organisationName="FALCON";
private int employeeCount=2;
private string ceoName;
private List<string> departmentNames;
[JsonIgnore]
private List<Employee> employees;
private List<Address> addesses;
private bool isCeoChangeAvailable= true;
public string OrganisationName
{
get {return organisationName;}
set {organisationName = value;}
}
public int EmployeeCount
{
get {return employeeCount;}
set { employeeCount = value; }
}
public string CeoName
{
get { return ceoName; }
set { ceoName = value; }
}
public List<string> Departments
{
get { return departmentNames; }
set { departmentNames = value; }
}
[IgnoreDataMember]
public List<Employee> Employees
{
get {return employees; }
set {employees=value; }
}
public List<Address> OfficeAdresses
{
get {return addesses; }
set { addesses = value; }
}
public bool IsCeoChange
{
get{ return isCeoChangeAvailable; }
set { isCeoChangeAvailable = value; }
}
public Organisation(string ceoName,List<string> departments,List<Address> addresses)
{
this.CeoName = ceoName;
this.Departments = departments;
this.OfficeAdresses = addresses;
}
public Organisation() { }
}
and an employee class
public class Employee
{
private Guid employeeId;
private string name;
private string departmentName;
private bool isManager;
private int salary;
public Guid Id
{
get
{ return employeeId;}
set
{ employeeId = value;}
}
public string Name
{
get
{ return name;}
set
{ name = value;}
}
public bool IsManager
{
get
{ return isManager;}
set
{ isManager = value;}
}
public string DepartmentName
{
get { return departmentName;}
set { departmentName = value;}
}
public int Salary
{
get { return salary; }
set { salary = value; } }
public Employee()
{
}
}
have a two controllers for Employee and Organisation, I am trying to post Organisation data and storing it in a variable.
In my organisaiton controller I have this method
public IActionResult PostOrganisationData(Organisation organisation)
{
_organisationData.PostOrganisationDetails(organisation);
return Created(HttpContext.Request.Scheme + "://" + HttpContext.Request.Host + HttpContext.Request.Path + "/" + organisation.OrganisationName, organisation);
}
In my datalayer have a class MockEmployeeData which implements IOrganisationData interface
here is the code where I am constructing the Organisation object and assigning it to an instance of Organisation object. During the request The organisation object holds the assigned data. Here is the code
private Organisation organisation = new Organisation();
public Organisation PostOrganisationDetails(Organisation organisation)
{
Organisation organisationObject = new Organisation(organisation.CeoName, organisation.Departments, organisation.OfficeAdresses);
organisation = organisationObject;
return organisation;
}
When I call the Get action method which gets the organisation details from the controller the previously assigned object has properties with null. Here is the get method
[HttpGet]
[Route("api/[controller]")]
public IActionResult GetOrganisationDetails()
{
var organisationData=_organisationData.GetOrganisationData();
return Ok(organisationData);
}
public Organisation GetOrganisationData()
{
Organisation organisationObject = new Organisation();
organisationObject = organisation;
return organisationObject;
}
Here in this method I,m assigning the variable which holds the previously assigned data from post request and getting back the object where all properties have gone null now.Please help me solve this

You need to make your properties in your Organisation model class to nullable. Now you can do this by either manually making each property nullable in your class:
public class Organisation
{
public string? OrganisationName
{
get {return organisationName;}
set {organisationName = value;}
}
public int? EmployeeCount
{
get {return employeeCount;}
set { employeeCount = value; }
}
public string? CeoName
{
get { return ceoName; }
set { ceoName = value; }
}
public List<string?> Departments
{
get { return departmentNames; }
set { departmentNames = value; }
}
....so on
Or you can globally enable this property by editing your csproj file which contains your project properties:
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<!--<Nullable>enable</Nullable>-->
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

Related

I dont understand the error.. I think as far as I know there is a dataType, but I dont see how?

In the code below I hope that I specify correctly whats wrong. I need to be able to call item.departments.dept_Type and that should be possible because of the association that I have made. I don't need to create inner joins on a query to get the data if I understand correctly.
This is my PersonClass
namespace DATALAYER.DataHandler
{
[Table(Name = "People")]
public class Person
{
private int _DepartmentID;
public EntityRef<Department> _Department;
public Person() { this._Department = new EntityRef<Department>(); }
private int _ID;
[Column(IsPrimaryKey =true, Storage ="_ID")]
public int ID
{
get { return this._ID; }
set { this._ID = value; }
}
private string _p_FirstName;
[Column(Storage = "_p_FirstName")]
public string p_FirstName
{
get { return this._p_FirstName; }
set { this._p_FirstName = value; }
}
private string _LastName;
[Column(Storage = "_LastName")]
public string p_LastName
{
get { return this._LastName; }
set { this._LastName = value; }
}
private string _EmailAddress;
[Column(Storage = "_EmailAddress")]
public string p_EmailAddress
{
get { return this._EmailAddress; }
set { this._EmailAddress = value; }
}
private string _Password;
[Column(Storage = "_Password")]
public string p_Password
{
get { return this._Password; }
set { this._Password = value; }
}
private string _SSID;
[Column(Storage = "_SSID")]
public string p_SSID
{
get { return this._SSID; }
set { this._SSID = value; }
}
private string _DOB;
[Column(Storage = "_DOB")]
public string p_DOB
{
get { return this._DOB; }
set { this._DOB = value; }
}
private string _CellNumber;
[Column(Storage = "_CellNumber")]
public string p_CellNumber
{
get { return this._CellNumber; }
set { this._CellNumber = value; }
}
[Column(Storage = "_DepartmentID", DbType = "Int")]
public int p_Department_dept_ID
{
get { return this._DepartmentID; }
set { this._DepartmentID = value; }
}
[Association(Storage = "_DepartmentID", ThisKey = "p_Department_dept_ID")]
public Department Department
{
get { return this._Department.Entity; }
set { this._Department.Entity = value; }
}
}
}
This is my Department code
namespace DATALAYER.DataHandler
{
[Table(Name = "Departments")]
public class Department
{
//private EntitySet<Person> _Person;
//public Department()
//{
// this._Person = new EntitySet<Person>();
//}
private int _DepartmentID;
[Column(IsPrimaryKey = true, Storage = "_DepartmentID")]
public int dept_ID
{
get { return this._DepartmentID; }
set { this._DepartmentID = value; }
}
private string _deptType;
[Column(Storage = "_deptType")]
public string dept_Type
{
get { return this._deptType; }
set { this._deptType = value; }
}
//[Association(Storage = "_Person", OtherKey = "ID")]
//public EntitySet<Person> Persons
//{
// get { return this._Person; }
// set { this._Person.Assign(value); }
//}
}
}
Now the problem I'm thinking is that there is a problem with datatypes between the primary key of the person and a foreign key the departments. But Since they are both int I don't see how that can be a problem.
If someone can just explain my problem to cleary if Im wrong or something or help me with a solution pls.
Added this class
namespace DATALAYER.DataHandler
{
public class SHSdb2 : DataContext
{
public Table<Person> People;
public Table<Department> Department;
//public Table<Address> Address;
public SHSdb2(string connection) : base(connection) { }
}
}
Person table
Department table
Without this code it works but then I can only call stuff inside the table
[Column(Storage = "_DepartmentID", DbType = "Int")]
public int p_Department_dept_ID
{
get { return this._DepartmentID; }
set { this._DepartmentID = value; }
}
[Association(Storage = "_DepartmentID", ThisKey = "p_Department_dept_ID")]
public Department Department
{
get { return this._Department.Entity; }
set { this._Department.Entity = value; }
}
Seems like I fixed it with this not sure if its a fix or I just lucky if someone can still maybe explain this to me it would be much appreciated.
var personQuery =
from per in db.People
where per.p_FirstName == "Christian"
select per.Department;
I'm thinking by specifying that it needs to get the data from department it would allow it in the foreach.

Custom Collection/List To Store Custom Objects

I have a customer object class:
public class customerObject
{
private string _address1;
private string _address2;
private string _address3;
private string _category;
private string _country;
private string _county;
private string _custcode;
private string _fullname;
private string _int_rep_hou;
private string _int_rep_key;
private double _lat;
private double _lng;
private string _postcode;
private string _rep_code;
private string _telephone;
public customerObject()
{
}
public string Address1
{
get { return _address1; }
set { _address1 = value; }
}
public string Address2
{
get
{
return _address2;
}
set { _address2 = value; }
}
public string Address3 { get { return _address3; } set { _address3 = value; } }
public string Category
{
get { return _category; }
set { _category = value; }
}
public string Country { get { return _country; } set { _country = value; } }
public string County { get { return _county; } set { _county = value; } }
public string Custcode
{
get { return _custcode; }
set { _custcode = value; }
}
public string Fullname
{
get { return _fullname; }
set { _fullname = value; }
}
public string Int_rep_hou
{
get { return _int_rep_hou; }
set { _int_rep_hou = value; }
}
public string Int_rep_key
{
get { return _int_rep_key; }
set { _int_rep_key = value; }
}
public double Lat { get { return _lat; } set { _lat = value; } }
public double Lng { get { return _lng; } set { _lng = value; } }
public string Postcode { get { return _postcode; } set { _postcode = value; } }
public string Rep_code
{
get { return _rep_code; }
set { Rep_code = value; }
}
public string Telephone { get { return _telephone; } set { _telephone = value; }
}
}
I have a CustomCollections class
public class CustomerCollection
{
public List<customerObject> Customers { get; set; }
}
My method that loops through dt rows and converts to a customer object
public List<Valueobjects.CustomerCollection> dolist(DataTable temptablename)
{
//Create Collection Object
Valueobjects.CustomerCollection Collection = new Valueobjects.CustomerCollection();
foreach (DataRow row in temptablename.Rows)
{
//Create Customer Object
Valueobjects.customerObject Customer = new Valueobjects.customerObject();
//set values of customer object
Customer.Rep_code = "";
Customer.Int_rep_key = "";
Customer.Int_rep_hou = "";
Customer.Fullname = row["Fullname"].ToString().Trim();
Customer.Custcode = row["Custcode"].ToString().Trim();
Customer.Category = row["Category"].ToString().Trim();
Customer.Address1 = row["Address1"].ToString().Trim();
Customer.Address2 = row["Address2"].ToString().Trim();
Customer.Address3 = row["Address3"].ToString().Trim();
Customer.Postcode = row["Postcode"].ToString().Trim();
Customer.Country = row["Country"].ToString().Trim();
Customer.Telephone = row["Telephone"].ToString().Trim();
Customer.Lat = Convert.ToDouble(row["Lat"]);
Customer.Lng = Convert.ToDouble(row["Lng"]);
Customer.County = row["County"].ToString().Trim();
//add to the collection (list)
Collection.Customers.Add(Customer);
}
temptablename = null;
return Collection;
}
However when I create a new Customer object and a new CustomerCollection object I am getting an error when adding the customer to the collection list.
Error:
Error 32 Cannot implicitly convert type
'Classes.Valueobjects.CustomerCollection' to
'System.Collections.Generic.List'
Your method is returning a List<CustomerCollection>:
public List<Valueobjects.CustomerCollection> dolist(DataTable temptablename)
{
//...
}
But the code is trying to return a CustomerCollection:
return Collection;
Just as the error says, these two types are different.
If a CustomerCollection is already a collection of customers, then semantically what is a List<Valueobjects.CustomerCollection>? A collection of collections? It seems like you're over-pluralizing your objects :)
There are two approaches here. Either return a CustomerCollection from the method:
public CustomerCollection dolist(DataTable temptablename)
{
//...
}
Or use a List<Customer> if you want to use generic lists as your collection containers:
public List<Customer> dolist(DataTable temptablename)
{
//...
var Collection = new List<Customer>();
//...
Collection.Add(Customer);
//...
return Collection;
}
Side note: You may want to stick to C# conventions for variable naming. As you can see from the code highlighting here on Stack Overflow, your variable names can easily be mistaken for classes/types, which can cause confusion when supporting the code.
Return a CustomerCollection instead of a List<Valueobjects.CustomerCollection>:
public Valueobjects.CustomerCollection Dolist(DataTable temptablename)
{
// ...
Your object has a list, it is not a list.
MSDN: Inheritance

How to use the Same Class on Client as on the Service?

How can I pass an entire defined class through a WCF service? I have the class defined on both the service and client side. I keep getting an error:
Best overloaded method match has some invalid arguments.
The whole class was copied from the client-side to the service-side.
Client side calling:
TransferProxy.PutTransferOnService(Transfer);
Defined on service:
[OperationContract]
bool PutTransferOnService(TypeTransfer Transfer);
I don't want to access individual items on the class from the client, I just want to move the WHOLE populated object through and do processing on the server side.
[DataContract]
public class TypeTransfer
{
private string userID;
private string transferNum;
private DateTime effectiveDate;
private int unitCount;
private int skuCount;
private string reason;
private string localStatus;
private string destStatus;
private string carrier;
private string sourceStore;
private string destinationStore;
private string inSeal;
private string outSeal;
[DataMember]
private List<TypeSOQ> correspondingSOQ = new List<TypeSOQ>();
[DataMember]
private List<TypeProductList> ProductList = new List<TypeProductList>();
public TypeTransfer() { }
// Function adds single item to transfer object
public void AddItem(int ProductID, string SKU, string PrimarySKU, string SCC, string ProductDescription, int TransferQty)
{
ProductList.Add(new TypeProductList
{
productID = ProductID,
sku = SKU,
primaryUPC = PrimarySKU,
scc = SCC,
description = ProductDescription,
transferQty = TransferQty
});
}
// Add SOQ to transfer object (can support multiple SOQ's)
public void AddSOQ(TypeSOQ soq)
{
correspondingSOQ.Add(soq);
}
// Function returns number of skus in Product List
public int GetSKUTotal()
{
return ProductList.Count();
}
// Function returns total number of items in transfer
public int GetItemTotal()
{
int itemtotal = 0;
for (int i = 0; i < ProductList.Count(); i++)
{
itemtotal += ProductList[i].transferQty;
}
return itemtotal;
}
// Return entire SOQ list
public List<TypeSOQ> GetSOQs()
{
return correspondingSOQ;
}
// Returns full product list in transfer object
public List<TypeProductList> GetProductList()
{
return ProductList;
}
[DataMember]
public string UserID
{
get { return userID; }
set { userID = value; }
}
[DataMember]
public string TransferNum
{
get { return transferNum; }
set { transferNum = value; }
}
[DataMember]
public DateTime EffectiveDate
{
get { return effectiveDate; }
set { effectiveDate = value; }
}
[DataMember]
public int UnitCount
{
get { return unitCount; }
set { unitCount = value; }
}
[DataMember]
public string Reason
{
get { return reason; }
set { reason = value; }
}
[DataMember]
public string LocalStatus
{
get { return localStatus; }
set { localStatus = value; }
}
[DataMember]
public string DestStatus
{
get { return destStatus; }
set { destStatus = value; }
}
[DataMember]
public string Carrier
{
get { return carrier; }
set { carrier = value; }
}
[DataMember]
public string SourceStore
{
get { return sourceStore; }
set { sourceStore = value; }
}
[DataMember]
public string DestStore
{
get { return destinationStore; }
set { destinationStore = value; }
}
[DataMember]
public string InSeal
{
get { return inSeal; }
set { inSeal = value; }
}
[DataMember]
public string OutSeal
{
get { return outSeal; }
set { outSeal = value; }
}
[DataMember]
public int SKUCount
{
get { return skuCount; }
set { skuCount = value; }
}
}
You said - The whole class was copied from the client-side to the service-side.
You don't need to copy your class to server side. just define your class in a separate library and give reference of that same library to both client and server.

Newtonsoft.Json : Cant get JSON data to convert into C# Object

Call to a REST based API returns me data in JSON format(stored in variable strJSONStringFromAPI).
{
"id": "551",
"name": "Dev D",
"work": [
{
"employer": {
"name": "Microsoft Corporation"
},
"position": {
"name": "Software Development"
}
}
],
"gender": "male"}
I have created following classes corresponding to above JSON data
public class Employer
{
private string _name;
public string name
{
get { return _name; }
set { _name = value; }
}
}
public class Position
{
private string _name;
public string name
{
get { return _name; }
set { _name = value; }
}
}
public class Work
{
private Employer _employer;
private Position _position;
public Employer employer
{
get { return _employer; }
set { _employer = value; }
}
public Position position
{
get { return _position; }
set { _position = value; }
}
}
public class UserInfo
{
private string _id;
private string _name;
private Work[] _wk;
public string id
{
get { return _id; }
set { _id = value; }
}
public string name
{
get { return _name; }
set { _name = value; }
}
public Work[] work
{
get { return _wk; }
set { _wk = value; }
}
}
Now i have method GetUserInfo which should return object UserInfo as shown below
Public UserInfo GetUserDetails()
{
UserInfo user = New UserInfo();
user = Newtonsoft.Json.JsonConvert.DeserializeObject<UserInfo>(strJSONStringFromAPI);
return user;
}
later i will access the values as
label1.text = user.ID ;
label2.text = user.name;
As of now i am getting all properties of above user object as NULL (user.ID = null etc etc)
I know i am missing something very important here..can someone help me what else needs to be done in in Employer , Position and Work classes so that i get proper values (eg user.ID = "551" etc)
Your Work class above will not compile.
public class Work
{
private Employer _employer;
private Position _position;
public Employer employer
{
get { return _employer; }
set { _employer = value; }
}
public Position position
{
get { return _employer; }
set { _employer = value; }
}
}
The Position property can't use _employer.
Tested your code with that corrected, it does work as expected. Here's a simple test using a HTTP Handler:
<%# WebHandler Language="C#" Class="JsonDotnet" %>
using System;
using System.IO;
using System.Web;
using Newtonsoft.Json;
public class JsonDotnet : IHttpHandler {
public void ProcessRequest (HttpContext context) {
string json = context.Server.MapPath(
"~/app_data/json-test.txt"
);
UserInfo user = Newtonsoft.Json.JsonConvert
.DeserializeObject<UserInfo>(
File.ReadAllText(json)
);
context.Response.Write(user.id + "<br>");
context.Response.Write(user.name + "<br>");
context.Response.Write(user.work[0].employer.name + "<br>");
}
public bool IsReusable {
get { return false; }
}
public class Employer {
public string name { get; set;}
}
public class Position {
public string name { get; set;}
}
public class Work {
public Employer employer { get; set;}
public Position position { get; set;}
}
public class UserInfo {
public string id { get; set;}
public string name { get; set;}
public Work[] work { get; set;}
}
}
Don't forget to put your json string in ~/app_data/json-test.txt.
Are you sure strJSONStringFromAPI is exactly the same as the string you specified in your first code snippet above?
Your private variable should be public when you deserialize the json object and all name should be same in both in json object as well as in classes

How do you do joins in hibernate activerecord?

How do I do a join that would return me results similar to-
SELECT * FROM Project LEFT JOIN ProjectRegion ON ProjectRegion.id = Project.projectRegion
I currently use the syntax-
Project[] projects = Project.FindAll();
My tables are setup with ActiveRecord/hibernate as follows -
[ActiveRecord]
public class ProjectRegion : ActiveRecordBase<ProjectRegion>
{
private int id;
private String title;
public ProjectRegion()
{
}
[PrimaryKey]
public int Id
{
get { return id; }
set { id = value; }
}
[Property]
public String Title
{
get { return title; }
set { title = value; }
}
}
[ActiveRecord]
public class Project : ActiveRecordBase<Project>
{
private int id;
private String projectNumber;
private String projectRegion;
public Project()
{
}
[PrimaryKey]
public int Id
{
get { return id; }
set { id = value; }
}
[Property]
public String ProjectInternalCode
{
get { return projectNumber; }
set { projectNumber = value; }
}
[Property]
public String ProjectRegion
{
get { return projectRegion; }
set { projectRegion = value; }
}
...
If you declare the ProjectRegion property in the Project class to be of type ProjectRegion instead of string, when NHibernate fetches a Project it will automatically join (according to a fetch policy) the ProjectRegion table.

Categories