Forums - C# Corner

Forum guidelines
Mangesh barmate

Mangesh barmate

  • 1.2k
  • 115
  • 72k

How to pass XML as input paramter from c# code to SQL Table

Apr 1 2013 10:01 AM

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à
public static string ObjectToXml(SalesPurchase salesPurchase)
        {
            System.IO.StringWriter output = new System.IO.StringWriter();
XmlSerializer xs = new XmlSerializer(salesPurchase.GetType());
            xs.Serialize(output, salesPurchase);
return output.ToString();

        }

HERE I AM PASSING XML AS A INPUT PARAMETER TO STORED PROCEDUREà
public static 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!!


Answers (1)

C# Corner Annual Conference 2017

SPONSORED BY

File APIs for .NET
Aspose are the market leader of .NET APIs for file business formats – natively work with DOCX, XLSX, PPT, PDF, MSG, MPP, images formats and many more!