title: Retrieving data from a Datatable in C#
body: <p>I have a data able that has the three columns: Username Password and Name.
  I have a login in screen that checks that checks the username and password to allow for access or not. I am trying to display the Name of the user onto another Form, but what I have is not working. </p>
  
  <p>The program runs until i press login and then an IndexOutOfRangeException is shown. I feel like I am not calling the correct cell that the data is located in, but I can not figure it out. I am very new to data tables.</p>
  
  <p>Login Form:</p>
  
  <pre><code>namespace Inventory_Program
  {
      public partial class Login : Form
      {
          public string name;
  
          public Login()
          {
              InitializeComponent();
          }
  
  
          /*
           * Used when user accepts to login. username and password must be correct or error message will display
           * Using a table in sql form. 
           */
          private void loginButton_Click(object sender, EventArgs e)
          {
              SqlConnection connection = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = C:\Users\brand\Documents\Data.mdf; Integrated Security = True;");
              SqlDataAdapter adapter = new SqlDataAdapter("Select Count(*) From Login where Username='" + usernameTextfield.Text + "' and Password = '" + passwordTextfield.Text + "'", connection);
              DataTable dt = new DataTable();
              adapter.Fill(dt);
              if (dt.Rows[0][0].ToString() == "1")
              {
  
                  name = dt.Rows[0][3].ToString();
                  this.Hide();
                  MainGUIPanel mainview = new MainGUIPanel();
                  mainview.Show();  
  
              }
              else
              {
                  MessageBox.Show("Username or Password Incorrect! Try Again!");
              }
  
          } //end of login button
  
  
          //can canel out of login in screen, closes window.
          private void cancelButton_Click(object sender, EventArgs e)
          {
              this.Close();
          } //end of cancel button
  
  
          public string getName()
          {
              return name;
          }
      }
  }
  </code></pre>
  
  <p>MainGuiView:</p>
  
  <pre><code>    namespace Inventory_Program
  {
      public partial class MainGUIPanel : Form
      {
  
          Login login = new Login();
  
          public MainGUIPanel()
          {
              InitializeComponent();
  
              //runs the current time and data
              currentTime.Start();
  
          }
  
          //Method is adding a horizontal line to the top panel
          private void topControlPanel_Paint(object sender, PaintEventArgs e)
          {
  
              Graphics graphics = e.Graphics;
              Pen pen = new Pen(Color.Black, 1);
              graphics.DrawLine(pen, 1091, 93, 00, 93);
              graphics.Dispose();
  
              nameLabel.Text = login.getName();
  
          }
  
          //allows for the current time and date to be displayed in the top panel
          private void currentTime_Tick(object sender, EventArgs e)
          {
              DateTime dateTime = DateTime.Now;
  
          }
  
          private void inventoryButton_Click(object sender, EventArgs e)
          {
  
          }
      }
  }
  </code></pre>