Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
problem
how to return user id and branches datatble from this function by using csharp ?

so that i need to return multiple data from function but i cannot know how ?
data i need to return is in case of IsUserValid return
<pre>UserId,dtGetBranches

and if is invalid
string.Format("Invalid User Name {0} Or Password {1}",UserId,Password);


and in case of no valid return

and if

What I have tried:

public string PostUserLogin(string UserId, string Password)
       {
           DataAccess.CurrentDataBase = DataAccess.Credentials.SecurityDB;
           //================
           if (!string.IsNullOrEmpty(UserId))
           {
               DbParameter[] parameters = UserManager.CreateLoginParameters(UserId,Password);
               //:: Check For PassWord Validation days
               DataTable dt = DataAccess.ExecuteDataTable(SqlFactory.Queries.Userss_ValidatePasswordDays(), parameters);
               DataRow PassWordDays;
               if (dt.Rows.Count > 0) PassWordDays = dt.Rows[0];
               else PassWordDays = null;


               if (PassWordDays != null)
               {
                   if (!string.IsNullOrEmpty(PassWordDays["PasswordDate"].ToString()) && !string.IsNullOrEmpty(PassWordDays["PWDays"].ToString()))
                   {
                       if (Convert.ToInt32(PassWordDays["PWDays"].ToString()) > 0)
                       {
                           if (Convert.ToDateTime(PassWordDays["PasswordDate"].ToString()).AddDays(Convert.ToDouble(PassWordDays["PWDays"].ToString())).ToOADate() < DateTime.Now.ToOADate())
                           {


                           }
                       }
                   }
               }

               //==============
               bool IsUserValid = UserManager.IsValidUser(UserId, Password);
               if (IsUserValid)
               {
                   DataAccess.Credentials.SystemUserID = UserId;
                   DataTable dtGetBranches = DataAccess.ExecuteDataTable(
                           SqlFactory.Queries.Users_GetBranches(
                                                       DataAccess.Credentials.SystemUserID));
               }
               else
               {
                   string.Format("Invalid User Name {0} Or Password {1}",UserId,Password);
               }

           }
       }
Posted
Updated 28-Aug-19 19:47pm

1 solution

The main problem is that you want to return two dissimilar types, which would mean that your function would have to return an object - the highest common class between the two.
And that makes your function very difficult to use, because each time you call it, you have to check what type it returned, and cast it to the appropriate type before you can report an error or use it. That's bad design.

Instead, give the method out parameters:
public bool PostUserLogin(string UserId, string Password, out DataTable results, out string errorMessage)
Then return a bool which is true if it worked, and false if it failed.
The "outside world" can then use the appropriate parameter and continue without problems.
 
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