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
{
}
}