LInq to xml help in silverlight application - silverlight

I am working on one silverlight application. I need one help regarding LInq to xml.
It is basically ERP system where objects are dynamic and entity creation is dynamic.
I have added SilverlightTable concept with dynamic objects in the applicaiton.
I have one xml like :
<NewDataSet>
<Table>
<knd_entity_Id>1</knd_entity_Id>
<CheckboxCol>0</CheckboxCol>
<kndtbkndnr>4001</kndtbkndnr>
<kndtbkndstatus>1</kndtbkndstatus>
<kndtbkndname1>Fritz & Franz Bikes GmbH</kndtbkndname1>
<kndtbkndname3 />
<kndtbkndplzstr>59321</kndtbkndplzstr>
<kndtbkndname2 />
<kndtbkndstrasse>In der Höh 8</kndtbkndstrasse>
<kndtbkndortstr>Wadersloh</kndtbkndortstr>
<kndtbkndtel>56673-54633</kndtbkndtel>
<kndtbkndfax />
<kndtbkndemail />
<kndtbkndwww>www.3s-erp.de</kndtbkndwww>
<kndtbkndmatchcode>Fritz & Franz Bikes,</kndtbkndmatchcode>
<kndtbkndlandpf>D</kndtbkndlandpf>
<kndtbkndwaehrung>EUR</kndtbkndwaehrung>
<kndtbkndlandstr>D</kndtbkndlandstr>
</Table>
<Table>
<knd_entity_Id>2</knd_entity_Id>
<CheckboxCol>0</CheckboxCol>
<kndtbkndnr>4002</kndtbkndnr>
<kndtbkndstatus>1</kndtbkndstatus>
<kndtbkndname1>Fahrrad Leasing AG</kndtbkndname1>
<kndtbkndname3 />
<kndtbkndplzstr>53622</kndtbkndplzstr>
<kndtbkndname2 />
<kndtbkndstrasse>Auf dem Holz 8</kndtbkndstrasse>
<kndtbkndortstr>Königswinter</kndtbkndortstr>
<kndtbkndtel>0245-98521</kndtbkndtel>
<kndtbkndfax />
<kndtbkndemail />
<kndtbkndwww />
<kndtbkndmatchcode>Fahrrad Leasing AG,</kndtbkndmatchcode>
<kndtbkndlandpf>D</kndtbkndlandpf>
<kndtbkndwaehrung>EUR</kndtbkndwaehrung>
<kndtbkndlandstr>D</kndtbkndlandstr>
</Table>
</NewDataSet>
Where COntents inside the Tables are not fixed. they may very as per the Entity attributes.
I need an Ilist from this XML using LInq to XML.
Kindly provide help.
Thanks and Regards,
Ruchi Patel

Can you try the below code
public class Table
{
public int EntityId {get;set;}
public string CheckboxCol {get;set;}
//TODO: Add rest of the properties
}
XElement element = XElement.Load("Your xml file path"); //replace with xml file path
if (element != null)
{
IList<Table> result = (from e in element.Descendants("Table")
select new Table
{
EntityId = int.Parse(e.Element("knd_entity_Id").Value),
CheckboxCol= e.Element("CheckboxCol").Value //TODO: Add rest of the properties
}).ToList();
}

Related

Composite C1 MVC Function - Data Annotations aren't being rendered into page

Trying to use the MVC Function (http://docs.composite.net/Functions/MVC/MVC-Functions), and when we run our standalone MVC App, the Data Annotations are appearing on the form controls, but when use the MVC Function feature on a C1 page, the control and value will render just fine, but the data annotations are missing.
Our View Model in the MVC App
[EmailAddress]
[StringLength(255)]
[Required]
public string Email { get; set; }
Inside the view (.cshtml) used for the MVC Function
#Html.TextBoxFor(m => m.AcctInfo.Email)
Rendered in the C1 page.
<input id="AcctInfo_Email" name="AcctInfo.Email" type="text" value="foo#test.com" >
This is the MVC Web App page (outside of C1)
<input data-val="true" data-val-email="The Email field is not a valid e-mail address."
data-val-length="The field Email must be a string with a maximum length of 255." data-val-length-max="255"
data-val-required="The Email field is required." id="AcctInfo_Email" name="AcctInfo.Email"
type="text" value="foo#test.com">
Is it possible to use Data Annotations in Composite C1 MVC Functions?
Figured it out. The web.config for the C1 project needed the following.
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>

Entity Framework orderby does not update datagrid

I'm new to C#, MVVM, WPF and Entity Framework
my problem is, if I order the reading of the database (orderby) the datagrid will not be changed if I add a new row. if i do not order the reading it works.
so addition infos:
my database looks like this
Table Konto
KontoID
KontoName
Table Buchung
KontoID
BuchungsID
Name
BuchDate
after the Konto is loaded it will load all related Buchungen
_entityKontoView = new CollectionViewSource();
_entityBuchungView = new CollectionViewSource();
// Loads the Konto
_entityKontoView.Source = _database.Konto;
_entityKontoView.View.CurrentChanged += (x, y) =>
{
_entityBuchungView.Source = ((Konto)_entityKontoView.View.CurrentItem).Buchung
//.OrderBy(date => date.BuchDate)
//.ThenBy(buchnr => buchnr.BuchungsID)
;
};
_entityKontoView.View.Refresh();
when i do now an OrderBy the datagrid will not be update after new inserted Row
i open the the database entity as follow:
public static databaseEntities _database = new databaseEntities();
my XAML binding on DataGrid:
ItemsSource="{Binding EntityBuchungsView.View}"
and my columns binding ;
Binding="{Binding Name}"
as far i used _database.Buchung.AddObject(test); and then _database.SaveChanges(); and my DataGrid got updated.
what do i false?
if anyone have the same problem like me
i did implement a new method for refreshing the view:
public static void RefreshView(CollectionViewSource entity)
{
entity.View.Refresh();
}
then it worked

ASP.NET MVC dropdownlist with entity framework

I'm new to asp.net mvc and the entity framework and i'm trying to make a dropdownlist with data out of my database. I have the following:
Controler:
//
// GET: /StoreEditor/Edit/(ID)
public ActionResult Edit(int id)
{
StoreEditorViewModel data = new StoreEditorViewModel(id);
return View(data);
}
ViewModel:
public StoreEditorViewModel(int id)
{
using (MvcTicketsEntities storeDB = new MvcTicketsEntities())
{
details = (from t in storeDB.Tickets.Include("Artists").Include("Genres")
where t.TicketId == id
select t).FirstOrDefault();
}
}
Now i get get all my labels and editors in de view. But how do i make a dropdownlist with all the names in the database table GENRES in the field NAME.
The table GENRES has 2 fields GenreId and Name.
Please refer to the below link:
http://codeclimber.net.nz/archive/2009/08/10/how-to-create-a-dropdownlist-with-asp.net-mvc.aspx
This explains quite well, how to approach it.
Basically, you would need to create List<SelectListItem> object and supply it to the dropdown list.
Thanks

MVVM light datagrid loading from two relational database tables

How to Load DataGrid with two related tables using MVVM light, I am using .NET RIA and silverlight 4.
for example if my data tables are:
userInfo-
userID, Name, AddressID
Address -
AddressID, StreetName, Zip
how can i create datagrid that displays [Name, StreetName, ZIp]
First of all you have to include Address table in GetQuery of UserInfo in your DomainService class like below...
[Query]
public IQueryable<UserInfo> GetUserInfos()
{
return this.ObjectContext.UserInfos.Include("Address");
}
then in the metadata file u have to add [Include] just above the
[Include]
public Addresses Address{ get; set; }
public int AddressID { get; set; }
Now Build the solution.
Now in the Xaml you can use like this
<sdk:DataGrid ItemsSource="{Binding UserList, Mode=TwoWay}" SelectedItem="{Binding CurrentUser, Mode=TwoWay}" Margin="0,0,0,2" AutoGenerateColumns="False">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Header="Name" Binding="{Binding Path=Name}"/>
<sdk:DataGridTextColumn Header="Street Name" Binding="{Binding Path=Address.StreetName}"/>
<sdk:DataGridTextColumn Header="Zip" Binding="{Binding Path=Address.Zip}"/>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
There are a couple approaches possible here. I will explain both that come to mind. First, is using a relationship already defined in your database. Second is to return via a custom model class.
Option 1:
I will assume you have created an Entity Framework Model (with a relationship) from your database on the Web Project and created the DomainService to host the Model. !!!One key to do when you create the model is to create meta-data classes for the models!!! (I missed this the first few times through, and it is critical to modify the behavior of your models, such as you are needing here)
Within the meta-data model, you will find the association property, and decorate it with the [Include] attribute. Here is an example I have from another project with a WorklanDetail, where I want to return the associated Assignments:
[MetadataTypeAttribute(typeof(WorkplanDetailMetadata))]
public partial class WorkplanDetail
{
internal sealed class WorkplanDetailMetadata
{
[Include]
public EntityCollection<Assignment> Assignments { get; set; }
}
}
then, you can just reference the property in your Silverlight application, and viola your address data is available.
To bind a single Entity (EntityReference, not collection) then you will just use you property to access the sub-entity on your datagrid's binding... For exmaple:
Text="{Binding Path=Address.StreetName}"
That is the simplest method that I know of.
Option 2 requires creating your own custom class (you must at least have a property decorated with the [Key] attribute ot facilitate the transfer to/from the client). Here is an example, I have used to get foldersearch result information:
public class FolderSearchResult
{
[Key]
public string EFOLDERID { get; set; }
public string Subject { get; set; }
public string FolderName { get; set; }
}
The key being that the EFOLDERID propety has the [Key] attribute, which uniquely identifies each item just like a PK in a database.
Then in your service class you can return this kind of like this:
public IEnumerable<FolderSearchResult> GetFolderResults(string search)
{
var query = from ge in this.ObjectContext.Generic_Engagement
from f in this.ObjectContext.eFolders
where ge.EFOLDERID == f.eFolderID &
f.eArchived == 0 &
f.eSubject.Contains(search) &
(from wp in this.ObjectContext.Workplans
where wp.EFOLDERID == f.eFolderID
select wp).Count() == 0 &
(from r in this.ObjectContext.Resources
where r.EFOLDERID == f.eFolderID
select r).Count() == 0
select new FolderSearchResult()
{
EFOLDERID = f.eFolderID,
FolderName = f.eFolderName,
Subject = f.eSubject
};
return query.AsEnumerable<FolderSearchResult>();
}
Couple of notes about this approach:
This works best in my opinion when you don't need Read-Only access. If you are doing updates/inserts/deletes use the first approach.
This works well to help when you need to create some logical object between completely different data sources (maybe userid and employeeid from database, plus display name, site location, etc. from Active Directory).
Updating this way is possible, but you must do a bit more work to separate out your items and update everything properly.
In summary, I would suggest using the first approach, unless the following occur: You need a logical object which crosses data-storage (separate db sources), or you need fast read-only access (such as ItemSource binding for drop-down list).
Offloading sorting and filtering logic to the server, even at the expense of additional entities loaded in memory so that your client can remain fast pays dividends in my experience. Using local sort in a CollectionViewSource or something can do the same, but just a few of these can really start slowing your application speed.

NHibernate IInterceptor implementation(add properties to DB table that original domain class doesn't have)

How is possible to set some special column values when update/insert entities via NHibernate without extending domain classes with special properties?
For example: in my case I would like to get object and just moment before update/insert db add to that object some additional information (like user id or computer name) by using IInterceptor. In other words I would like to add a few columns to DB Table without specifying new properties in original object's class. Do I have to configure/change/add to my Object.hbm.xml or App.config in that case?
The problem is that I can't change my original objects and base classes.So I have to figure out if possible to add information to DB table without changing of the original objects (even no inherit from any base classes)
Example:
Original Object has : FirstName ,LastName,Birthday,Address properties
Customer.hbm.xml has:
<property name="FirstName" column="FirstName" type="string" not-null="true" length="64" />
<property name="LastName" column="LastName" type="string" not-null="true" length="64" />
<property name="Birthday" column="Birthday" type="DateTime" not-null="true" />
<property name="Address" column="Address" type="string" not-null="true" />
My Interceptor class has method:
public bool OnSave(object entity, object id, object[] state, string[] propertyNames, NHibernate.Type.IType[] types)
at that moment or even maybe before save I have to add to the DB Customer table additional 2 columns (Computer Name and User Name for example ) that propertyNames[] and state[] parameters don't have them from the beginning,so that should be done on the fly.
MY DB Customer table has all the columns that I have described above.
Thank you.
I haven't tried it, but you should be able to add the dynamic properties to the Configuration prior to factory creation. Then you can populate those values in your interceptor.
Ex, retrieve the relevant PersistentClass from config.ClassMappings, then add your properties to it.
private void AddProperty(
PersistentClass pc,
string propertyName,
string columnName,
IType dataType)
{
SimpleValue val = new SimpleValue(pc.Table);
Column col = new Column(dataType, 0);
col.Name = columnName;
val.AddColumn(col);
val.Type = dataType;
Property prop = new Property(val);
prop.IsUpdateable = true;
prop.Name = propertyName;
prop.PropertyAccessorName = "nosetter.camelcase";
prop.Cascade = "none";
pc.AddNewProperty(prop);
}

Resources