views:

2184

answers:

4

I am simply trying to call a store procedure (SQL Server 2008) using C# and passing XMLDocument to a store procedure parameter that takes a SqlDbType.Xml data type. I am getting error: Failed to convert parameter value from a XmlDocument to a String. Below is code sample. How do you pass an XML Document to a store procedure that is expecting an XML datatype? Thanks.

        XmlDocument doc = new XmlDocument();
        //Load the the document with the last book node.
        XmlTextReader reader = new XmlTextReader(@"C:\temp\" + uploadFileName);
        reader.Read();
        // load reader 
        doc.Load(reader);

        connection.Open();

        SqlCommand cmd = new SqlCommand("UploadXMLDoc", connection);

        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.Add("@Year", SqlDbType.Int);
        cmd.Parameters["@Year"].Value = iYear;
        cmd.Parameters.Add("@Quarter", SqlDbType.Int);
        cmd.Parameters["@Quarter"].Value = iQuarter;
        cmd.Parameters.Add("@CompanyID", SqlDbType.Int);
        cmd.Parameters["@CompanyID"].Value = iOrganizationID;
        cmd.Parameters.Add("@FileType", SqlDbType.VarChar);
        cmd.Parameters["@FileType"].Value = "Replace";
        cmd.Parameters.Add("@FileContent", SqlDbType.Xml);
        cmd.Parameters["@FileContent"].Value = doc;
        cmd.Parameters.Add("@FileName", SqlDbType.VarChar);
        cmd.Parameters["@FileName"].Value = uploadFileName;
        cmd.Parameters.Add("@Description", SqlDbType.VarChar);
        cmd.Parameters["@Description"].Value = lblDocDesc.Text;
        cmd.Parameters.Add("@Success", SqlDbType.Bit);
        cmd.Parameters["@Success"].Value = false;
        cmd.Parameters.Add("@AddBy", SqlDbType.VarChar);
        cmd.Parameters["@AddBy"].Value = Page.User.Identity.Name;

        cmd.ExecuteNonQuery();
        connection.Close();
A: 

From this, it looks like you need to pass a string in, instead of an XmlDocument.

Deeksy
+2  A: 

You need to pass the xml as a string.

But if you don't need the xml functions in the database, you might consider using varbinary to store the files.

Mehmet Ergut
Thanks. I got it to work. Added the following coded:StringWriter sw = new StringWriter();XmlTextWriter xw = new XmlTextWriter(sw);doc.WriteTo(xw);StringReader transactionXml = new StringReader(sw.ToString());XmlTextReader xmlReader = new XmlTextReader(transactionXml);SqlXml sqlXml = new SqlXml(xmlReader);Converting it to a string was not enough. I got the following error: XML parsing: line 1, character 38, unable to switch the encoding”. So, I converted to string then coverted it to SqlXml and it worked.
Lakeshore
You can take a simpler approach: `cmd.Parameters["@FileContent"].Value = new SqlXml(File.OpenRead("file.xml"));`
Mehmet Ergut
A: 

Another simpler way is to write the xmldoc to a string and pass that to the stored procedure.

Dim sb As New StringBuilder()
Dim wrtr As New System.IO.StringWriter(sb)
doc.Save(wrtr)
Dim strxml As String = sb.ToString()
cmd.Parameters.Add("@FileContent", SqlDbType.Xml);
cmd.Parameters["@FileContent"].Value =strxml;
Faraz
A: 

Other way to do it if you don't mind loosing the xml declaration (version and encoding) is just:

XML.DocumentElement.OuterXml 'where XML is a XMLDocument
alcobiavieira