title: Connect to SQL DB via Textbox
body: <p>So I am trying to use a connection string that is pasted into a textbox to connect to a db when the connect button is clicked. I have the following code:</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.SqlClient;
  
  namespace SQLTool
  {
      public partial class SQLTool : Form
      {
          public SQLTool()
          {
              InitializeComponent();
  
          }
          public static SqlConnection myConnection = null;
  
          public void Connection()
          {
              myConnection = new SqlConnection(DBConnectionBox.Text);
              //myConnection.Open();
          }
  
  
          private void DBConnectBtn_Click(object sender, EventArgs e)
          {
              SqlConnection myConnection = new SqlConnection(DBConnectionBox.Text.ToString());
              myConnection.ConnectionString = "Data Source=ServerName;" + "Initial Catalog=DatabaseName;" + "User ID=UserName;" + "Password=Password";
              myConnection.Open();
  
              if (myConnection !=null &amp;&amp; myConnection.State == ConnectionState.Closed)
              {
                  MessageBox.Show("SUCCESS!!");
              }
  
  
          }
  
  
  
  
      }
  }
  </code></pre>
  
  <p>I have added a picture of my form. The combobox I will be adding data from the db I connect to. So any help on that would be appreciated as well.
  <a href="http://i.stack.imgur.com/hSrEi.png" rel="nofollow">Form Picture</a></p>
  
  <p>Maybe someone can point me into the right direction. The if statement was just for testing if I was connecting to the db. This is my first time trying something like this so I am a bit lost. Any help would be greatly appreciated.</p>
  
  <p>UPDATE:</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.SqlClient;
  
  namespace SQLTool
  {
      public partial class SQLTool : Form
      {
          public SQLTool()
          {
              InitializeComponent();
  
          }
          public static SqlConnection myConnection = null;
  
  
  
  
          private void DBConnectBtn_Click(object sender, EventArgs e)
          {
  
              try
              {
                  var myConnection = new SqlConnection(DBConnectionBox.Text);
                  myConnection.Open();
                  if (myConnection.State == ConnectionState.Open)
                  {
                      var sqlCmd = new SqlCommand("SELECT * FROM Menu", myConnection);
                      var sqlReader = sqlCmd.ExecuteReader();
  
                      while (sqlReader.Read())
                          ClientComboBox.Items.Add(sqlReader["Name"].ToString());
  
                      //MessageBox.Show("SUCCESS!!");
  
  
                  }
              }
              catch (Exception ex)
              {
                  MessageBox.Show($"Failed to connect. Message {ex.Message}");
              }
  
  
          }
  
  
      }
  }
  </code></pre>
  
  <p>The only thing left to do is to populate the datagridview with a SQL Query that is ran depending on what I pick in the combobox.</p>
  
  <p>Ok guys, new problem:</p>
  
  <pre><code>private void ClientComboBox_SelectedIndexChanged(object sender, EventArgs e)
      {
  
          try
          {
              var myConnection = new SqlConnection(DBConnectionBox.Text);
              myConnection.Open();
              if  (myConnection.State == ConnectionState.Open)
              {
                  var Cmd = new SqlCommand("SELECT * FROM Menu WHERE Name ='" + ClientComboBox.Text + "';");
                  var Reader = Cmd.ExecuteReader();
  
                  while(Reader.Read())
                  {
                      SqlDataAdapter sqlDataAdap = new SqlDataAdapter(Cmd);
  
                      DataSet dtRecord = new DataSet();
                      sqlDataAdap.Fill(dtRecord);
                      ClientInfoDGV.DataSource = dtRecord;
  
                  }
              }
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.ToString());
          }
      }
  </code></pre>
  
  <p>When I click on a combobox item something is populated in the datagridview (ClientInfoDGV). It goes straight to the catch.</p>