Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 ?
C#
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 
            }
}
Posted
Updated 30-Aug-19 17:31pm
v2

1 solution

A better approach would be to have a separate class with properties for Branch and make it child to the Users. Maybe something like following-

C#
public class Branch{
   public string BranchId { get; set; }
   public string BranchName { get; set;}
}

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 IList<Branch> Branches {get;set;}
   }

And to convert your datatable to IList of Branch, you can try doing something like following-
C#
var branches = from yourDataTable in YourDataTable.AsEnumerable()
                select new Branches
                {
                    BranchId = Convert.ToString(yourDataTable["BranchId"]),
                    BranchName = Convert.ToString(yourDataTable["BranchName"])
                };


Please let me know if this doesn't work.

Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900