Question Passing parameters to XSL in ASP.NET

stats

Member
Joined
Jan 15, 2009
Messages
11
Programming Experience
1-3
I've got my ASP.NET pulling in the XML and XSL and outputting a HTML page but I can't seem to get the parameters to go through. Does anyone have any idea what to do here? Thanks

VB.NET:
   Partial Class Default2
   Inherits System.Web.UI.Page

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim transformer As New XslCompiledTransform() ' create transformer
        Dim args As XsltArgumentList
        Dim XSLTsettings As New XsltSettings
        XSLTsettings.EnableDocumentFunction = True

        transformer.Load(New XPathDocument(Server.MapPath("nba_wap_rosters.xsl")), XSLTsettings, Nothing)

        args.AddParam("TEAM", "", "11")

        transformer.Transform(Server.MapPath("nba_all_roster.xml"), args, Server.MapPath("nba_roster.html"), Nothing)

        Response.WriteFile(Server.MapPath("nba_roster.html"))

    End Sub

End Class
 
The VB.Net code looks almost right, except it won't run at all until you create a New XsltArgumentList object.
Then there is an issue with Transform not allowing the parameter types you have provided.
Tell me, aren't you using Visual Studio for development? It actually tells you that you can't use that code.
Create the argument list object and send the transform to Response.OutputStream as I've shown you before and you should have a go.
How about the Xsl code? It is similar to the example code for XSLT Parameters in help?
 
The VB.Net code looks almost right, except it won't run at all until you create a New XsltArgumentList object.
Then there is an issue with Transform not allowing the parameter types you have provided.
Tell me, aren't you using Visual Studio for development? It actually tells you that you can't use that code.
Create the argument list object and send the transform to Response.OutputStream as I've shown you before and you should have a go.
How about the Xsl code? It is similar to the example code for XSLT Parameters in help?

I'm using Visual Web Developer 2008 Express. The XSL code is good. That's the site I've been using. Not creating a new XsltArgumentList object would be a problem, I can't believe I missed that. I'll try the transform thing too. Thanks a lot, I'm still trying to learn this and you've been a big help. I really appreciate it.
 
Finally, it worked! Thanks again. If I ever get good enough at this I'll try to answer questions too.

It is getting an error about having a null value for this line:
VB.NET:
args.AddParam("TEAM", "", TeamID)

If I change it to:
VB.NET:
args.AddParam("TEAM", "", "TeamID")
and run it then remove the quotes from TeamID it works. Does that make any sense?
 
Last edited:
What is the TeamID variable (declare) and how is it used?
 
What is the TeamID variable (declare) and how is it used?

I'm getting it from a Request.Querystring. I'm using it as an id to get a section of the xml in my xsl.

VB.NET:
Dim TeamID As String
TeamID = Request.QueryString("id")
 
Ok, it's a String. In that case Request.QueryString("id") does not find a "id" key in query string and returns Nothing.
 
Ok, it's a String. In that case Request.QueryString("id") does not find a "id" key in query string and returns Nothing.

So...

args.AddParam("TEAM", "", TeamID)

"TEAM" is the name of my parameter.
"" is what returns nothing if there is no value in the id
TeamID is my variable that I'm passing through

Is that correct? I get an ArgumentNullException: Value cannot be null. Parameter name: parameter when I have TeamID outside of quotes. Does this make any sense?

Thanks again.
 
Nevermind I got it. I just needed If statements if the values were blank. Thanks for all the help. I think I'm good now.
 
args.AddParam("TEAM", "", TeamID)

"TEAM" is the name of my parameter.
"" is what returns nothing if there is no value in the id
TeamID is my variable that I'm passing through
No, as VWD IDE tell you when you write the code, and when you hover AddParam, and also the documentation if you look it up, those parameters are "name", "namespaceUri" and "parameter". The documentation may offer more information, but if you just use context menu on that method and select "go to definition" the Object Browser will open and you will the see the same short descriptions for all the parameters that intellisense show you when you write the code for each one.
Value cannot be null. Parameter name: parameter
With the short explanations above in mind you now know exactly which of the method parameters is the problem, yes, it's the one named "parameter", the third one. You can also see this when debugging by hover the TeamID variable, you'll see it has value Nothing when problem occurs. Then you seek back in code to understand why it has this unexpected value. (and in this case you find it is assigned by Request.QueryString lookup)
I just needed If statements if the values were blank.
Yes, a preemptive strike is the way to guard against evil null values. This is the best way to prevent your application from crashing, knowing when problems can and often do occur, and making sure you have a valid object before using it. Sometimes it is not as easy as this to handle unexpected input, sometimes a call may cause crash no matter what, and in such cases using a Try-Catch block may be the only remedy, but always look out for possible execution paths of your code and take safety measures when needed. The big warning sign, which this case is a prime example of, is any situation when you receive input from user, or input that can be modified by user, and use that value for processing.

Perhaps I just misunderstood, and you just don't know what a namespace is, in that case look around, for example Wiki has this generic explanation: Namespace - Wikipedia, the free encyclopedia, Visual Basic help has this: Namespaces in Visual Basic, and since you're dealing with Xml here W3Schools has this: XML Namespaces. After reading these three articles you should realize they all say the same thing.
 
Back
Top