0

I am new to c# ,I want to convert a model class object to DataTable in Web API.

Model Class

 public class Employee_Master_Model
    {
        public string UserType { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string MiddleName { get; set; }
        public string Mobile { get; set; }
        public string Phone { get; set; }
        public string Email { get; set; }
        public bool IsActive { get; set; }
        public int EmployeeId { get; set; }
    }

Controller:

 public async Task<IActionResult> CreateEmployee([FromBody] Employee_Master_Model employee_Master_Model)
        {
            try
            {
              // Need to convert the object (employee_Master_Model) to DataTable
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

trying it for hours can't get it done .Can anyone help me to fix this .

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Zhu
  • 3,679
  • 14
  • 45
  • 76

3 Answers3

7

By the following code you can dynamically create the datatable columns:

PropertyDescriptorCollection props = 
    TypeDescriptor.GetProperties(typeof(Employee_Master_Model));
DataTable dt = new DataTable();
foreach (PropertyDescriptor p in props)
    dt.Columns.Add(p.Name, p.PropertyType);

The rest is just copying entity fields values to datatable columns.

Amir Molaei
  • 3,700
  • 1
  • 17
  • 20
1

Why can't you just create a datatable and fill it with the model data value like

DataTable dt = new DataTable();

DataColumn c = new DataColumn("FirstName");
dt.Columns.Add(c);

DataRow r = dt.NewRow();
r["FirstName"] = model.FirstName; // model is an instance of Employee_Master_Model
Rahul
  • 76,197
  • 13
  • 71
  • 125
  • @PrasadTelkikar May be possible but that's not realistic since having 100 column in a single table is against normalization similarly a type having 100 data members is as well not realistic – Rahul Feb 27 '19 at 06:41
  • @PrasadTelkikar yes and there is no need since there can't be any unless using any ORM framework – Rahul Feb 27 '19 at 06:44
0

You have a number of issues in your code.

First, EmployeeId has no business being part of your create model. Let the database generate the id for you. I suspect you use Employee_Master_Model for both create and update. If that is the case I would suggest you stop doing that, create a separate model for create and remove the properties you don't need.

Second, do not return a DataTable from your API. A common approach is to return the generated id of the created entity and nothing more. If you really need to get all the data then you can create a GET endpoint, pass it the id you just received and that will give you the data.

Third, since you are using WebAPi, learn to use models and learn to use DTOs ( Data Transfer Objects ). Those are commonly used for returning data. Datatables are part pf a database implementation, you don't want that exposed via the API.

Andrei Dragotoniu
  • 6,155
  • 3
  • 18
  • 32