EDN Admin
Well-known member
Hi there xDocument linq <br/>
Why when I add a XElemnt to existing XDocument and call SubmitChanges() its not updating my database XML Field
<br/>
I have got
using System;<br/>
using System.Collections.Generic;<br/>
using System.Linq;<br/>
using System.Text;<br/>
using System.Xml.Linq;<br/>
using System.Windows;<br/>
namespace SecurityAdministration.BussinessLayer<br/>
{<br/>
class StudyConfigWrapper<br/>
{<br/>
private StudyManagerDataContext _studyDataContext = new StudyManagerDataContext(SecurityManagerUtility.Database.CreateStudyManagerSqlConnection().ConnectionString);<br/>
private XDocument _xmlDoc;<br/>
private StudyConfig _studyConfigData;<br/>
private XElement _parent;<br/>
private XElement _xElement;<br/>
private bool _dataExists = false;<br/>
private int _studyId;<br/>
private int _siteId;<br/>
public StudyConfigWrapper()<br/>
{
}<br/>
public int StudyId<br/>
{<br/>
get { return _studyId; }<br/>
set { _studyId = value; }<br/>
}<br/>
public int SiteId<br/>
{<br/>
get {return _siteId;}<br/>
set {_siteId =value;}<br/>
<br/>
}
public void AddElement(XElement Element)<br/>
{<br/>
SetStudyConfigData();<br/>
_studyConfigData.InputDataXML.Element("Parent").Add(Element);<br/>
// _parent.Add(Element);<br/>
<br/>
// _xmlDoc.Add(Element);<br/>
}<br/>
void SetStudyConfigData ()<br/>
{<br/>
try<br/>
{<br/>
_studyConfigData = (from sc in _studyDataContext.StudyConfigs<br/>
where sc.SiteId == SiteId && sc.StudyId
== StudyId<br/>
select sc).Single<StudyConfig>();<br/>
}<br/>
catch (Exception ex)<br/>
{<br/>
}<br/>
finally<br/>
{<br/>
if (_studyConfigData == null)<br/>
{<br/>
_studyConfigData = new StudyConfig();<br/>
_xmlDoc = new XDocument();<br/>
_parent = new XElement("Parent");<br/>
_xmlDoc.Add(_parent);<br/>
_dataExists = false;<br/>
_studyConfigData.InputDataXML = _xmlDoc;<br/>
}<br/>
else<br/>
{<br/>
_xmlDoc = _studyConfigData.InputDataXML;<br/>
_dataExists = true;<br/>
// _studyDataContext.ExecuteCommand("UPDATE StudyConfig SET inputDataXML = NULL WHERE Id =" + _studyConfigData.Id);
}<br/>
}<br/>
<br/>
}<br/>
public void Save()<br/>
{<br/>
try<br/>
{<br/>
<br/>
if (!_dataExists)<br/>
{<br/>
_studyDataContext.StudyConfigs.InsertOnSubmit(_studyConfigData);<br/>
}
_studyDataContext.SubmitChanges();<br/>
}<br/>
catch (Exception ex)<br/>
{<br/>
MessageBox.Show(ex.Message);<br/>
}<br/>
}
<br/>
}<br/>
}
_studyConfigData is "<Parent> <sample_Recieved number_of_itme="Int" date="DateTime" /></Parent>" at the beginning
<br/>
And call AddElement(XElement Element) where Element is “<sample_Recieved_Second number_of_itme="Int" date="DateTime" />”<br/>
So when I debug the value of the _studyConfigData.InputDataXML it shows as <br/>
<Parent> <br/>
<sample_Recieved number_of_itme="Int" date="DateTime" /><br/>
<sample_Recieved_Second number_of_itme="Int" date="DateTime" /><br/>
</Parent><br/>
But when I call Save Ling is not updating the record when I view from the sql profiler there is not update command is excuted and the database recode is unchanged
<br/>
Where am I gone worng ?
<br/>
<br/>
<br/>
View the full article
Why when I add a XElemnt to existing XDocument and call SubmitChanges() its not updating my database XML Field
<br/>
I have got
using System;<br/>
using System.Collections.Generic;<br/>
using System.Linq;<br/>
using System.Text;<br/>
using System.Xml.Linq;<br/>
using System.Windows;<br/>
namespace SecurityAdministration.BussinessLayer<br/>
{<br/>
class StudyConfigWrapper<br/>
{<br/>
private StudyManagerDataContext _studyDataContext = new StudyManagerDataContext(SecurityManagerUtility.Database.CreateStudyManagerSqlConnection().ConnectionString);<br/>
private XDocument _xmlDoc;<br/>
private StudyConfig _studyConfigData;<br/>
private XElement _parent;<br/>
private XElement _xElement;<br/>
private bool _dataExists = false;<br/>
private int _studyId;<br/>
private int _siteId;<br/>
public StudyConfigWrapper()<br/>
{
}<br/>
public int StudyId<br/>
{<br/>
get { return _studyId; }<br/>
set { _studyId = value; }<br/>
}<br/>
public int SiteId<br/>
{<br/>
get {return _siteId;}<br/>
set {_siteId =value;}<br/>
<br/>
}
public void AddElement(XElement Element)<br/>
{<br/>
SetStudyConfigData();<br/>
_studyConfigData.InputDataXML.Element("Parent").Add(Element);<br/>
// _parent.Add(Element);<br/>
<br/>
// _xmlDoc.Add(Element);<br/>
}<br/>
void SetStudyConfigData ()<br/>
{<br/>
try<br/>
{<br/>
_studyConfigData = (from sc in _studyDataContext.StudyConfigs<br/>
where sc.SiteId == SiteId && sc.StudyId
== StudyId<br/>
select sc).Single<StudyConfig>();<br/>
}<br/>
catch (Exception ex)<br/>
{<br/>
}<br/>
finally<br/>
{<br/>
if (_studyConfigData == null)<br/>
{<br/>
_studyConfigData = new StudyConfig();<br/>
_xmlDoc = new XDocument();<br/>
_parent = new XElement("Parent");<br/>
_xmlDoc.Add(_parent);<br/>
_dataExists = false;<br/>
_studyConfigData.InputDataXML = _xmlDoc;<br/>
}<br/>
else<br/>
{<br/>
_xmlDoc = _studyConfigData.InputDataXML;<br/>
_dataExists = true;<br/>
// _studyDataContext.ExecuteCommand("UPDATE StudyConfig SET inputDataXML = NULL WHERE Id =" + _studyConfigData.Id);
}<br/>
}<br/>
<br/>
}<br/>
public void Save()<br/>
{<br/>
try<br/>
{<br/>
<br/>
if (!_dataExists)<br/>
{<br/>
_studyDataContext.StudyConfigs.InsertOnSubmit(_studyConfigData);<br/>
}
_studyDataContext.SubmitChanges();<br/>
}<br/>
catch (Exception ex)<br/>
{<br/>
MessageBox.Show(ex.Message);<br/>
}<br/>
}
<br/>
}<br/>
}
_studyConfigData is "<Parent> <sample_Recieved number_of_itme="Int" date="DateTime" /></Parent>" at the beginning
<br/>
And call AddElement(XElement Element) where Element is “<sample_Recieved_Second number_of_itme="Int" date="DateTime" />”<br/>
So when I debug the value of the _studyConfigData.InputDataXML it shows as <br/>
<Parent> <br/>
<sample_Recieved number_of_itme="Int" date="DateTime" /><br/>
<sample_Recieved_Second number_of_itme="Int" date="DateTime" /><br/>
</Parent><br/>
But when I call Save Ling is not updating the record when I view from the sql profiler there is not update command is excuted and the database recode is unchanged
<br/>
Where am I gone worng ?
<br/>
<br/>
<br/>
View the full article