I'm working on a project to dynamically create PDFs with content from an SQL server. My current approach is to get the data I need into a DataSet using SqlDataAdapter, write the DataSet to a stream as XML, transform the XML into FO and then output a pdf using FOP. For some reason I get the following exception "System.NullReferenceException - Object reference not set to an instance of an object" when I try to set the SelectCommand property of my data adapter. Code for the project follows:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim con As SqlConnection
Dim cmd As SqlCommand
Dim IDlist As String
Dim keyidary As Array
Dim query As String
Dim tempxml As Stream
query = "*****query is valid and very long, so it's not included here*****"
IDlist = Request.Form("chosenIDs")
keyidary = Split(IDlist, ",")
Dim makequery As New StringBuilder(query)
For Each ID As String In keyidary
makequery.Append(" OR (KeyID = '" & ID & "')")
Next
query = makequery.ToString()
'Response.Write(query) When uncommented this prints the query just fine
'Response.End()
Try
con = New SqlConnection
con.ConnectionString = "****Connection String is valid****"
cmd = New SqlCommand
cmd.CommandText = query
cmd.CommandType = CommandType.Text
cmd.Connection = con
Dim adpt As SqlDataAdapter
adpt.SelectCommand = cmd
'Response.Write(query) When these are above or between the previous 2 statements, query is printed,
'Response.End() otherwise I get the error described above.
Dim profiles As New DataSet
adpt.Fill(profiles)
profiles.WriteXml(tempxml)
Dim step1 As XslTransform = New XslTransform
Dim step2 As XslTransform = New XslTransform
step1.Load(Server.MapPath("TransAttmpt1.xslt"))
step2.Load(Server.MapPath("formatXML.xsl"))
Dim xml, pdf As String
xml = "profiles.xml"
pdf = "Profiles.pdf"
Dim temp2xml As New XPathDocument(tempxml)
Response.Write(query)
Response.End()
Dim midstream As Stream
Dim finalxml As StreamWriter = New StreamWriter(xml)
step1.Transform(temp2xml, Nothing, midstream, Nothing)
Dim xpathdoc2 As XPathDocument = New XPathDocument(midstream)
step2.Transform(xpathdoc2, Nothing, finalxml, Nothing)
GeneratePDF(xml, pdf)
'There's a lot more but it doesn't seem relevant now...
I'm somewhat at a loss on how to proceed and any help is very greatly appreciated.
Thanks!
You are not initializing the dataadpater. You should initialize like this:
Dim adptAs SqlDataAdapter =New SqlDataAdapter()|||
Figured this out probably 3 minutes before you posted but thanks!
No comments:
Post a Comment