views:

743

answers:

1

Hi there. I am doing small Silverlight app which will allow users to Create orders. I have created Linq 2 SQL dbml and dragged there my database tables, "Orders" and "OrderLines" , there is an association between them. Order.ID ~ OrderLine.OrderID, so then i created DomainService for my tables, where i enabled client access, it generated for me the methods, Insert,Update,Delete,Get, for Orders and OrderLines, now i am creating New Order from my silverlight application, the usercontrol looks like this:

 public partial class NewOrderView : UserControl
{
    public ObservableCollection<OrderLine> OrderLines { get; set; }
    public NewOrderView()
    {
        InitializeComponent();
        OrderLines = new ObservableCollection<OrderLine>();
        dataGrid.ItemsSource = OrderLines;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var order = new Order();
        foreach (var orderLine in OrderLines)
        {
            order.OrderLines.Add(orderLine);
        }
        order.CompanyId = int.Parse(StaticContainer.CurrentUser.CompanyId.ToString());
        order.CreationDate = DateTime.Now;
        order.Status = "შეკვეთილი";
        order.Id = 1;
        var ctx = new EntreeDomainContext();
        ctx.Orders.Add(order);
        ctx.SubmitChanges();
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        StaticContainer.Navigation.Back();
    }

    private void Button_Click_2(object sender, RoutedEventArgs e)
    {
        StaticContainer.Navigation.LogOut();
    }

    private void AddProduct(object sender, ProductAdditionEventHandlerArgs args)
    {
        var result = OrderLines.Where(x => x.Item.itmKEY == args.Product.itmKEY).FirstOrDefault();
        if(result==null)
        OrderLines.Add(new OrderLine(){Item = args.Product});
    }
}

and Domain Service Method:

 public void InsertOrder(Order order)
    {
        Context.Orders.InsertOnSubmit(order);
        Context.SubmitChanges();
    }

i have put here break point, and the thread comes here , and everything dones ok. but after that in the database no order and no orderline exist. and output writes : "A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.Linq.dll"

what can i do? please help, and thank you.

+1  A: 

Are you creating OrderLines from scratch? If so, you need to make sure that if they're new, that you're calling Context.OrderLines.InsertOnSubmit(orderLine) for each OrderLine you're adding, or you'll get problems like this. Also, you should provide all exception details here...

Dave Markle
yes i am creating from scratch, but i tought orderlines will be added into database with order. so that means i have to add orderlines manually, separated from order?
Va
Yes. The reason is because linq 2 SQL doesn't know if your relationship is associative or a parent-child type of relationship, so it makes you be explicit. This threw me too when I was first starting out with the framework. It kind of makes sense of you think about it...
Dave Markle