leety
leety

Reputation: 177

Linq to Sql General Help - Insert Statement

I am currently trying to create a new order (which will be shown below) in a web service, and then send that data to insert a new row into the database. For some reason my DBML / Data Context does not allow me to use InsertOnSubmit.

Any ideas? I haven't used Linq to Sql in about 7 months.

Thanks in advance.

[WebMethod]
    public string InsertOrderToDatabases()
    {
        //Start Data Contexts ------
        DataContext db = new DataContext(System.Configuration.ConfigurationManager.AppSettings["RainbowCMSConnectionString"]);
        DataContext dcSqlOES = new DataContext(System.Configuration.ConfigurationManager.AppSettings["OESConnectionString"]);

        //Get table from local database
        Table<Schedule> Schedule = db.GetTable<Schedule>();

        //Find last order number in databases
        var lastOrderNumber = from lOrder in Schedule
                              orderby lOrder.templ_idn descending
                              select lOrder.templ_idn;

        int firstOrderID;
        var firstOrder = lastOrderNumber.FirstOrDefault();
        firstOrderID = firstOrder.Value + 1;

        qrOrder qrOrd = new qrOrder
        {
            .... data in here creating a new order

        };

        //TODO: fix below with an insert on submit
        if (qrOrd != null)
        {
        //    **Schedule.InsertOnSubmit(qrOrd);**
        }
        //db.GetTable<Schedule>().InsertOnSubmit(qrOrd);

        try
        {
            //Submit the changes to the database
            db.SubmitChanges();
            return "Orders were sent to the databases.";
        }
        catch ()
        {

        }
    }

Upvotes: 0

Views: 542

Answers (3)

Jon Senchyna
Jon Senchyna

Reputation: 8037

Based on your response, it appears that you are using the wrong table, or perhaps the wrong data type. I also noticed that when you declare your localSchedule variable, you declare it as type Table<Schedule>, which means it should contain Schedule entities, not qrOrder entities.

Table<TEntity>.InsertOnSubmit expects a specific strongly typed entity to be passed in. In your case, it is expecting Web_Service.Schedul‌e, but you are trying to pass in a qrOrder.

Upvotes: 1

Aghilas Yakoub
Aghilas Yakoub

Reputation: 28970

you can try with

db.GetTable(typeof(Schedule)).InsertOnSubmit(qrOrd);

Or

 db.GetTable(qrOrd.GetType()).InsertOnSubmit(qrOrd);

Upvotes: 0

Pit Digger
Pit Digger

Reputation: 9780

 Schedule.InsertOnSubmit(qrOrd);

That line will not treat to submit changes to connected entity , Try this

    db.Schedule.InsertOnSubmit(qrOrd);
    db.SubmitChanges();

Upvotes: 0

Related Questions