title: C# Windows Forms Return result from OleDb query to MS Access to a custom class
body: <p>I have the design, framework, and most of the code finished for an inhouse application I have created.  I am attempting to add some functionality, which will translate into 3 completely different forms, that access, view, and update data stored on an Access Database, placed on a hidden network drive.</p>
  
  <p>The relevant information for the question I have is as follows...
  1)  User launches application which then collects and displays their current logged in username, and asks them if they wish to procede.
  2)  Once they choose to procede, the code searches an Agent table in the MS Access Database to see if there is a record created for the agent's name.
  3)  If the record exists (currently) the form will close, returning to a data entry form to begin inputing information only displaying their username
  4)  If the record does NOT exist, it open another form, where the user will enter their details, manager, shift start and end time, and their AgentID
  5)  Once the Agent details are saved, then the application returns to the data entry form to begin inputing information.</p>
  
  <p>What I wish to accomplish with this code...
  In step 3), after successfully finding a record that matches their username, return a value from the matching row, in the AgentID column.  The database has a lookup field that matches AgentID to their specific role, which I will use later to show an altername form based on the user's role.  For now, I only wish to get the AgentID from the corresponding row, then save the value to a static class variable I created to be utilized by other existing forms.</p>
  
  <p>The using statements for reference</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  using System.Data.OleDb;
  using System.Security.Principal;
  </code></pre>
  
  <p>The code I want to figure out is within the If function.  The code will successfully identify if the user has an entry in the Agent table, and if not, send the user to a form to create the entry.</p>
  
  <pre><code>private void button1_Click(object sender, EventArgs e)
      {
          // set FNameLabel to user's name without domain
          FNameLabel.Text = WindowsIdentity.GetCurrent().Name.Split('\\')[1];
          // create OleDb connection and command, @name for agent name variable
          OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.jet.oledb.4.0;data source=\\myserver\hidden$\database.mdb");
          OleDbCommand cmd = new OleDbCommand("SELECT * FROM Agent WHERE AgentName=@name");
          // create command connection and set parameters
          cmd.Connection = con;
          cmd.Parameters.AddWithValue("@name", FNameLabel.Text);
          con.Open();
          OleDbDataReader read = cmd.ExecuteReader();
          // If there is a record that matches Agent Name, return to previous form
          if (read.Read() == true)
          {
              // MessageBox.Show("agent name in database");
  
              // If Agent Name matches, return Agent ID, passed to custom class variable
              // Class variable for Agent ID
              // AgentID.Var = [some variable that contains AgentID where AgentName = current user]
              this.Close();
              return;
  
          }
          // If there is no record that matches Agent Name, open form to enter Agent details
          // MessageBox.Show("agent name NOT in database");
          AgentInfo agents = new AgentInfo();
          agents.ShowDialog();
          this.Close();
  
  
  
      }
  </code></pre>
  
  <p>The class variable works without a hitch</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  
  namespace DataEntry
  {
  static class AgentID
  {
      private static string agentid = "";
  
      public static string Var
      {
          get 
          { 
              return agentid; 
          }
          set 
          { 
              agentid = value; 
          }
      }
  }
  </code></pre>
  
  <p>}</p>