• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

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

0
Ajs135
Asked:
Ajs135
  • 2
1 Solution
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now