Relaxing Constraints for a data table adapter

Maxx picture Maxx · Oct 17, 2011 · Viewed 7.7k times · Source

I'm trying to relax my constraints while using a data table adapter, but I'm getting a Object reference not set to an instance of an object error. It happens on the line that says ds.DataSet.EnforceConstraints = false;

Generally, I would like to know how to keep a dataTable from enforcing constraints.

ubsmysDataSetTableAdapters.FormSaveDataTableAdapter ta = new ubsmysDataSetTableAdapters.FormSaveDataTableAdapter();

myDataSet.FormSaveDataDataTable ds = new myDataSet.FormSaveDataDataTable();

ds.DataSet.EnforceConstraints = false;

if (isAdmin) ds = ta.GetByUserIdForAdminUser(userId);
else ds = ta.GetByUserId(userId);
ds.DataSet.EnforceConstraints = true;

I'm fairly new to this, so any help would be appreciated.

Answer

KreepN picture KreepN · Oct 17, 2011

Try the following, you may have to adjust the text you see in blue to be a perfect match to what yours are, but you should be able to get the idea:

Use the Fill method to get your data:

ubsmysDataSet ds = new ubsmysDataSet();

ubsmysDataSet.FormSaveDataDataTable dt = new ubsmysDataSet.FormSaveDataDataTable();

ds.Tables.Add(dt);

ds.EnforceConstraints = false;

ubsmysDataSetTableAdapters.FormSaveDataTableAdapter ta = new ubsmysDataSetTableAdapters.FormSaveDataTableAdapter();

if (isAdmin)
{

}
else
{
    ta.FillByUserId(dt,130559)
}

ds.EnforceConstraints = true;

See the added line above.