Silverlight 4 RIA service issue - silverlight

Hi I have created a sample database application in Silverlight by following a tutorial. I am trying to insert a record in DB but it throws an error Submit operation failed validation. Please inspect Entity.ValidationErrors for each entity in EntitiesInError for more information. I am using Entity framework for DAL. My code is given below. Exception occure when I insert a record. But Iam not sure at which stage exception occurs.
RecordInsertPage.cs file
public partial class BookRegistaeration : ChildWindow
{
public Book newBook { get; set; }
public BookRegistaeration()
{
InitializeComponent();
newBook = new Book();
AddBookForm.CurrentItem =AddBookForm;
AddBookForm.BeginEdit();
}
private void OKButton_Click(object sender, RoutedEventArgs e)
{
AddBookForm.CommitEdit();
this.DialogResult = true;
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
newBook = null;
AddBookForm.CancelEdit();
this.DialogResult = false;
}
}
Service.cs Page
public class OrganizationService : LinqToEntitiesDomainService<LibraryEntities1>
{
public IQueryable<Book> GetBooks()
{
return this.ObjectContext.Books.OrderBy(e => e.BookId);
}
public void InsertBook(Book book)
{
//book.Title = "Hello book";
//book.Author = "Ali";
//book.Category = "Humanity";
if ((book.EntityState != EntityState.Detached))
{
this.ObjectContext.ObjectStateManager.ChangeObjectState(book, EntityState.Added);
}
else
{
this.ObjectContext.Books.AddObject(book);
}
}
public void UpdateBook(Book currentBook)
{
this.ObjectContext.Books.AttachAsModified(currentBook, this.ChangeSet.GetOriginal(currentBook));
}
public void DeleteBook(Book book)
{
if ((book.EntityState != EntityState.Detached))
{
this.ObjectContext.ObjectStateManager.ChangeObjectState(book, EntityState.Deleted);
}
else
{
this.ObjectContext.Books.Attach(book);
this.ObjectContext.Books.DeleteObject(book);
}
}
}
private void LibraryDataSource_SubmittedChanges(object sender, SubmittedChangesEventArgs e)
{
if (e.HasError)
{
MessageBox.Show(string.Format("Changes were not saved: {0}", e.Error.Message));
e.MarkErrorAsHandled();
}
submitButton.IsEnabled = true;
}
void addBook_Closed(object sender, EventArgs e)
{
BookRegistaeration book = (BookRegistaeration)sender;
if (book.newBook != null)
{
OrganizationContext _OrganizationContext = (OrganizationContext)(LibraryDataSource.DomainContext);
_OrganizationContext.Books.Add(book.newBook);
LibraryDataSource.SubmitChanges();
}
}

Try doing as the exception message suggests, put a breakpoint on your SubmittedChanges event to inspect that exception object for which you should be able to see Entity.ValidationErrors for each entity in EntitiesInError.
This should tell you which of the fields in the object you are trying to add has failed the validation check, you may have null data in fields which cannot be null. You may find string properties are not allowed to be empty.
Try also to ensure your form is properly populating the object you will be adding, you could place a breakpoint before CommitEdit is called and inspect the object state.

i do not understand this line !
AddBookForm.CurrentItem =AddBookForm;
why did you write this?
it should be something like this
AddBookForm.CurrentItem = your class object !

Related

While attaching current binding source error occurs: "An entity object cannot be referenced by multiple instances of IEntityChangeTracker"

I have created simple form for editing client list using Entity Framework and data binding. However, when im trying to edit rows error "An entity object cannot be referenced by multiple instances of IEntityChangeTracker" occurs. I don't know where I attach one object to two data contexts..
Main user control:
ArtGalleryEntities db;
public UserControl5()
{
InitializeComponent();
}
private void UserControl5_Load(object sender, EventArgs e)
{
db = new ArtGalleryEntities();
db.Configuration.LazyLoadingEnabled = false;
klienciBindingSource.DataSource = db.Klienci.ToList();
}
private void edit_button_Click(object sender, EventArgs e)
{
if (klienciBindingSource.Current == null)
return;
using (AddEditForm frm = new AddEditForm(klienciBindingSource.Current as Klienci))
{
if (frm.ShowDialog() == DialogResult.OK)
{
klienciBindingSource.DataSource = db.Klienci.ToList();
}
}
}
AddEditForm here :
ArtGalleryEntities db;
public AddEditForm(Klienci obj)
{
InitializeComponent();
db = new ArtGalleryEntities();
if (obj == null)
{
klienciBindingSource.DataSource = new Klienci();
db.Klienci.Add(klienciBindingSource.Current as Klienci);
}
else
{
db.Entry(obj).State = System.Data.Entity.EntityState.Unchanged;
klienciBindingSource.DataSource = obj;
db.Klienci.Attach(klienciBindingSource.Current as Klienci); // here error occurs
}
}
I have tried following replaces for attaching:
db.Entry(klienciBindingSource.Current as Klienci).State = System.Data.Entity.EntityState.Unchanged;
db.Entry(klienciBindingSource.Current as Klienci).State = System.Data.Entity.EntityState.Modified;
But it didn't work out for me

Awesomium 1.7.5.1, WinForms, WebControl obj.ExecuteJavascript

I want to insert html code into existing html code.
But I do not see the result. Here is the code C #:
1) Program.cs
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
wUI.DocumentReady += wUI_DocumentReady;
}
private void Form1_Load(object sender, EventArgs e)
{
// code here ?
}
void wUI_DocumentReady(object sender, DocumentReadyEventArgs e)
{
wUI.LoadHTML("<html><body>sadasdsad</body></html>");
HtmlManager html = HtmlManager.Instance;
string[] placeholders = { "asset://customdatastore/path/to/any", "type-button", "no-action", "Example link" };
html.Add("{3}", placeholders);
html.InnerCode(html.Code, wUI, "body");
wUI.Refresh();
}
}
2) HtmlManager.cs
public sealed class HtmlManager
{
private static readonly Lazy<HtmlManager> InstanceField = new Lazy<HtmlManager>(() => new HtmlManager());
private StringBuilder _stringBuilder = null;
public string Code { get { return _stringBuilder.ToString(); } }
private HtmlManager()
{
if (_stringBuilder != null)
_stringBuilder.Clear();
_stringBuilder = new StringBuilder();
}
public static HtmlManager Instance { get { return InstanceField.Value; } }
public void Add(string row, string[] placeholders = null)
{
if (placeholders != null)
_stringBuilder.AppendLine(string.Format(row, placeholders));
_stringBuilder.AppendLine(row);
}
public void InnerCode(string code, object sender, string afterTag = "html")
{
Awesomium.Windows.Forms.WebControl ui = (Awesomium.Windows.Forms.WebControl)sender;
ui.ExecuteJavascript(string.Format("document.getElementsByTagName({0})[0].innerHTML({1})", afterTag, code));
}
public void Clear()
{
_stringBuilder.Clear();
}
}
The event (DocumentReady) does not happen, I do not believe, maybe I'm wrong somewhere?
UP: I try do it:
private void Form1_Load(object sender, EventArgs e)
{
wUI.LoadHTML("<html><body>sadasdsad</body></html>");
}
void wUI_DocumentReady(object sender, DocumentReadyEventArgs e)
{
HtmlManager html = HtmlManager.Instance;
string[] placeholders = { "asset://customdatastore/path/to/any", "type-button", "no-action", "Example link" };
html.Add("{3}", placeholders);
wUI.ExecuteJavascript("document.getElementsByTagName('body').innerHTML(\"sometext\")");
//html.InnerCode(html.Code, wUI, "body");
//wUI.Refresh();
}
No result
UP 2:
public void Add(string row, string[] placeholders = null)
{
if (placeholders != null)
_stringBuilder.AppendLine(string.Format(row, placeholders));
if (placeholders == null)
_stringBuilder.AppendLine(row);
}
UP 3:
Work with:
wUI.Source = new Uri(#"http://google.com");
in Form1_Load
You can use LoadHtml method, but only after document is fully loaded (don't confuse with DocumentReadyState.Ready) It works for me at least:
private void WebControl_DocumentReady(object sender, DocumentReadyEventArgs e)
{
if (e.ReadyState != DocumentReadyState.Loaded) return;
}
But as an initialisation, you should use Source property, like you wrote in your third update

Query CRM data in Silverlight

I am building a silverlight app for CRM 2011 and I was wondering what the best way to retrieve data from the CRM system is.
I have linked in my organisation as a service reference and am able to access that. I have seen a few different ways to retrieve data but they all seem rather complicated. Is there anything like what we can use in plugins such as a fetch XMl query or a simple Service.Retrieve method?
Thanks
If you add a service reference to your project you can use LINQ to query the datasets.
You can download the CSDL from Developer Resources under the customisation area.
private FelineSoftContext context;
private System.String serverUrl;
private DataServiceCollection<SalesOrder> _orders;
public MainPage()
{
InitializeComponent();
serverUrl = (String)GetContext().Invoke("getServerUrl");
//Remove the trailing forward slash returned by CRM Online
//So that it is always consistent with CRM On Premises
if (serverUrl.EndsWith("/"))
serverUrl = serverUrl.Substring(0, serverUrl.Length - 1);
Uri ODataUri = new Uri(serverUrl + "/xrmservices/2011/organizationdata.svc/", UriKind.Absolute);
context = new FelineSoftContext(ODataUri) { IgnoreMissingProperties = true };
var orders = from ord in context.SalesOrderSet
orderby ord.Name
select new SalesOrder
{
Name = ord.Name,
SalesOrderId = ord.SalesOrderId
};
_orders = new DataServiceCollection<SalesOrder>();
_orders.LoadCompleted += _orders_LoadCompleted;
_orders.LoadAsync(orders);
}
void _orders_LoadCompleted(object sender, LoadCompletedEventArgs e)
{
if (e.Error == null)
{
if (_orders.Continuation != null)
{
_orders.LoadNextPartialSetAsync();
}
else
{
OrderLookup.ItemsSource = _orders;
OrderLookup.DisplayMemberPath = "Name";
OrderLookup.SelectedValuePath = "Id";
}
}
}
You will also need to add a method in your class as below:
private static ScriptObject GetContext()
{
ScriptObject xrmProperty = (ScriptObject)HtmlPage.Window.GetProperty("Xrm");
if (null == xrmProperty)
{
//It may be that the global context should be used
try
{
ScriptObject globalContext = (ScriptObject)HtmlPage.Window.Invoke("GetGlobalContext");
return globalContext;
}
catch (System.InvalidOperationException)
{
throw new InvalidOperationException("Property \"Xrm\" is null and the Global Context is not available.");
}
}
ScriptObject pageProperty = (ScriptObject)xrmProperty.GetProperty("Page");
if (null == pageProperty)
{
throw new InvalidOperationException("Property \"Xrm.Page\" is null");
}
ScriptObject contextProperty = (ScriptObject)pageProperty.GetProperty("context");
if (null == contextProperty)
{
throw new InvalidOperationException("Property \"Xrm.Page.context\" is null");
}
return contextProperty;
}
You will need to add an additional class library and put the following code within it. Change the class name to match the Context you exported:
partial class FelineSoftContext
{
#region Methods
partial void OnContextCreated()
{
this.ReadingEntity += this.OnReadingEntity;
this.WritingEntity += this.OnWritingEntity;
}
#endregion
#region Event Handlers
private void OnReadingEntity(object sender, ReadingWritingEntityEventArgs e)
{
ODataEntity entity = e.Entity as ODataEntity;
if (null == entity)
{
return;
}
entity.ClearChangedProperties();
}
private void OnWritingEntity(object sender, ReadingWritingEntityEventArgs e)
{
ODataEntity entity = e.Entity as ODataEntity;
if (null == entity)
{
return;
}
entity.RemoveUnchangedProperties(e.Data);
entity.ClearChangedProperties();
}
#endregion
}
public abstract class ODataEntity
{
private readonly Collection<string> ChangedProperties = new Collection<string>();
public ODataEntity()
{
EventInfo info = this.GetType().GetEvent("PropertyChanged");
if (null != info)
{
PropertyChangedEventHandler method = new PropertyChangedEventHandler(this.OnEntityPropertyChanged);
//Ensure that the method is not attached and reattach it
info.RemoveEventHandler(this, method);
info.AddEventHandler(this, method);
}
}
#region Methods
public void ClearChangedProperties()
{
this.ChangedProperties.Clear();
}
internal void RemoveUnchangedProperties(XElement element)
{
const string AtomNamespace = "http://www.w3.org/2005/Atom";
const string DataServicesNamespace = "http://schemas.microsoft.com/ado/2007/08/dataservices";
const string DataServicesMetadataNamespace = DataServicesNamespace + "/metadata";
if (null == element)
{
throw new ArgumentNullException("element");
}
List<XElement> properties = (from c in element.Elements(XName.Get("content", AtomNamespace)
).Elements(XName.Get("properties", DataServicesMetadataNamespace)).Elements()
select c).ToList();
foreach (XElement property in properties)
{
if (!this.ChangedProperties.Contains(property.Name.LocalName))
{
property.Remove();
}
}
}
private void OnEntityPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (!this.ChangedProperties.Contains(e.PropertyName))
{
this.ChangedProperties.Add(e.PropertyName);
}
}
#endregion
}
I am will suggest you to use Silvercrmsoap , it's very easy to use. I have used this in my silverlight projects.

Windows Phone Navigation code error

where am I going wrong here?
private void lstCars_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string currCar = (sender as ListBox).SelectedItem as string;
NavigationService.Navigate(new Uri("/ViewCarDetails.xaml?info=" + currCar, UriKind.Relative));
}
here is the page im trying to navigate to
public ViewCarDetails(string registrationNum)
{
//stuff
}
and here is the code that the program jumps to when I get an error (in App.xaml.cs)
private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
{
if (System.Diagnostics.Debugger.IsAttached)
{
// A navigation has failed; break into the debugger
System.Diagnostics.Debugger.Break();
}
}
ive checked the URI but no typos
Thank you
The problem is that your are passing a parameter via NavigationService and ViewCarDetails class constructor is expecting a parameter that you are not passing through.
To solve it you have to create a constructor with no parameters and take the parameter you are passing through navigationservice from NavigatedTo event as follow:
public ViewCarDetails()
{
//stuff
}
and
protected override void OnNavigatedTo(NavigationEventArgs e)
{
string registrationNum = string.Empty;
if (NavigationContext.QueryString.TryGetValue("index", out registrationNum))
{
//do stuff
}
}
Try it out and let us know,
ADDED:
public class ViewCarDetails : PhoneApplicationPage
{
private string registrationNum;
public ViewCarDetails()
{
//stuff
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
registrationNum = string.Empty;
if (NavigationContext.QueryString.TryGetValue("index", out registrationNum))
{
//do stuff
}
}
//other methods and properties
}
regards,

Error with WP7 Silverlight - No Overload for "" Matches Delegate

I am new to WP7 programming and I have been following this tutorial
http://weblogs.asp.net/scottgu/archive/2010/03/18/building-a-windows-phone-7-twitter-application-using-silverlight.aspx
However I have run into a number of errors and I was wondering if anyone could tell me why. I have been over and over the code and as far as I can see it is all correct.
The first issue is a:
No overload for "twitter_DownloadsStringCompleted" matches delegate system.net.downloadStringEventHandler
Here is the code:
private void button2_Click(object sender, RoutedEventArgs e)
{
WebClient twitter = new WebClient();
twitter.DownloadStringCompleted += new DownloadStringCompletedEventHandler(twitter_DownloadStringCompleted);
twitter.DownloadStringAsync(new Uri("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=" + username.Text));
}
void twitter_DownloadStringCompleted(object sender, DownloadStringCompletedEventHandler e)
{
throw new NotImplementedException();
}
public class TwitterItem
{
public string UserName { get; set; }
public string Message { get; set; }
public string ImageSource { get; set; }
}
void twitter_DownloadStringCompleted(object sender, DownloadStringCompletedEventHandler e)
{
if (e.Error != null)
return;
XElement xmlTweets = XElement.Parse(e.Result);
listBox1.ItemsSource = from tweet in xmlTweets.Descendants("status")
select new TwitterItem
{
ImageSource = tweet.Elemend("user").Element("profile_image_url").Value,
Message = tweet.Element("text").Value,
UserName = tweet.Element("user").Element("SCreen_name").Value
};
}
}
}
The argument list for your completed event handler should be:
void twitter_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
note it's DownloadStringCompletedEventArgs not DownloadStringCompletedEventHandler.
See this image from the tutorial:

Resources