NewPassionnate NewPassionnate - 1 month ago 10
C# Question

if loop can't break

I am working with dao, My user is adding a table and its field. My programs checks if the table exists, if yes, it is adding only the field, if the table doesn't exist it is adding the table and then appends the field.

My problem is happening when I am trying to add the second field. It is telling me that the table already exists. It is not going through the loop the proper way. can you help?

string tablename = txtNameTable.Text.Trim();
string fieldname = txtFieldName.Text.Trim();
string size = txtSize.Text.Trim();

//create a table
myTable = clsDataSource.mydb.CreateTableDef(tablename);

//create fields
if (cboFieldType.SelectedItem.ToString() == "Text")
{
myField = myTable.CreateField(fieldname, DAO.DataTypeEnum.dbText, size);
}


if (cboFieldType.SelectedItem.ToString() == "AutoNumber")
{
myField = myTable.CreateField(fieldname, DAO.DataTypeEnum.dbLong);
myField.Attributes = (int)DAO.FieldAttributeEnum.dbAutoIncrField;
}


if (cboFieldType.SelectedItem.ToString() == "Number")
{
myField = myTable.CreateField(fieldname, DAO.DataTypeEnum.dbLong, size);
}


if (cboFieldType.SelectedItem.ToString() == "Date")
{
myField = myTable.CreateField(fieldname, DAO.DataTypeEnum.dbDate);
}


//check if table exists
bool tableExist = false;

foreach (TableDef tb in clsDataSource.mydb.TableDefs)
{
if (tb.Name == myTable.Name)
{
myTable.Fields.Append(myField);
tableExist = true;
break;
}
}
if (tableExist == false)
{
myTable.Fields.Append(myField);
clsDataSource.mydb.TableDefs.Append(myTable);
}



//add primary key

if (ckPK.Checked == true)
{
Index myIndex = myTable.CreateIndex("PrimaryKey");
myField = myIndex.CreateField(fieldname);
((IndexFields)myIndex.Fields).Append(myField);
myIndex.Primary = true;

try
{
clsDataSource.mydb.TableDefs[myTable.Name].Indexes.Append(myIndex);
}
catch (Exception)
{
MessageBox.Show("This table already has a primary key", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Activate(true, true, true);
}

Answer

Okay, here is the thing. If you have multiple tables in your foreach statement, then your if statement with checking if Findtable is false being reached only after all tables are iterated. Hence, you have to move your if statement inside the loop, so that it could add a new table in case if it does not exist. Practically, this is how it should look like:

    List<String> existingTables = new List<String>();
    foreach (TableDef tb in clsDataSource.mydb.TableDefs)
        {
               if (tb.Attributes == 0 && tb.Name == myTable.Name)
                {
                    clsDataSource.mydb.TableDefs[myTable.Name].Fields.Append(myField);
                    myTable.Fields.Append(myField);
                    clsDataSource.mydb.TableDefs.Append(myTable);
                }         
                else {       
                    existingTables.Add(MyTable.Name.ToString());
                }      
        }//end foreach
    foreach (var el in existingTables){
           MessageBox.Show("This table already exists: {0}", el)
    }//end foreach

Thus, you get rid of another condition and add your table right away if it matches all parameters. Moreover, you also get rid of that boolean, which is unnecessary in this case. Once all tables have been iterated, you will exit the loop.