.Net, ADO.NET, ASP.NET, Error Handling, Programming, SQL, XML

InvalidCastException – Failed to convert parameter value from a XmlDocument to a String

InvalidCastException
InvalidCastException was unhandled by user code

If you are working with XML data types in your .NET and SQL Server code, then you will most likely encounter this error message at some point.

Specifically, this error is related to improperly passing an XML value as a SQLParameter to your database.

Since most of the work one does with XML in .NET is done using the XmlDocument object, it is certainly surprising when Visual Studio complains when one tries to save the XML object as an XmlDocument to the database (say to a stored procedure or directly to a database table).

The bottom line is that you must adjust your  XmlDocument object  to a format compatible with the SqlDbType.XML accepted by your SqlParameters.

There are a few ways that you can use to pass your XML object to your database:

Continue reading “InvalidCastException – Failed to convert parameter value from a XmlDocument to a String”

Advertisement
.Net, ASP.NET, HTML, JavaScript, ListView, WebForms, XML, XML Control, XPath, XSLT

Call a JavaScript Function in an XSL StyleSheet of an ASP.NET XML Control

Using an XSL stylesheet together with an ASP.NET XML control can be a great way to format the output of a database query. For instance a cool implementation is that you can embed an XML control within a ListView control so that you can output a tree-like structure in your ListView rather than the standard tabular structure.

One surprisingly tricky thing is setting up a call to a JavaScript function within your XSL stylesheet that passes data from your database query dynamically. Unlike traditional ASP/ASP.NET you cannot directly output your database variable in your XSL markup (ie: along the lines of: Text='<%# Eval(“SomeData”) %>’). Instead, you first need to assign the dynamic data to a variable and then you need to use that variable to format your JavaScript call.

Continue reading “Call a JavaScript Function in an XSL StyleSheet of an ASP.NET XML Control”