6 Replies - 691 Views - Last Post: 31 August 2019 - 08:49 AM Rate Topic: -----

#1 ahmedba   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 220
  • Joined: 24-January 14

What data type can receive data table inside class ?

Posted 30 August 2019 - 06:10 PM

Problem
what datatype can receive data table inside class as branchid and branchname

and how to receive ?

I have class Users for login as following :

public class Users
   {
       public string UserName { get; set; }
       public string Password { get; set; }
       public string MessageStatus { get; set; }
       public int StatusCode { get; set; }
       public string StatusText { get; set; }
       public string IpAddress { get; set; }
       public string BrowserInfo { get; set; }
       public ??? branches_datatable {get;set;}
   }


i need to get property from class can have the values of branches datatable but i dont know how to
write datatype inside class ?

and how to receive data table ?
datatable branches have the following :

branchid  branchname
1         englandbranch
2         francebranch



What I have tried:

public JsonResult PostUserLogins(Users user)
        {
            int LoginStatus = _AuthunticateService.PostUserLogin(user.UserName,user.Password ,out DataTable Branches,out string errorMessage, out int statusCode);
           
           if(LoginStatus == 1)
            {
                user.StatusCode = statusCode;
                user.StatusText = "failed";
                user.MessageStatus = "login failed, incorrect username or password";
Datatable dtGetBranches = DataAccess.ExecuteDataTable(
                           SqlFactory.Queries.Users_GetBranches(
                                                       DataAccess.Credentials.SystemUserID));
user.???? branches_datatable=dtGetBranches 
// how to assign datatable to property of class here and what property for class datatable 
            }
}

This post has been edited by ahmedba: 30 August 2019 - 06:11 PM


Is This A Good Question/Topic? 0
  • +

Replies To: What data type can receive data table inside class ?

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15338
  • View blog
  • Posts: 61,467
  • Joined: 12-June 08

Re: What data type can receive data table inside class ?

Posted 30 August 2019 - 06:14 PM

Why not a basic STRUCT that houses an integer and a string?
Was This Post Helpful? 0
  • +
  • -

#3 ahmedba   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 220
  • Joined: 24-January 14

Re: What data type can receive data table inside class ?

Posted 30 August 2019 - 06:19 PM

you mean that declare struct to receive data from data table
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15338
  • View blog
  • Posts: 61,467
  • Joined: 12-June 08

Re: What data type can receive data table inside class ?

Posted 30 August 2019 - 06:26 PM

Sure.. or a small class or even just a datatable itself.
Was This Post Helpful? 0
  • +
  • -

#5 ahmedba   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 220
  • Joined: 24-January 14

Re: What data type can receive data table inside class ?

Posted 30 August 2019 - 06:36 PM

if i need to use it as dictionary so that how to make it
user.?????=how to get data from datatable and assign to dictionary
Was This Post Helpful? 0
  • +
  • -

#6 ahmedba   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 220
  • Joined: 24-January 14

Re: What data type can receive data table inside class ?

Posted 30 August 2019 - 07:36 PM

OK thank you very much
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7109
  • View blog
  • Posts: 24,146
  • Joined: 05-May 12

Re: What data type can receive data table inside class ?

Posted 31 August 2019 - 08:49 AM

I recommend a simple list of POCO's:

class Branch
{
    public int Id      { get; set; }
    public string Name { get; set; }
}

class Users
{
    :
    IList<Branch> Branches { get; set; }
}



I don't recommend a DataTable just because it makes it a little bit harder to reason with the code -- you need to know what the column names are and guarantee that they exist. It's fine when you are sitting adjacent to the code or just one layer above, but imagine that Users class being passed up a couple of layers and or as a pass-through dependency for a view model further away than you cousin through your aunt twice removed. Intellisense nor Peek Declaration won't help you.

A few asides:
  • In most cases, names should be singular, not plural.
  • The .NET Framework has an IpAddress class and HttpStatusCode enum.
  • In an object oriented world, you shouldn't have any need for an "id" field or column. The "id" is only of use to a relational database. If you have objects why would you need to keep track of ids? The object reference is the id.


Given those asides above, then all you really need would be:
public class User
{
    public string UserName           { get; set; }
    public string Password           { get; set; }
    public string MessageStatus      { get; set; }
    public HttpStatusCode StatusCode { get; set; }
    public string StatusText         { get; set; }
    public IpAddress IpAddress       { get; set; }
    public string BrowserInfo        { get; set; }
    public IList<string> Branches    { get; set; }
}



As another aside, as an outsider looking in, it's kind of confusing to see MessageStatus, StatusCode, and StatusText. What is each one used for?

There is also a code smell present there: It looks like the same POCO is used for both input and output. The user name and password is used for input, but later the other fields get filled in. It may make sense to use two different classes.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1