LINQ to SQL - SubmitChanges not working

Last post 05-16-2008 3:14 PM by hapax_legomenon. 2 replies.

Sort Posts:

  • LINQ to SQL - SubmitChanges not working

    05-15-2008, 1:21 PM

    I have an XML string in memory, which I want to write to my database. I'm using LINQ

    But SubmitChanges() isn't working. Everything works fine up to that point, and all the values are correct when I debug. The database values just aren't updated.

      

            //pare down xml to contain locations only
            //using LINQ
            XElement resultData = XElement.Parse(xmlResult);  //xmlResult is the string containing the XML
            var locationsXmlData = from lc in resultData.Descendants("location")
                            select lc;
    
            //for each location, write data to database
            LocationDataContext locationData = new LocationDataContext();
    
            foreach (var locationXmlElement in locationsXmlData)  
            {
                XElement xmlId = locationXmlElement.Element("locationid");
                
                //check if id/key already exists for this location
                var queryExists = from dbRow in locationData.locations  //locations is db table name
                                  where dbRow.locationid == (string)xmlId
                                  select new { dbRow.locationid };
    
                if (queryExists.Count() == 1)  //row exists, do database UPDATE
                {
                    var locx = locationData.locations.Single(x => x.locationid == (string)locationXmlElement.Element("locationid"));
    
                    string debug = (string)locationXmlElement.Element("name");
                    
                    //locx.name = (string)locationXmlElement.Element("name");
                    locx.name = debug;
    
                    locationData.SubmitChanges();  //DOESN'T WORK, DATABASE TABLE NOT UPDATED
                }
                else  //row doesn't exist, do database INSERT
                {
                    
                }
    
            }
     
  • Re: LINQ to SQL - SubmitChanges not working

    05-16-2008, 1:10 PM

    I've learned that I need to use the object defined in my DataContext. In my case, the object is named "location".

    Here's a simpler test, but SubmitChanges() does not work for this either

     

    DbDevDataContext myDataContext = new DbDevDataContext();
    
    //update db
    location myloc = myDataContext.locations.Single(x => x.locationid == "loc1");
    myloc.name = "changedname";
    myloc.startdate = DateTime.Now;
    
    myDataContext.SubmitChanges();
    
     
  • Re: LINQ to SQL - SubmitChanges not working

    05-16-2008, 3:14 PM
    Answer

    OK, now my simpler test is working. Maybe restarting my computer, Visual Studio, etc did it?

Page 1 of 1 (3 items)