Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having great difficulty inserting a record into a table inside of a database.

Below is the code in which im getting the generic error message "Error at this request":

C#
context.SaveChanges();



Below is the code for the entire class. bear in mind the Product and ProductOptions viewsource are both there to load the details of the product from the previous window.

I basically want the details being entered into ordersViewSource to be inserted into the database once the Buy button is clicked. But it has proven to be very difficult.

Below is the code of the entire class, good luck.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using System.Data.Services.Client;
using iShopClient.iShop;

namespace iShopClient.CustomerFolder
{
    /// <summary>
    /// Interaction logic for OrderItemWindow.xaml
    /// </summary>
    public partial class OrderItemWindow : Window
    {
        public OrderItemWindow()
        {
            InitializeComponent();
        }

        private iShopEntities context;

        private Uri svcUri = new Uri("http://localhost:29875/iShop.svc");

        System.Windows.Data.CollectionViewSource productsViewSource;
        System.Windows.Data.CollectionViewSource productOptionsViewSource;
        System.Windows.Data.CollectionViewSource ordersViewSource;

        Product product;

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            productsViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("productsViewSource")));
            productOptionsViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("productOptionsViewSource")));
            ordersViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("ordersViewSource")));

            context = new iShopEntities(svcUri);
            context.IgnoreMissingProperties = true;


            var query = context.CreateQuery<Product>("GetProductDetails").AddQueryOption("ProductID", GlobalVariables.productID);

            var query2 = context.CreateQuery<ProductOption>("GetProductOptionDetails").AddQueryOption("OptionID", GlobalVariables.optionID);

            productsViewSource.Source = new DataServiceCollection<Product>(query);
            productOptionsViewSource.Source = new DataServiceCollection<ProductOption>(query2);


            //var query3 = context.CreateQuery<Customer>("GetCustomers").AddQueryOption("customerNo", GlobalVariables.customerNo);

            //ordersViewSource.Source = new DataServiceCollection<Customer>(query3)

            ordersViewSource.Source = new DataServiceCollection<Order>(context.Orders);
            ListCollectionView orderList = (ListCollectionView)ordersViewSource.View;

            orderList.AddNewItem(new Order());
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void buyButton_Click(object sender, RoutedEventArgs e)
        {
            context.SaveChanges();
        }
    }
}
Posted

1 solution

i don't thing you can do like that. you are adding object on windows load nad saving on button click. an what is "new Order()" in this code
C#
orderList.AddNewItem(new Order());
. are you trying to add blank object.

see example

C#
public void addProject()
{
     Project OProject = new Project();
     var context = new DbEntities();
     OProject.Name = "Project1";
     OProject.modificationDate = DateTime.Now;
     context.Project.Add(OProject);
     context.SaveChanges();
}


you have to initialize the object's required properties before adding item.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900