title: Check if table exists with if statement in C#?
body: <p>I try to put up an if statement to check if a table is already created. I only want to create one table, but as it is now I create a table every time I click the button to store the info. Any suggestions? </p>
  
  <pre><code>    DataTable dt;
  
      private void InitDataTable()
      {
  
          if () { 
  
          }
  
          dt = new DataTable();
          DataSet ds = new DataSet();
          ds.ReadXml("gjesteInfo.xml");
          ds.Tables.Add(dt);
  
          DataColumn dc1 = new DataColumn("Fullt navn");
          DataColumn dc2 = new DataColumn("Start dato");
          DataColumn dc3 = new DataColumn("Antall dager");
  
          dt.Columns.Add(dc1);
          dt.Columns.Add(dc2);
          dt.Columns.Add(dc3);
  
          dt.Rows.Add(gjestenavnInput.Text, datoInnsjekk.Text, antallDager.Text);
  
          ds.Merge(dt);
  
          ds.WriteXml("gjesteInfo.xml");
  
      }
  
  
  
      private void lagre_Click(object sender, EventArgs e)
      {
  
          InitDataTable();
  
          gjesterutenrom.Items.Add(gjestenavnInput.Text);
  
          gjestenavnInput.Text = "";
          datoInnsjekk.Text = "";
          antallDager.Text = "";
  
          DataSet onClick = new DataSet();
          onClick.ReadXml("gjesteInfo.xml");
          lagredeGjester.DataSource = onClick.Tables[0];
  
      }
  </code></pre>
  
  <p>I try to get out the info stored in the XLM with a DataGridView named lagredeGjester as seen over.</p>
  
  <p>UPDATED QUESTION :</p>
  
  <p>Now I wrote the code like this : </p>
  
  <pre><code>    DataTable dt;
  
      DataSet ds = new DataSet();
  
      private void InitDataTable()
      {
  
  
           if( ds.Tables.Contains("Gjester")   )
          {
              dt.Rows.Add(gjestenavnInput.Text, datoInnsjekk.Text, antallDager.Text);
              ds.Merge(dt);
  
              ds.WriteXml("gjesteInfo.xml");
  
          }
          else {
  
              dt = new DataTable("Gjester");
  
              ds.ReadXml("gjesteInfo.xml");
              ds.Tables.Add(dt);
  
              DataColumn dc1 = new DataColumn("Fullt navn");
              DataColumn dc2 = new DataColumn("Start dato");
              DataColumn dc3 = new DataColumn("Antall dager");
  
              dt.Columns.Add(dc1);
              dt.Columns.Add(dc2);
              dt.Columns.Add(dc3);
  
              dt.Rows.Add(gjestenavnInput.Text, datoInnsjekk.Text, antallDager.Text);
              ds.Merge(dt);
  
              ds.WriteXml("gjesteInfo.xml");
  
          }
  
      }
  </code></pre>
  
  <p>On my first run I entered two different infomations and pressed my button. Both info got in the same table as I wanted it to. But I can't seem to write my if statement correctly. When I run the code above it works with an empty XML (with no tables), but as long as "Gjester" table is created it says "A DataTable named 'Gjester' already belongs to this DataSet." But isn't this what my if statement should prevent? As I wrote it now, should it not just add the info and not try to create a new table?</p>