c# how to return a query result from inside a method


 i have a Access database that contains username and password.  User is ask to enter username and password to log in. i passed those 2 strings to a a method that will verify if user exist in the datbase.

My problem. how do you return the query result back to main so i can use it for other queries?
I also have a condition there to see if it does exist display name , etc.


static void Main(string[] args)
{

     //Login USer
            Console.WriteLine(String.Format("{0," + Console.WindowWidth / 2 + "}", "Press Log In "));
            Console.WriteLine(Environment.NewLine);
            Console.WriteLine(Environment.NewLine);
            Console.Write(String.Format("{0," + Console.WindowWidth / 2 + "}", "UserName: "));
            string UsName = Console.ReadLine();
            Console.Write(String.Format("{0," + Console.WindowWidth / 2 + "}", "Password: "));
            string PassWd = Console.ReadLine();
            
 string name = Login.LognCus(UsName,PassWd);  //Cannot be a string?
}



//Method 

public  static LognCus(string a, string b)
      {
          string UsName = a;
          string PassWd = b;

          OleDbCommand cmd = DbUtils.GetCommand();
          cmd.CommandType =CommandType.Text;
          cmd.CommandText="Select * FROM [Atm_Customers] WHERE UserName='" + UsName + "' AND Password='" + PassWd + "'";
          

          OleDbDataReader rdr = null;
          rdr=cmd.ExecuteReader();

//Condition
          if (rdr.Read() == true)
          {

                
              // i want to return this if its true back to main..
     
          }
          else
          {
              Console.WriteLine("UserName does not exist");
          }
          
        cmd.Connection.Close();

Open in new window

Ajs135Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

edemcsCommented:
Change your method into a function and pass it back as your choice: datatable, dataview, custom object, etc.
0
nmarunCommented:
Try something like this:

Arun
public static string LognCus(string a, string b)
      {
          string UsName = a;
          string PassWd = b;

          OleDbCommand cmd = DbUtils.GetCommand();
          cmd.CommandType =CommandType.Text;
          cmd.CommandText="Select * FROM [Atm_Customers] WHERE UserName='" + UsName + "' AND Password='" + PassWd + "'";
          
          OleDbDataReader rdr = null;
          rdr=cmd.ExecuteReader();

//Condition
          string name = string.Empty;
          if (rdr.Read() == true)
          {
              // replace the string "Name" with
              // appropriate column name for the user's name
              name = rdr["Name"];
          }
          else
          {
              Console.WriteLine("UserName does not exist");
          }
          
        cmd.Connection.Close();
        return name;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nmarunCommented:
Once you've done that, you might want to place a check in the Main method as follows.

Arun
string name = Login.LognCus(UsName,PassWd);  //Cannot be a string?
if(!string.IsNullOrEmpty(name))
{
    Console.WriteLine(name);
}

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.