How to pass XML as input paramter from c# code to SQL Table column which has type XML ?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
My Table column type is XML.
I am passing data to table by parsing string into XML and passed as a parameter from C# code.
I am doing in below stepà
publicstatic string ObjectToXml(SalesPurchase salesPurchase)
{
System.IO.StringWriter output =new System.IO.StringWriter();
XmlSerializer xs = newXmlSerializer(salesPurchase.GetType());
xs.Serialize(output, salesPurchase);
return output.ToString();
}
HERE I AM PASSING XML AS A INPUT PARAMETER TO STORED PROCEDUREà
publicstatic bool SaveXML(Sales111 salesPurchase)
{
int recordsAffected = 0;
string orderXml = ObjectToXml(salesPurchase);
XElement planPropertyElement = XElement.Parse(orderXml);
using (SqlConnection conn =new SqlConnection(connString))
{
conn.Open();
SqlParameter[] parms = new SqlParameter[3];
parms[0] =new SqlParameter("@Id", salesPurchase.ReportingCustomerId);
parms[1] =new SqlParameter("@SesId", salesPurchase.SessionId);
parms[2] =new SqlParameter("@CenXml", planPropertyElement);
try
{
recordsAffected =SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "usp_PSaveXml", parms);
}
catch (Exception ex)
{
string str = ex.Message.ToString();
}
}
return recordsAffected > 0 ? true : false;
}
I AM GETTING BELOW EXCEPTIONà
No mapping exists from object type System.Xml.Linq.XElement to a known managed provider native type.
How i overcome it ?Please suggest any approach!!


View the full article
 
Back
Top