Cannot add constraint to DataTable which is a child table in two nested relations - c#-2.0

void ReadXMLData()
{
string filePath = #"D:\XMLFiles\cms.xml";
DataSet ds = new DataSet();
ds.EnforceConstraints = false;
ds.ReadXml(filePath);
}
When I read the above xml file I got the error: Cannot add constraint to DataTable which is a child table in two nested relations.
Kindly help me that how can I overcome the mentioned DataSet exception?
Thanks in advance.

do you have any control over the XML formatting? I believe XML where child nodes have the same names as ancestor nodes are not fully supported. Would that be your case?
Edit: this might be helpfull: http://www.codeproject.com/KB/cpp/dataset.aspx. (MC++, but the same principles appy).

xml data is loaded into VS.NET 2005 xml editor
right clicking "view data grid" yields
"Cannot add constraint to datatable 'xxx' which is a child table in two nested relations"
HOWEVER if you move element before and , or if you delete or
right clicking "view data grid" properly displays the data grid. Note that and are indentical.

Related

Creating and Editing related entities from a ASP.NET MVC web form

I'm creating a ASP.NET MVC 4 web app with a database and Entity Framework 5 for a web form to maintain Document (aka Contract) entries.
Below is a sample ERD (in the form of edmx diagram with navigational properties) I have created for a DB in SQL server. For MVC web app the Model is generated from the DB with .edmx file.
Based on the Model, I have setup a form which can create and edit a Document like so:
Document Create and Edit View
This view refers to Document model class. So all the input fields using html helper (Html.TextBoxFor...,etc) are referred from Document object and its related objects (Section 1 & Section 2). The checkbox hides and shows the subform for a section with JS/jQuery. For the Edit view I use same as Create view, except it contains additional hidden ID fields to identity the records to edit from the DB.
I have added a variable length list for section item addition by following this:
http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/
In the partial view for the items dynamic list I use Section1Item and Section2Item as model reference.
Document Controller's Create method
So on Create the document object posted in Create(...) method contains all the attributes from the Document including the section's attributes.
If the checkbox value for Section 1 & Section 2 are false then I set the Document's Section 1 & Section 2 to null like so:
if (section1_chkbox == false) {
document.Section1 = null;
}
I have to add each item from the dynamic list into the document object like so:
foreach(Section1Item item in itemsList) {
document.Section1.Section1Items.Add(item);
}
So finally the Create method in the DocumentController adds the document in a db context and saves changes. The document and its related entities are successfully added to database tables.
Document Controller's Edit method
But when I'm editing the document with the Edit method in DocumentController there are some inconsistencies with some section1 object's ID being null for some related objects in Section1Items. For example when Section1Items are present the posted Document object (for edit) does not contain reference to Section 1 object's ID.
So it is fiddly and messy to update an existing document record as I'm having to check the ID attributes of a object. There are always errors like inconsistent principal and dependent objects in the relationship when saving or setting the object's entry state to modified.
My Question
How can I setup the View and Controller to achieve a functioning Create and Edit of a document along with its related entities as in the form's interface able? I need a efficient and correct way to use Entity Framework for this. How can I use view models for this if that would make this easier?
From what you described here I understand that there is a problem with virtual reference to Section1 from Section1Items. So in other words the problem is that entity framework model doesn't suit you display needs. You would need to render duplicated entities to map entier Document in your view. Am I right?
If that's the case I see two solutions:
like you suggested use view models to solve the problem. If you confirm the issue I can try to write example view model.
serialzie/encrypt entier Document in your view and next merge edited result with encrypted Document. You can achive that by using Mvc3Futures feature.
Serialize and encrypt your document in view:
#Html.Serialize("Document", Model, SerializationMode.EncryptedAndSigned)
And to deserialzie and decrypt you can use:
public ActionResult Edit(string id,[Deserialize(SerializationMode.EncryptedAndSigned)]Document document)

SelectItem with property multiple select

I´m using the SelectItem component with configuration:
private SelectItem nElementsCombo;
nElementsCombo = new SelectItem();
nElementsCombo.setMultiple(true);
nElementsCombo.setMultipleValueSeparator("|");
In the combo the elements selected are shown item_selected_1|item_selected_2|item_selected_3
but when I do:
nElementsCombo.getValueAsString()
Return item_selected_1,item_selected_2,item_selected_3 and I´d like item_selected_1|item_selected_2|item_selected_3
How can I solve this?
from the javadoc :If this item is displaying multiple values, this property will be the string that separates those values for display purposes. Display purpose
I don't catch it, can you replace in your returned string the comma by the pipe ..... !!!
As per Alain's answer, MultipleValueSeparator is only for display purpose.
Means, when you select multiple values from picklist & then when the picklist is hidden on blur (focus lost) of multi select item, the selected values are displayed as a string separated by comma(default). This display can be changed by MultipleValueSeparator. But not the one you get by multiSelectItem.getValueAsString().
Also I don't think, there's any provision in SmartGWT API which fulfills your requirement, as of now.

EF Persisting Columns Ignore those that are not in view

I have mvc view that has some columns from entity. When I do db.SaveChanges(), all columns that are not part of the view are being updated with NULL, so overwriting any values that were present in database record. That's very lame.
I am aware that I could do ModelView for the the view and bind just those columns that i want. But I am looking for a way to simply tell EF, to 'ignore' columns during this particular update, to NOT update columns that are NOT present in the MvC View.
I am using EF 5. Any suggestions?
I don't know why you want to avoid creating a separate Model but i guess you know what you are doing so maybe try this approach to trick EF into thinking the properties have not changed:
var entry = context.Entry(obj);
entry.Property(name).IsModified = false;
I haven't tried it myself but it should be possible in EF 5. If it doesn't work try accessing the property entry by searching the entry.CurrentValues.PropertyNames collection and then setting the IsModified to false.
I think what i needed is this
User u = db.Users.Find(user.UserID);
if (u!=null) {
TryUpdateModel(u);
if (ModelState.IsValid)
{
db.SaveChanges();
}

Creating Drop down list for a column which is defined as an Int in the SQL database - jqGrid ASP.NET MVC

I am using Entity Framework Database First approach in ASP.NET MVC.
I have created models from an existing database and everything works fine.
I am using jqGrid and trying to create a drop down list for a column which is defined as an Int in the database.
In the database, the values are either 0 or 1, so in the dropdown list I will have to show two values such as "Import" or "Export" based on whether it is 0 or 1.
Would I be able to handle this scenario in the jqGrid?
Please post any suggestions if you have!
I think you can handle this thing in colModal itself. there's is one option editOption you can specify with you colModal and based on the value you are getting from data you can specfy the text property of that column
In this case, the data for the grid should contain “One” or “Two” to be set in the column myname.
Now, with this setting
<script>
jQuery("#grid_id").jqGrid({
...
colModel : [ {name:'myname', edittype:'select', formatter:'select', editoptions:{value:"1:One;2:Two"}} ... ]
...
});
</script>
the data should contain the keys (“1” or “2”), but the value (“One”, or “Two”) will be displayed in the grid.
check this link for further help. You will get your answer. Because you didn't post your code(atleast you should have done that for ur that particular column) i can not say you are implementing the same way.

ASP.NET MVC - Passing Grouped data into a View

I've got a LINQ to SQL object, and I want to group the selected data and then pass it into a view. What's the correct way of doing this? I'm sure I need to group the data when I select it rather than grouping it in the view, as this will result in about 200 rather 50000 rows I need to pass into my view. Are there any good examples of this online that anyone has seen?
Cheers
MH
-----edit-----
I want a bit of both:-
for example, my data object has (amongst others) 2 properties I want to extract, and group on, ItemDetail.ItemID and ItemDetail.Label - it is a set of those I want to pass into my view. My data factory returns a IQueryable which will contain (in live) about 100 records for each ItemID/Label combination, and thus I want to group this in my view so that it only shows 1 row per ItemID/Label combination.
Also, how do I type my View - I have tried passing in something like the var xxx = ...; return View(xxx); but I'm not sure how to strongly type (if I can) the view properly. I can probably boj this and get it working, but I wanted to do this correctly.
----edit 2----
I've just got a bit further on this.
using the var IQueryable itemDetList
itemDetList = itemDetList.OrderBy(i => i.ItemID).GroupBy(i => i.ItemID).Select(i => i.First());
produces a grouped list, with 1 row per ItemID, and preserves the object typing so that I can pass it into a strongly-typed view - is that the correct way of manipulating the data? How can I put another layer of grouping so that it groups by .Label within each .ItemID group?
You may want to abstract the model you pass to your view from the LINQ 2 SQL objects; check out the View Model Pattern. If this means you find yourself potentially writing lots of code to map properties from LINQ 2 SQL objects to your View Model objects then consider using AutoMapper.
Well, then group the data and pass it onto your View from your Controller...
public ViewResult Foo()
{
var data = this.GetGroupedData();
return this.View(data);
}
private IEnumerable<Bar> GetGroupedData()
{
return from x in GetData()
group x by x.Baz into g
select new Bar(g.Key);
}
I would define Presentation-Models that represent your groups in your View. Fill the Presentation-Models with LINQ and pass them to your View.
With Presentation-Models you have strongly typed data to display in your view.

Resources