I'm using the following code to add a property to a table.
var prj = PlantApplication.CurrentProject.ProjectParts["Piping"]; PnPDatabaseMode prevMode = prj.Mode; prj.Mode = PnPDatabaseMode.Exclusive; using (PnPDatabase db = PnPDatabase.Open(prj.DataCacheFile)) { PnPTable enginitems = db.Tables["EngineeringItems"]; if (enginitems.Columns.Contains(projectcolum)) return; PnPColumn col = new PnPColumn(projectcolum, typeof(string), PnPDatabase.DefaultStringValueLength); enginitems.Columns.Add(col); db.AcceptChanges(); } prj.Mode = prevMode;
However, I get an error in the Datalinksmanager when I try to set the PnPDatabaseMode to Exclusive. Any ideas?
The PnPDatabaseMode was shared, but the error says it's already opened exclusive.
The database is already opened in the project so trying to go exclusive on another instance of PnPDatabase will always fail.
Try changing the code to this:
[CommandMethod("PNPADDCOL", CommandFlags.Modal)]
public static void PnPAddCol()
{
var prj = PlantApplication.CurrentProject.ProjectParts["Piping"];
PnPDatabaseMode prevMode = prj.Mode;
prj.Mode = PnPDatabaseMode.Exclusive;
// commented out wrong code
var db = prj.DataLinksManager.GetPnPDatabase();
//using (PnPDatabase db = PnPDatabase.Open(prj.DataCacheFile))
//{
PnPTable enginitems = db.Tables["EngineeringItems"];
if (enginitems.Columns.Contains("TestNewCol"))
return;
PnPColumn col = new PnPColumn("TestNewCol", typeof(string), PnPDatabase.DefaultStringValueLength);
enginitems.Columns.Add(col);
db.AcceptChanges();
//}
prj.Mode = prevMode;
}