I have a litle problem with session. I want to put 2 values in session but I don't know how. Any suggestions?
I have a problem with this line: Me.Session("Rola1") = ("admin"; "user")
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim UserName As String = User.Identity.Name
Dim Conn As New System.Data.SqlClient.SqlConnection()
Conn.ConnectionString = "Data Source=MICHALK\SQLEXPRESS;persist security info=True; Initial Catalog=znam;Integrated Security=SSPI"
Dim cmd3 As New SqlCommand()
cmd3.CommandText = "Select * From Rodzina where UserName=@UserName"
cmd3.CommandType = CommandType.Text
cmd3.Connection = Conn
Dim param1 As New SqlParameter("@UserName", SqlDbType.NVarChar, 50)
param1.Value = UserName
cmd3.Parameters.Add(param1)
Conn.Open()
Dim ParentID As Integer = cmd3.ExecuteScalar()
Me.Session("RID") = ParentID
Conn.Close()
Me.Session("Rola1") = ("admin"; "user")
End If
End Sub
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:znamConnectionString %>"
SelectCommand="SELECT * FROM Rodzina Where RID=@RID and Rola=@Rola1">
<selectparameters>
<asp:Parameter name="RID" Type="Int32" />
<asp:Parameter name="Rola1" Type="String" />
</selectparameters>
</asp:SqlDataSource>
And then in your code-behind do this:
Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles SqlDataSource1.Selecting
e.Command.Parameters("RID") = "Value"
e.Command.Parameters("Rola1") = "Value"
End Sub
Spontan23
Member
345 Points
314 Posts
2 values in me.session
Feb 24, 2012 07:37 PM|LINK
Hi,
I have a litle problem with session. I want to put 2 values in session but I don't know how. Any suggestions?
I have a problem with this line: Me.Session("Rola1") = ("admin"; "user")
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then Dim UserName As String = User.Identity.Name Dim Conn As New System.Data.SqlClient.SqlConnection() Conn.ConnectionString = "Data Source=MICHALK\SQLEXPRESS;persist security info=True; Initial Catalog=znam;Integrated Security=SSPI" Dim cmd3 As New SqlCommand() cmd3.CommandText = "Select * From Rodzina where UserName=@UserName" cmd3.CommandType = CommandType.Text cmd3.Connection = Conn Dim param1 As New SqlParameter("@UserName", SqlDbType.NVarChar, 50) param1.Value = UserName cmd3.Parameters.Add(param1) Conn.Open() Dim ParentID As Integer = cmd3.ExecuteScalar() Me.Session("RID") = ParentID Conn.Close() Me.Session("Rola1") = ("admin"; "user") End If End Subsushanth009
Contributor
6243 Points
1168 Posts
Re: 2 values in me.session
Feb 24, 2012 07:40 PM|LINK
The namespace to be used is System.Web.HttpContext.Current.
To save a value in a Session use
Session["Name"] = yourName ;
To retrieve a value from session
string yourName = Session["Name"] as String;
Spontan23
Member
345 Points
314 Posts
Re: 2 values in me.session
Feb 24, 2012 07:43 PM|LINK
But i want to retrieve two values that are in the table. For example yourName and myName.
sushanth009
Contributor
6243 Points
1168 Posts
Re: 2 values in me.session
Feb 24, 2012 07:50 PM|LINK
Then you would use two session variables..
int parentID = Session["RID"] as Int32 ;
string name = Session["YourName"] as string ;
Spontan23
Member
345 Points
314 Posts
Re: 2 values in me.session
Feb 24, 2012 07:57 PM|LINK
So how should I use it in the select comand:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:znamConnectionString %>" SelectCommand="SELECT * FROM Rodzina Where RID=@RID and Rola=@Rola1"> <selectparameters> <asp:sessionparameter name="RID" sessionfield="RID" Type="Int32" /> <asp:sessionparameter name="Rola1" sessionfield="Rola1" Type="String" /> </selectparameters> </asp:SqlDataSource>DaNuGai
Member
149 Points
87 Posts
Re: 2 values in me.session
Feb 24, 2012 07:57 PM|LINK
Do you do something like this:
Session("Rola1") = "Admin|User"And if you want to see if the session variable has a perticular value, you cand do it this way:
If Session("Rola1") IsNot Nothing If InStr(Session("Rola1"), "Admin") > 0 ' True Else ' False End If End IfHope this helps
Spontan23
Member
345 Points
314 Posts
Re: 2 values in me.session
Feb 24, 2012 08:02 PM|LINK
But I want to show rows from table that containes values from rola column = admin and user.
DaNuGai
Member
149 Points
87 Posts
Re: 2 values in me.session
Feb 24, 2012 08:06 PM|LINK
You could do something like this:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:znamConnectionString %>" SelectCommand="SELECT * FROM Rodzina Where RID=@RID and Rola=@Rola1"> <selectparameters> <asp:Parameter name="RID" Type="Int32" /> <asp:Parameter name="Rola1" Type="String" /> </selectparameters> </asp:SqlDataSource>And then in your code-behind do this:
Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles SqlDataSource1.Selecting e.Command.Parameters("RID") = "Value" e.Command.Parameters("Rola1") = "Value" End SubDaNuGai
Member
149 Points
87 Posts
Re: 2 values in me.session
Feb 24, 2012 08:07 PM|LINK
You have to combine my last two replies for a complete solution. Good Luck!!
Spontan23
Member
345 Points
314 Posts
Re: 2 values in me.session
Feb 24, 2012 08:12 PM|LINK
I change the select command to: SelectCommand="SELECT * FROM Rodzina Where RID=@RID and Rola='admin' or rola= 'user'">
Is this going to work?