0

I am trying to create a simple application where I am querying against entity, getting results, running the results through a web service and getting the missing information. I am stuck at making updating the database with the new and updated results here is the code I have so far.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VetexV2
{
 class Program
 {
    static void Main(string[] args)
    {
        string tid = "somevalue".ToString();

        //webservice related 
        VertexWebService.PostalAddressType pat = new VertexWebService.PostalAddressType();
        VertexWebService.VertexEnvelope env = new VertexWebService.VertexEnvelope();
        VertexWebService.lookupTaxAreasRequest LTAR = new VertexWebService.lookupTaxAreasRequest();
        VertexWebService.LookupTaxAreasWS60Client soap = new VertexWebService.LookupTaxAreasWS60Client();
        VertexWebService.LoginType log = new VertexWebService.LoginType();
        VertexWebService.TaxAreaLookupType talt = new VertexWebService.TaxAreaLookupType();
        VertexWebService.TaxAreaRequestType tart1 = new VertexWebService.TaxAreaRequestType();

        log.TrustedId = tid;

        using (var db = new VetexV2.pesqlshareEntities1())
        { 
            // query against the database
            var query = from b in db.SalesOrder_FromSF
                        where b.VertexGeoCode.Length == 1
                        select new { address1 = b.Address1,
                                     address2 = b.Address2,
                                     city = b.JobCity,
                                     state = b.StateCode,
                                     zipcode = b.JobZip,
                                     country = b.Country,
                                     TAID = b.VertexGeoCode,
                                     SalesforceOpportunity = b.SF_OpportunityID};

          //parsing through the result 
          foreach (var item in query)
          {
              Console.WriteLine(item.address1);
              Console.WriteLine(item.address2);
              Console.WriteLine(item.city);
              Console.WriteLine(item.state);
              Console.WriteLine(item.zipcode);
              Console.WriteLine(item.TAID);
              pat.PostalCode = item.zipcode;
              pat.MainDivision = item.state;
              pat.Country = item.country;
              pat.City = item.city;
              pat.StreetAddress1 = item.address1;
              pat.StreetAddress2 = item.address2;
              talt.Item = pat;

              tart1.TaxAreaLookup = talt;
              env.Item = tart1;
              env.Login = log;
              env.Item = tart1;

                  LTAR.VertexEnvelope = env;

              //using the info from above  providing it to websevice
                  soap.Open();

                  soap.LookupTaxAreas60(ref LTAR.VertexEnvelope);

                  var reslt = ((VertexWebService.TaxAreaResponseType)(LTAR.VertexEnvelope.Item)).TaxAreaResult[0].taxAreaId.ToString();

    Console.WriteLine(reslt);// displaying the missing or updated field on screen 
                  Console.WriteLine("Press any Key");
                 // how do I put this updated field back into database ?


          }
        }
    }
  }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Aaron
  • 662
  • 8
  • 20
  • which updated field do you exactly mean? What I see from your code is that you construct some wcf related objects. Than your query the DB and assign the values from your db to the wcf objects and push them through a soap pipe. I nowhere see you put something in a DbContext related object, or am I missing something... – Ric .Net Nov 06 '14 at 18:40
  • That is the part I am having issue with. how do I assign this value reslt variable back to entity? so that it would recognize the change and I can do update. – Aaron Nov 06 '14 at 18:45
  • The field I am trying to update in database is the VertexGeoCode – Aaron Nov 06 '14 at 18:47
  • take a look at db.SaveChanges() – Hamid Pourjam Nov 06 '14 at 19:06
  • you should fetch the entity you want from your db, change it values, and call `db.SaveChanges()` – Hamid Pourjam Nov 06 '14 at 19:07
  • I am not able to asign the value to item.TAID it says Error 2 Property or indexer 'AnonymousType#1.TAID' cannot be assigned to -- it is read only C:\Projects\VetexV2\VetexV2\Program.cs 69 23 VetexV2 – Aaron Nov 06 '14 at 19:20

2 Answers2

2

As I read your code correctly I'm making some assumptions:

  • VertextGeoCode is a string
  • The result (reslt) must be directly placed in VertextGeoCode without any modification.

Instead of making an anonymous type in your query, just select the entity itself:

var query = from b in db.SalesOrder_FromSF
            where b.VertexGeoCode.Length==1
            select b;

Assign your webservice objects directly to the entity properties and push back in the result. Then just call SaveChanges() on the dbContext:

using (var db = new VetexV2.pesqlshareEntities1())
{
    // query code as above
    // other possible code...

    foreach (var item in query)
    {
        pat.PostalCode = item.JobZip;
        pat.MainDivision = item.StateCode;
        pat.Country = item.Country;
        pat.City = item.JobCity;
        pat.StreetAddress1 = item.Address1;
        pat.StreetAddress2 = item.Address2;

        //... other code omitted for brevity

        soap.LookupTaxAreas60(ref LTAR.VertexEnvelope);
        var reslt = ((VertexWebService.TaxAreaResponseType)(LTAR.VertexEnvelope.Item))
                   .TaxAreaResult[0].taxAreaId.ToString();

        item.VertexGeoCode = reslt;
        // other code...        
    }
    db.SaveChanges();
}
Ric .Net
  • 5,540
  • 1
  • 20
  • 39
  • didnt work ... now i am getting error on db.savechanges(); error is {"New transaction is not allowed because there are other threads running in the session."} – Aaron Nov 06 '14 at 19:38
  • see my edit... just call savechanges once. see [this](http://stackoverflow.com/a/2180920/3294832) answer. – Ric .Net Nov 06 '14 at 19:50
0

I found another way by using

using (TransactionScope scope = new TransactionScope())

{
using ( var db= new dbEntity)
{
//query code 
 Foreach ( var item in query)
{
 //program logic 
 db.savechange();
}
}
scope.complete();
}

by doing above I was able to do the save within foreach loop and save the transction i needed to make sure that had primary key and also needed to reference the system.Transctions

Aaron
  • 662
  • 8
  • 20