title: How to display table from MySQL database in Windows Form Textbox
body: <p>I'm creating an application that displays a table of data (id, name, age, castle) when you press a button. I'm not sure how to make it display the table into a textbox after clicking the button.</p>
  
  <p>Here is a display of the Windows Form: <a href="http://puu.sh/lPSi6/204b0d15c0.png" rel="nofollow">http://puu.sh/lPSi6/204b0d15c0.png</a></p>
  
  <p>Once the button "Get People From Selected Castle" is pressed I want it to display the table into the textbox.</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.Threading.Tasks;
  using System.Windows.Forms;
  using System.Data.OleDb;
  using System.Data.Common;
  
  namespace DatabaseApp
  {
      public partial class Form1 : Form
      {
          public Form1()
          {
              InitializeComponent();
          }
  
          private static DbConnection CreateMySQLConnection()
          {
              String connectionString = "id=csuperson;persistsecurityinfo=True;database=westeros;allowuservariables=True";
  
              DbProviderFactory factory = DbProviderFactories.GetFactory("MySql.Data.MySqlClient");
              DbConnection conn = factory.CreateConnection();
              conn.ConnectionString = connectionString;
              return conn;
          }
  
          private static void PrintPeopleByCastle(DbConnection conn, String castleValue)
          {
              DbCommand mycmd = conn.CreateCommand();
              String mySqlStmt = "select* from  person";
  
              if (castleValue != "all") mySqlStmt += " where castle='" + castleValue + "'";
              mycmd.CommandText = mySqlStmt;
              mycmd.CommandType = System.Data.CommandType.Text;
  
              DbDataReader dataReader = mycmd.ExecuteReader();
              Console.WriteLine("\n Executing: " + mySqlStmt);
  
              while (dataReader.Read())
              {
                  Console.WriteLine(dataReader[0].ToString().PadRight(5)
                      + dataReader[1].ToString().PadRight(20)
                      + dataReader[2].ToString().PadRight(5)
                      + dataReader[3].ToString().PadRight(20));
              }
              dataReader.Close();
          }
  
          private static void UpdatePeople (DbConnection conn)
          {
              DbCommand mycmd = conn.CreateCommand();
              mycmd.CommandText = "update person set age = (age+1) where castle = 'WINTERFELL'";
              mycmd.CommandType = System.Data.CommandType.Text;
  
              int recordsAffected = mycmd.ExecuteNonQuery();
              Console.WriteLine("\nRecords affected (see age)... " + recordsAffected);
              Console.WriteLine();
          }
  
          private void getPeopleButton_Click(object sender, EventArgs e)
          {
            try
              {
                  DbConnection conn = CreateMySQLConnection();
                  conn.Open();
                  PrintPeopleByCastle(conn, "all");
                  conn.Close();
              }
              catch (Exception x)
              {
                  Console.WriteLine("ERROR: " + x.Message);
              }
              Console.Read();
          }
  
          private void youngerButton_Click(object sender, EventArgs e)
          {
  
          }
  
          private void olderButton_Click(object sender, EventArgs e)
          {
  
          }
  
          private void textBox1_TextChanged(object sender, EventArgs e)
          {
  
          }
      }
  }
  </code></pre>
  
  <p>My SQL Script is: </p>
  
  <pre><code>CREATE DATABASE westeros;
  
  CREATE TABLE `westeros`.`person` (
   `id` INT NOT NULL,
   `name` VARCHAR(45) NULL,
   `age` VARCHAR(45) NULL,
   `castle` VARCHAR(45) NULL,
   PRIMARY KEY (`id`));
  
  INSERT INTO `westeros`.`person` (`id`, `name`, `age`, `castle`) VALUES ('100', 'Arya
  Stark', '12', 'Winterfell');
  INSERT INTO `westeros`.`person` (`id`, `name`, `age`, `castle`) VALUES ('200',
  'Daenerys Targaryen', '20', 'Dragonstone');
  INSERT INTO `westeros`.`person` (`id`, `name`, `age`, `castle`) VALUES ('300',
  'Cersei Baratheon', '35', 'KingsLanding');
  INSERT INTO `westeros`.`person` (`id`, `name`, `age`, `castle`) VALUES ('400',
  'Tiryon Lannister', '32', 'KingsLanding');
  INSERT INTO `westeros`.`person` (`id`, `name`, `age`, `castle`) VALUES ('500', 'Jon
  Snow', '21', 'Winterfell');
  </code></pre>
  
  <p><a href="http://puu.sh/lPSCc/6034433a44.png" rel="nofollow">http://puu.sh/lPSCc/6034433a44.png</a></p>