One of my ASP Alliance readers recently sent me the following message:
The article to save the infopath form to the sql-2005 db is really useful for me and I am thankful to you.
Now, I want to show the retrieved infopath xml from the db in an asp.net web page
Pls post any article regarding retrieving and displaying the infopath forms on the web.
InfoPath uses XSL stylesheets to convert and display forms as HTML. These XSL stylesheets get created when you create Views in an InfoPath form template. An InfoPath form template will contain more than one view.xsl file if the form has multiple Views. You can use one of these XSL stylesheets or create your own from scratch and then use it in your ASP.NET code to convert the XML of an InfoPath form to HTML, and then use for example an iFrame to embed that HTML in an ASP.NET web page.
Note: You can extract the view.xsl files from an InfoPath form template by going to the File > Save As Source Files menu and then saving the extracted files locally on disk.
The first part of this solution, which is storing InfoPath forms in a SQL Server database table has already been explained in the ASP Alliance article I wrote back in 2007. In addition, you’ll have to come up with a mechanism for a user to select a form to be displayed in the first place. For example, you could pre-fill a Grid with the InfoPath forms that are available to be displayed, and then have the user select a form and click a button to display the InfoPath form. Any mechanism you choose should suit your specific solution.
To keep things simple, I’ll just show you how you could write code to retrieve the XML of an InfoPath form from a SQL Server table, convert it to HTML, and then display it in an ASP.NET web page.
In the following example, I’ll display an InfoPath form that has an ID equal to 2 in an iFrame on an ASP.NET web page as follows:
- Create a new ASP.NET Web Application project.
- Add the view.xsl file you extracted earlier in the root of the ASP.NET Web Application project.
- Add a Web Form to the ASP.NET Web Application project.
- Add an iFrame to the Web Form by using for example the following HTML code:
<iframe src="" height="600" width="800" runat="server" name="iFrame1" id="iFrame1" scrolling="auto" ></iframe>
- Add the following using statements to the code-behind of the Web Form:
using System.Data.SqlClient;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.IO;
-
Add code similar to the following to the Page_Load event of the Web Form:
using (SqlConnection conn = new SqlConnection(
"Data Source=ServerName;Initial Catalog=DatabaseName;Integrated Security=True"))
{
try
{
// Open the database connection
conn.Open();
// Retrieve the InfoPath form from the database
SqlCommand cmd = new SqlCommand(
@"SELECT Form FROM IPForms WHERE Id = 2", conn);
SqlDataReader reader = cmd.ExecuteReader();
XmlDocument doc = new XmlDocument();
while (reader.Read())
{
doc.LoadXml(reader["Form"].ToString());
}
if (doc != null && !String.IsNullOrEmpty(doc.InnerXml))
{
// Create an XslCompiledTransform object
XslCompiledTransform trans =
new XslCompiledTransform();
trans.Load(this.MapPath("~/view1.xsl"));
XPathNavigator nav = doc.CreateNavigator();
// Generate a temporary HTML file
string fileName =
Guid.NewGuid().ToString() + ".htm";
string filePath =
Path.Combine(this.MapPath("~"), fileName);
using (XmlWriter writer = XmlWriter.Create(filePath))
{
// Convert the XML to HTML
trans.Transform(nav, writer);
writer.Close();
}
// Display the HTML file in the iFrame
this.iFrame1.Attributes["src"] = fileName;
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
// Close the database connection
conn.Close();
}
}
where ServerName is the name of your SQL Server, DatabaseName the name of the database containing the table IPForms in which the InfoPath forms have been stored, 2 the ID of the InfoPath form being retrieved, Form the name of the database field containing the XML of the InfoPath form, view1.xsl the XSL stylesheet used to convert the XML to HTML, and iFrame1 the ID of the iFrame control on the web page.
The solution described in this post only displays InfoPath forms to be viewed. If you want InfoPath forms to be filled out through the web, you’ll have to create InfoPath browser forms and host the InfoPath form template in SharePoint.
Tips:
You can create an extra View, which can be used as a Print View and which displays all of the data and fields you’d want a user to be able to view, for your InfoPath form and use the view.xsl file of this View to convert the InfoPath form to HTML.
Any images – such as for example the calendar icon used on Date Picker controls – that are used by InfoPath and available as resource files stored in the InfoPath executable will display as broken images on the web page. If you want to retain these images you could create your own images, add them to your web application project, and change the content of the view.xsl file to reference them.
Other ways of retrieving and displaying InfoPath forms are described in these two articles:
- Retrieve an InfoPath form from SQL Server and display it in a new instance of InfoPath
- Retrieve an InfoPath form from SQL Server and display it in an InfoPath FormControl

Comments to this post were closed 30 days after it was published.