0
votes

I'm absolutely beginner to develop silverlight with RIA

i found the WCF RIA Services sample from silverlight.net by following the tutorial

i get an error while i trying to add new employee

it will pop up one window with message VS JIT Debugger

Code: 4004

Category: Managed Runtime Error

Message: System.ServiceModel.DomainServices.Client.DomainException: An error occurred while submitting changes on the Domain Context of type

I'm using silverlight 4 with AdventureWork2008Entities do this tutorial, and below are the code in the apps

    <dataForm:DataForm x:Name="addEmployeeDataForm"   AutoGenerateFields="False" AutoCommit="True" AutoEdit="True" CommandButtonsVisibility="None">
        <dataForm:DataForm.EditTemplate>
            <DataTemplate>
                <StackPanel>
                    <dataForm:DataField Label="Business Entity ID">
                        <TextBox Text="{Binding BusinessEntityID, Mode=TwoWay}" />
                    </dataForm:DataField>
                    <dataForm:DataField Label="Login ID">
                        <TextBox Text="{Binding LoginID, Mode=TwoWay}" />
                    </dataForm:DataField>
                    <dataForm:DataField Label="National ID">
                        <TextBox Text="{Binding NationalIDNumber, Mode=TwoWay}" />
                    </dataForm:DataField>
                    <dataForm:DataField Label="Title">
                        <TextBox Text="{Binding JobTitle, Mode=TwoWay}" />
                    </dataForm:DataField>
                    <dataForm:DataField Label="Marital Status">
                        <TextBox Text="{Binding MaritalStatus, Mode=TwoWay}" />
                    </dataForm:DataField>
                    <dataForm:DataField Label="Gender">
                        <TextBox Text="{Binding Gender, Mode=TwoWay,NotifyOnValidationError=True,  ValidatesOnExceptions=True }" />
                    </dataForm:DataField>
                    <dataForm:DataField Label="Salaried">
                        <CheckBox IsChecked="{Binding SalariedFlag, Mode=TwoWay,NotifyOnValidationError=True,  ValidatesOnExceptions=True }" />
                    </dataForm:DataField>
                    <dataForm:DataField Label="Active">
                        <CheckBox IsChecked="{Binding CurrentFlag, Mode=TwoWay,NotifyOnValidationError=True,  ValidatesOnExceptions=True }" />
                    </dataForm:DataField>
                </StackPanel>
            </DataTemplate>
        </dataForm:DataForm.EditTemplate>
    </dataForm:DataForm>

Employee Registration Window.xaml

private void addNewEmployee_Click(object sender, RoutedEventArgs e) { EmployeeRegistrationWindow addEmp = new EmployeeRegistrationWindow(); addEmp.Closed += new EventHandler(addEmp_Closed); addEmp.Show(); }

    private void addEmp_Closed(object sender, EventArgs e)
    {
        EmployeeRegistrationWindow emp = (EmployeeRegistrationWindow) sender;
        if (emp.NewEmployee != null)
        {
            OrganizationContext _organizationContext = (OrganizationContext) (employeeDataSource2.DomainContext);
            _organizationContext.Employees.Add(emp.NewEmployee);
            employeeDataSource2.SubmitChanges();
        }
    }

Employee List.xaml.cs

    public void InsertEmployee(Employee employee)
    {

        employee.HireDate = DateTime.Now;
        employee.ModifiedDate = DateTime.Now;
        employee.VacationHours = 100;
        employee.SickLeaveHours = 0;
        employee.rowguid = Guid.NewGuid();
        employee.BirthDate = new DateTime(1967, 3, 18);
        if ((employee.EntityState != EntityState.Detached))
        {
            this.ObjectContext.ObjectStateManager.ChangeObjectState(employee, EntityState.Added);
        }
        else
        {
            this.ObjectContext.Employees.AddObject(employee);
        }
    }

OrganizationService

1

1 Answers

2
votes

I notice no-one has replied to this post, but thought I'd add a little info I have come across while researching the exact same problem, in case it helps someone, if not the original poster.

  1. I assume you're using the AdventureWorks2008R2 database? If not please ignore the rest of this post!

  2. Some of the column names have apparently changed since this Silverlight walkthrough was generated. In the Employee table, EmployeeID is now BusinessEntityID for example, and Title is now JobTitle. If you fix these references you will then get a different error, which is caused by ...

  3. There is a Foreign Key relationship between the HumanResources.Employee table and the Person.Person table - if the Employee you're adding has no corresponding record in the Person table then the Insert will fail. So the code to insert an employee should first insert a record into the Person.BusinessEntity table and retrieve the resulting BusinessEntityID value. This BusinessEntityID value should then be used to insert relevant data into both the Person.Person and HumanResources.Employee tables.

At least, it looks that way to me. Hope this helps someone.

_Ade