2

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.

KreepN
  • 8,528
  • 1
  • 40
  • 58
Maxx
  • 3,925
  • 8
  • 33
  • 38
  • if ds is already a DataSet why do you call ds.DataSet ? do some debug anyway, is ds null or ds.DataSet null? – Davide Piras Oct 17 '11 at 16:33
  • That's a good question. The intellisense is telling me that is where the property for EnforceConstraints is. Let me try it without that. – Maxx Oct 17 '11 at 16:34
  • It seems like ds is not a data set, but a data table... hmm – Maxx Oct 17 '11 at 16:45
  • No, just a table adapter to grab data from a table. Nothing fancy. – Maxx Oct 17 '11 at 16:55
  • Ok, see my heavily edited answer below. Let me know if you follow the code or have any questions. – KreepN Oct 17 '11 at 17:20

2 Answers2

1

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.

KreepN
  • 8,528
  • 1
  • 40
  • 58
  • This looks hopeful. Let me try this. – Maxx Oct 17 '11 at 18:02
  • No, this doesn't work. The constraints are still enforced for dt, even though we turned them off for ds and added dt to ds.Tables – Maxx Oct 17 '11 at 18:13
  • You could just go to the dataset itself and turn off constraints, but I don't know what restrictions you are working around. (Let me see if I can adjust the code if that's not an option). – KreepN Oct 17 '11 at 18:21
  • I tried that too and seemed to have the exact same issues with it. Let me try it again just to see. – Maxx Oct 17 '11 at 18:28
  • If that doesn't work you can try to add the line I just added in my answer, and see if that works. – KreepN Oct 17 '11 at 18:30
  • Same result, unfortunately :( – Maxx Oct 17 '11 at 18:54
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/4337/discussion-between-kreepn-and-maxx) – KreepN Oct 17 '11 at 19:36
0
new myDataSet.FormSaveDataDataTable();

is returning null, thus ds will be null when you try to ds.DataSet.EnforceConstraints = false;.

Something feels wrong here, but since you have not posted all code, we cannot fully evaluate.

Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
  • Wouldn't it be null if it was a new dataset? If I fill it first and try to set EnforceConstraints to false, it tells me that I have a constraints issue. – Maxx Oct 17 '11 at 16:39
  • I'm pointing your **Object reference not set to an instance of an object** error. And a new dataset is different from a null dataset – Adriano Carneiro Oct 17 '11 at 16:41
  • Neither am I. Like I said, you gave us too little. Little is what you get, I guess. We cannot help any further. Maybe if you post the code in `FormSaveDataDataTable`? – Adriano Carneiro Oct 17 '11 at 16:45
  • It's code generated by the table adapter. Nothing out of the ordinary. Does a table adapter initially return null when created? – Maxx Oct 17 '11 at 16:54
  • It seems like what I'm actually trying to set is the data table which is null until it is set by the GetByUserId method. This returns a data table, however, not a dataset. How can I release the constraints on the data set for this datatable? – Maxx Oct 17 '11 at 16:59