c# - LINQ to Sharepoint InsertOnSubmit Question -


for example have list called product , has 3 columns, productname (which title), productprice , producttype.

  • productname string
  • productprice currency (double)
  • producttype lookup on producttypes list

normally easy me if not contain lookup column, dont know how deal columns when inserting.

i had tried 1 returns error specified cast not valid.

here current code

entitylist<producttypeitem> producttypes = dc.getlist<producttypeitem>("producttype");  productitem newproduct = new productitem();  newproduct.title = txtproductname.text; newproduct.productprice = double.parse(txtproductprice.text);  newproduct.producttype = (from in producttypes a.title == ddproducttype.selecteditem.text select a).firstordefault();  dc.product.insertonsubmit(newproduct); dc.submitchanges();    

what newproduct.producttype here error occurs.

please note ddproducttype datasource producttype list , uses title in datatextfield , datavaluefield

this might out. first example explains how insert should work links existing data. sample code should give enough hints fix problem:

adventureworksdatacontext db = new adventureworksdatacontext();  // linq query stateprovince stateprovince state = (from states in db.stateprovinces                        states.countryregioncode == "au" && states.stateprovincecode == "nsw"                        select states).firstordefault(); // linq function addresstype addresstype addrtype = db.addresstypes.firstordefault(s => s.name == "home");  customer newcustomer = new customer() {     modifieddate= datetime.now,     accountnumber= "aw12354",      customertype='i',     rowguid= guid.newguid(),     territoryid= state.territoryid    // relate record keys }; contact newcontact = new contact() {     title = "mr",     firstname = "new",     lastname = "contact",     emailaddress = "newcontact@company.com",     phone = "(12) 3456789",      passwordhash= "xxx",     passwordsalt= "xxx",     rowguid = guid.newguid(),     modifieddate = datetime.now }; individual newind = new individual() {     contact= newcontact,    // relate records objects (we dont know keys new records yet)     customer= newcustomer,     modifieddate= datetime.now }; address newaddress = new address() {     addressline1= "12 first st",     city= "sydney",     postalcode= "2000",      modifieddate=datetime.now,     stateprovince= state,     rowguid = guid.newguid() };  // link our customer address via new customeraddress record newcustomer.customeraddresses.add(new customeraddress() { address = newaddress, customer = newcustomer, addresstype = addrtype, modifieddate = datetime.now, rowguid = guid.newguid() });  // save changes database db.submitchanges(); 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -