Hi, I need to create a session variable in a page that is given a set string value.
In the next page I need to call that variable in the WHERE part of a SELECT clause eg.
SELECT [colA], [colB] WHERE colB = 'value from previous page'
Any help would be greatly appreciated.
Cheers, Chris
VB.NET:
"SELECT [colA], [colB] WHERE colB ='" & Session("Name") & "'"
C#
"SELECT [colA], [colB] WHERE colB ='" + Session["Name"] as string + "'"
HTH,
Ryan
Brilliant, cheers. How do I create the variable in the previous page in VB.NET
Cheers again, Chris
TheCrawsh wrote:
How do I create the variable in the previous page in VB.NET
Session("SomeSession") = SomeValue
Since the value is stored as an object, SomeValue can be of any datatype.
HTH,
Ryan
Cheers, I think I have the syntax wrong, I'm not sure on exactly where to put these, I have this so far:
In the page where the variable is created:
<scriptrunat="server">
Session("banner") ="BSAFD"
</script>
But do I need to put it in a function or something?
In the page that uses the variable:
<asp:SqlDataSourceID="BanRot"runat="server"ConnectionString="<%$ ConnectionStrings:w2gConnectionString %>"
SelectCommand="SELECT [Pic], [Nav], [Page], [Alt] FROM [ads-home] WHERE Page ='" &Session("Banner") &"'">
</asp:SqlDataSource>
Do I need to 'call' the variable in a parameter or something?
Many thanks, Chris
You can declare the session variable anywhere in your code. Just be sure to do it before you use it. Also, you can use the SessionParameter with the SqlDataSource in conjunction with the SelectCommand.
<asp:SqlDataSource ID="BanRot" runat="server" ConnectionString="<%$ ConnectionStrings:w2gConnectionString %>" SelectCommand="SELECT [Pic], [Nav], [Page], [Alt] FROM [ads-home] WHERE Page =?">
<SelectParameters>
<asp:SessionParameter Name="Page"SessionField="Banner" />
</SelectParameters>
</asp:SqlDataSource>
HTH,
Ryan
Ok, do I need to call it in the script on the same page as the sqldatasource? Or does the Session parameter automatically call it from the memory?
Also, On the initial page where I create the variable, I have
<scriptrunat="server">
Session("banner") = BSAFD
</script>
But I'm getting a 'Declaration Expected' Error, Do I need to put this in a function?
Cheers again, Chris
All you need is the SessionParameter for the SelectCommand and .NET will take care of the rest for you.
When you decalre your session variable, unless BSAFD is a variable, you'll need to enclose it in quotes as it is a string.
Session("Banner") = "BSAFD"
HTH,
Ryan
Ah, brilliant, I now have it working, I got confused with the ? in the WHERE clause, I've now changed it to @.Page - is this just a shortcut meaning put your Parameter here.
Another quick question, I have many pages that have a different variable so it will have a different session variable. If I create a new variable in a different page, will it overwrite the old one or do I need to put some code in there to do it, eg.
Page one:
<% Session("banner") ="value a" %>
I then use that however , but then navigate to page two where I want value b as the session variable, if I put in this:
<% Session("banner") ="value 2" %>
Will it overwrite the old one?
Cheers again, perfect help!! Chris
TheCrawsh wrote:
I've now changed it to @.Page - is this just a shortcut meaning put your Parameter here.
Yes. It's a placeholder for the value.
TheCrawsh wrote:
If I create a new variable in a different page, will it overwrite the old one or do I need to put some code in there to do it, eg.
Yes. Doing so would assign a new value to the session variable.
HTH,
Ryan
0 comments:
Post a Comment