Search code examples
c#ms-accessadox

how to set the AutoIncrement-property of table column in adox?


as i explain in the title, i cant set the property to the column:

Catalog cat = new Catalog();
        Table tableCustomer = new Table();
        Table tableAddresses = new Table();

        try
        {                
            //Create the table Customer and it's fields. 
            tableCustomer.Name = "Customer";
            tableCustomer.Columns.Append("Customer_ID", ADOX.DataTypeEnum.adInteger);
            //column.ParentCatalog = cat;
            //column.Name = "Customer_ID";
            //column.Type = ADOX.DataTypeEnum.adInteger;
            //column.Properties["AutoIncrement"].Value = true;

            //tableCustomer.Columns.Append(column);

            tableCustomer.Keys.Append("PrimaryKEy", KeyTypeEnum.adKeyPrimary, "Customer_ID");
            tableCustomer.Columns["Customer_ID"].Properties["AutoIncrement"].Value = true;
            tableCustomer.Columns.Append("Name", ADOX.DataTypeEnum.adVarWChar, 50);
            tableCustomer.Columns.Append("Email", ADOX.DataTypeEnum.adVarWChar, 50);
            tableCustomer.Columns.Append("TelNumber", ADOX.DataTypeEnum.adVarWChar, 32);
            tableCustomer.Columns.Append("Fax", ADOX.DataTypeEnum.adVarWChar, 32);
            tableCustomer.Columns.Append("AdressCounter", ADOX.DataTypeEnum.adSmallInt);

            tableAddresses.Name = "Addresses";
            tableAddresses.Columns.Append("Address_ID", ADOX.DataTypeEnum.adInteger);
            tableAddresses.Keys.Append("PrimaryKEy", KeyTypeEnum.adKeyPrimary, "Address_ID");
            tableAddresses.Columns.Append("Customer_ID", ADOX.DataTypeEnum.adInteger);
            tableAddresses.Keys.Append("ForeignKey", KeyTypeEnum.adKeyForeign, "Customer_ID", "Customer", "Customer_ID");
            tableAddresses.Columns.Append("Street", ADOX.DataTypeEnum.adVarWChar, 50);
            tableAddresses.Columns.Append("PostalCode", ADOX.DataTypeEnum.adVarWChar, 10);
            tableAddresses.Columns.Append("City", ADOX.DataTypeEnum.adVarWChar, 50);

            cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Application.StartupPath
                + "\\Customers.mdb" + "; Jet OLEDB:Engine Type=5");
            cat.Tables.Append(tableCustomer);
            cat.Tables.Append(tableAddresses);


            //Now Close the database
            ADODB.Connection con = cat.ActiveConnection as ADODB.Connection;
            if (con != null)
                con.Close();


            result = true;
        }
        catch (Exception ex)
        {
            result = false;
        }
        finally
        {
            if (!result)
            {
                ADODB.Connection con = cat.ActiveConnection as ADODB.Connection;
                if (con != null)
                    con.Close();
                File.Delete(Application.StartupPath + "\\Customers.mdb");
            }                 
        }
        cat = null;

i am getting an error, that the object in not found after executing the following line:

tableCustomer.Columns["Customer_ID"].Properties["AutoIncrement"].Value = true;

the error is that the object is not found. but in all solutions in vb, they solve it like i wrote. what am i doing wrong?


Solution

  • You should add the "Customer_ID" column like you did in the commented lines of your code. According to this source, you missed to set the ParentCatalog property for the Column.