title: Populating DataGridView In C# DataTable Gives An Error
body: <p>I am trying to populate a Data GridView in C# using SQL Server.</p>
  
  <p>The database connection to the SQL Server is inserted using App.config file.</p>
  
  <p>The Data Connection is correct and I have tried to insert data to the SQL Database Table and it worked perfectly.</p>
  
  <p>Please refer the below code snippets.</p>
  
  <p><strong><em>App.config</em></strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
     &lt;configuration&gt;
        &lt;startup&gt; 
          &lt;supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /&gt;
        &lt;/startup&gt;
        &lt;connectionStrings&gt;
           &lt;add name="MyDbConnectionString" providerName="System.Data.SqlClient" connectionString="Data Source=CHIRANTHAKA-PC;Initial Catalog=MyDatabase;Integrated Security=True" /&gt;
       &lt;/connectionStrings&gt;
    &lt;/configuration&gt;
  </code></pre>
  
  <p>The data will be inserted using <strong><em>InsertInfo Form</em></strong>
  Please refer the below code snippets for <strong><em>variable declaration</em></strong>.</p>
  
  <pre><code>public partial class InsertInfo : Form
  {
      private SqlConnection SqlConn;
      private SqlDataReader SqlRead;
      private SqlCommand SqlComm;
      private DataTable SqlDataTable;
      private SqlDataAdapter SqlAdapt;
      private DataSet DsMyData;
      private string strQuery;
  
      private string strDataId;
      private string strDataDesc;
      private DateTime dtmDataDate;
      private int intDataQty;
  
      private int intUserExist;
  
      public InsertInfo()
      {
          InitializeComponent();
      }
  </code></pre>
  
  <p>The database connection is made in the load(). This works perfectly. Please refer the below code snippet.</p>
  
  <pre><code>    private void InsertInfo_Load(object sender, EventArgs e)
      {
  
          ConnectionStringSettings ConnStrSett = ConfigurationManager.ConnectionStrings["MyDbConnectionString"];
          string ConnString = ConnStrSett.ConnectionString;
  
          try
          {
              SqlConn = new SqlConnection(ConnString);
              SqlConn.Open();
              MessageBox.Show("Database connected!", "DB Connection With App.Config", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
      }
  </code></pre>
  
  <p>My issue is with populating the <strong><em>Datagridview</em></strong>.
  Please refer the below code snippet.</p>
  
  <pre><code> private void btnShowData_Click(object sender, EventArgs e)
      {  
              try
              {
                  SqlComm.CommandText = "SELECT * FROM MyDataTable";
                  SqlDataTable = new DataTable();
                  SqlAdapt.Fill(SqlDataTable);
                  dgvInsertInfo.DataSource = SqlDataTable;
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.Message);
              }   
      }
  </code></pre>
  
  <p>I got the below error when I click the btnShowData button.
  <a href="https://i.stack.imgur.com/dnnLN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dnnLN.png" alt="enter image description here"></a></p>
  
  <p>I can't understand what I have done wrong?</p>