Asked by:
Pass session variable as parameter

Question
-
User-1506996564 posted
Hello Every body
my page contains a GridView that is bounded to SQLDataSource
this is the SelectQuery
SELECT Activity.ID,Activity.title FROM Activity WHERE Activity.Department=(SELECT Department.ID FROM Department WHERE Department.Head=(SELECT Employee.ID FROM Employee WHERE UserId=@UI))
this query works fine if I test it and returns the right records
in the page_load event handler , it reads the current logged in user id and stores it in a session variable
this is the code
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim user_id As Guid = Membership.GetUser().ProviderUserKey Session("UI") = user_id.ToString End Sub
I configured the SQLDataSource to read the parameter from this session variablesee image below
But ,
I dont know where is the mistake ,
the grid do not show the targeted records
Any Help is appreciated
thank you
Thursday, November 12, 2015 3:50 AM
All replies
-
User61956409 posted
Hi ghassan_aljabiri,
Firstly, please debug your code to make sure if the variable “user_id” is stored in session successfully.
Secondly, the SqlDataSource should look like this.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [MenuBoards] WHERE ([Id] = @Id)"> <SelectParameters> <asp:SessionParameter Name="Id" SessionField="UI" Type="Int32" /> </SelectParameters> </asp:SqlDataSource>
Besides, I create a simple sample to filter data via SessionParameter, it works fine on my side, please refer to it.
WebForm1
<div> <asp:TextBox ID="txtwhere" runat="server"></asp:TextBox><asp:Button ID="btnok" runat="server" Text="GO" /> </div>
Protected Sub btnok_Click(sender As Object, e As EventArgs) Handles btnok.Click Session("UI") = txtwhere.Text.ToString() Response.Redirect("WebForm2.aspx") End Sub
WebForm2
<div> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <br /> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource1"> <Columns> <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" /> <asp:BoundField DataField="SiteID" HeaderText="SiteID" SortExpression="SiteID" /> <asp:BoundField DataField="LineNumber" HeaderText="LineNumber" SortExpression="LineNumber" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [MenuBoards] WHERE ([Id] = @Id)"> <SelectParameters> <asp:SessionParameter Name="Id" SessionField="UI" Type="Int32" /> </SelectParameters> </asp:SqlDataSource> <br /> ALL DATA: <br /> <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource2"> <Columns> <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" /> <asp:BoundField DataField="SiteID" HeaderText="SiteID" SortExpression="SiteID" /> <asp:BoundField DataField="LineNumber" HeaderText="LineNumber" SortExpression="LineNumber" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [MenuBoards]"></asp:SqlDataSource> </div>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Label1.Text = "Id is " + Session("UI").ToString() End Sub
Best Regards,
Fei Han
Friday, November 13, 2015 1:41 AM -
User-1506996564 posted
Thank you Fei Han
user_id is stored in the Session variable
this how select parameter looks like ,
<asp:SessionParameter Name="ID" SessionField="UI" />
I do not use the
Type="Int32"
since the user_id is a Guide
but unfortunately , it wont work
binding happens at loading time
Thursday, November 19, 2015 4:35 AM -
User-1716253493 posted
- check the session value, test query the data using this string
Response.Write(Session("UI"))
copy the result above then use it to query the data.
Try also call grid.DataBind() after set session("UI") value
Wednesday, December 2, 2015 10:14 AM -
User-698989805 posted
I recommend you to check the select query. Try inner joins instead of writing queries in the where clause and match the UserId = @UI out of the where clause that is inside the first bracket. That may help you to sort it out.Friday, December 4, 2015 4:10 PM -
User-1716253493 posted
If you are sure that userid is match in the query, try call databind
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim user_id As Guid = Membership.GetUser().ProviderUserKey Session("UI") = user_id.ToString Grd.DataBind() End Sub
Monday, December 7, 2015 2:30 AM