none
System.Data.DataTable reuse RRS feed

  • Question

  •  

    Hi,

    the following only creates a table once, but I would like to create the same table often

     


    Code Snippet
    public DataTable MatrixColumns
    {
     get
     {
      //objDS.Tables.Clear("MATRIX_COLUMNS");
      //objDS.Tables["MATRIX_COLUMNS"].Clear();
      //objDS.Tables["MATRIX_COLUMNS"].Reset();
      //objDS.Tables.Remove("MATRIX_COLUMNS");
      if(objDS.Tables["MATRIX_COLUMNS"] == null)
      {    
       FmLogWriter.Instance.WriteLine(this.GetType().Name, "PopulateTables", "Populating MATRIX_COLUMNS table");
       PerformOnDemandLoad("MATRIX_COLUMNS");
       keys1[0] = objDS.Tables["MATRIX_COLUMNS"].Columns[0];
       objDS.Tables["MATRIX_COLUMNS"].PrimaryKey = keys1;
      }
      return objDS.Tables["MATRIX_COLUMNS"];
     }
    }

     

     

     

    is there a way of emptying the temporary space so that I can refill it with fresh data?

     


    tia,

    Tuesday, September 25, 2007 1:32 PM

All replies

  • I dont know what exactly your code is trying to do, but you can create the table once (store as member variable?) and use dataTable.Copy()

    A new DataTable with the same structure (table schemas and constraints) and data as this DataTable. If these classes have been derived, the copy will also be of the same derived classes. Both the Copy and the Clone methods create a new DataTable with the same structure as the original DataTable. The new DataTable created by the Copy method has the same set of DataRows as the original table, but the new DataTable created by the Clone method does not contain any DataRows.
    Tuesday, September 25, 2007 1:48 PM
  •  

    Hi Tony,

     

    thanks for the reply.

     

    When run the first time the table will have some records selected against a PK value

     

    I want to recreate the table using another PK value, and another etc.,

     

    So I want to flush the current table before sending in the next parameter, but I cannot find the correct method to use, they all give errors like

     

    "Table already exists" or "No table connected with this instance"!

     

    I'm using the table to populate a CheckedListBox via a ComboBox and want the CheckedListBox refreshed from another selection in the combo

     

     

    Smile

    Tuesday, September 25, 2007 5:38 PM
  • I am not familiar with the primary key property. I think you can just create the table with data, create a copy, assign primary key to it and use it each time, including the first time.

     TechNoFear wrote:

     

    Hi Tony,

     

    thanks for the reply.

     

    When run the first time the table will have some records selected against a PK value

     

    I want to recreate the table using another PK value, and another etc.,

     

    So I want to flush the current table before sending in the next parameter, but I cannot find the correct method to use, they all give errors like

     

    "Table already exists" or "No table connected with this instance"!

     

    I'm using the table to populate a CheckedListBox via a ComboBox and want the CheckedListBox refreshed from another selection in the combo

     

     

    Tuesday, September 25, 2007 7:21 PM

  • I am Sorry Tony I'm not explaining myself very well.

     

    The primary key is in the Oracle table not the C# table.

     

    I'm populating a DataTable with a select query which does not reflect an entire db table, only a subset.

     

    I want to requery the DB table and return another recordset and replace the C# DataTable with the new recordset, but VS doesn't seem to work that way.

     

    Code Block

      // combobox click event to fill a checkedlistbox against whats showing
      private void columnComboBox_SelectionChangeCommitted(object sender, System.EventArgs e)
      {
       ComboBox matrixID = (ComboBox)sender;
       if (((System.Data.DataRowView)(matrixID.SelectedItem)).Row.ItemArray[0].ToString() == "")
       {
        return;
       }
       int categoryID = Convert.ToInt32((((System.Data.DataRowView)(matrixID.SelectedItem)).Row.ItemArray[0]));
       LoadCheckBox(categoryID);
       Application.DoEvents();
      }
      // use the return values from the C# table to populate the list
      private void LoadCheckBox(int categoryID)
      {
       DTGetCategories = StaticData.Instance.GetCategories(categoryID);
       foreach (DataRow dr in DTGetCategories.Rows)
       {
        // 3 = description
        this.columnCheckedListBox.Items.Add(dr[3]);
       }
       if (this.columnCheckedListBox.Items.Count > 0)
       {
        this.columnCheckedListBox.SelectedIndex = 0;
       }
       else
       {
        this.columnCheckedListBox.SelectedIndex = -1;
       }
      }
      // create the table and call the sql
      public DataTable GetCategories(int categoryID)
      {
       DataTable tbl = new DataTable();
       DataRow[] rows;
       globalParameter = categoryID.ToString();
       rows = CategoryRecords.Select("category_id = '" + categoryID +"'", "category_description");
       tbl = CategoryRecords.Clone();
       foreach (DataRow row in rows)
       {
        tbl.ImportRow(row);
       }
       return tbl;
      }
      // fill the table with this query
      public DataTable CategoryRecords
      {
       get
       {
        if(objDS.Tables["GL_MATRIX_CATEGORIES"] == null)
        {    
         FmLogWriter.Instance.WriteLine(this.GetType().Name, "PopulateTables", "Populating GL_MATRIX_CATEGORIES table");
         PerformOnDemandLoad("GL_MATRIX_CATEGORIES");
         keys1[0] = objDS.Tables["GL_MATRIX_CATEGORIES"].Columns[0];
         objDS.Tables["GL_MATRIX_CATEGORIES"].PrimaryKey = keys1;
        }
        return objDS.Tables["GL_MATRIX_CATEGORIES"];
       }
      }

     

     


    I'll think of something else, thanks

    Thursday, September 27, 2007 3:33 PM