|
-
Aug 28th, 2005, 07:24 PM
#1
Thread Starter
Hyperactive Member
Simple Session Problem [RESOLVED]
I am passing my session variable from one page to another then storing the value into my database, for some reason my code isn't working?
page one:
Code:
Session["userID"] = "4545";
Server.Transfer("CreateProfile.aspx");
page two:
Code:
SQL = "INSERT INTO LoginDetails (UserID, Username, [Password])"+
"VALUES (@UserID, @Username, @Password)";
OleDbCommand WO_Cmd2 = new OleDbCommand(SQL, WO_Conn);
WO_Cmd2.Parameters.Add("@UserID", Session["userID"]);
WO_Cmd2.Parameters.Add("@Username", txtUsername.Text);
WO_Cmd2.Parameters.Add("@Password", txtPassword.Text);
WO_Conn.Open();
WO_Cmd2.ExecuteNonQuery();
WO_Conn.Close();
Server.Transfer("Login.aspx");
Error:
Code:
System.Data.OleDb.OleDbException: Parameter @UserID has no default value.
Anyone know what i am doing wrong?
Last edited by modernthinker; Sep 1st, 2005 at 02:35 PM.
-
Aug 29th, 2005, 12:05 AM
#2
Lively Member
Re: Simple Session Problem
Hey
you should use Session["userID"].ToString() instead of Session["userID"] in following line.
WO_Cmd2.Parameters.Add("@UserID", Session["userID"]);
happy coding
-
Aug 29th, 2005, 01:01 PM
#3
Thread Starter
Hyperactive Member
Re: Simple Session Problem
Hi,
I did originally have it as : Session["userID"].ToString()
But i would get the following error:
Code:
System.NullReferenceException: Object reference not set to an instance of an object.
Error point to the following line:
WO_Cmd2.Parameters.Add("@UserID", Session["userID"].ToString());
-
Aug 30th, 2005, 12:38 AM
#4
Lively Member
Re: Simple Session Problem
Check for the value of Session["userID"]. Most probably this is null when you are trying to access it to save to the database. Like:
string userID = "";
if(Session["userID"] != null)
{
userID = Session["userID"].ToString();
}
Also, check for the EnableSessionState value in the Page directive to be "true" (for both the source and destination page), and sessionState in the web.config file to be "inProc" (if you are using that form of Session State).
Last edited by kalaHasti; Aug 30th, 2005 at 12:50 AM.
Everything I code, is a piece for Museum.
-
Aug 31st, 2005, 07:47 PM
#5
Thread Starter
Hyperactive Member
Re: Simple Session Problem
Hi kalaHasti,
I tried what you said but its still not working. For some reason i can access the contents of the seesion if i have:
Code:
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
Label4.Text = Session["userID"].ToString();
}
}
i can then do:
Code:
WO_Cmd2.Parameters.Add("@UserID", Label4.Text);
So why does it work like this, any clues? I don't really want to access the session like this because i need to pass this session to other pages and i will have the same problem.
Any advice appreciated.
-
Aug 31st, 2005, 09:04 PM
#6
Thread Starter
Hyperactive Member
Re: Simple Session Problem
I have posted my full code below, just incase i am making an error somewhere:
Code:
private void btnLogin_Click(object sender, System.EventArgs e)
{
OleDbConnection WO_Conn = new OleDbConnection(ConfigurationSettings.AppSettings["DSN"]);
string SQL = "SELECT * from LoginDetails";
OleDbCommand WO_Cmd = new OleDbCommand(SQL, WO_Conn);
OleDbDataReader myReader;
WO_Conn.Open();
myReader = WO_Cmd.ExecuteReader();
while (myReader.Read())
{
if (myReader["Username"].ToString() == txtUsername.Text)
{
flagNewUsername = "false";
}
}
myReader.Close();
WO_Conn.Close();
if (flagNewUsername == "false")
{
lbl_UsernameExists.Text = "Username already exists, please choose another username";
}
else
{
SQL = "INSERT INTO LoginDetails (UserID, Username, [Password])"+
"VALUES (@UserID, @Username, @Password)";
OleDbCommand WO_Cmd2 = new OleDbCommand(SQL, WO_Conn);
WO_Cmd2.Parameters.Add("@UserID", Session["userID"].ToString());
WO_Cmd2.Parameters.Add("@Username", txtUsername.Text);
WO_Cmd2.Parameters.Add("@Password", txtPassword.Text);
WO_Conn.Open();
WO_Cmd2.ExecuteNonQuery();
WO_Conn.Close();
Server.Transfer("CreateProfile.aspx");
}
}
-
Sep 1st, 2005, 11:14 AM
#7
Thread Starter
Hyperactive Member
Re: Simple Session Problem
The problem seems to do with the ButtonClick event. I move my code into the Page_Load then it works.
I did a basic test, if you try and acces the contents of a session variable using a button event it doesn't work? Why?
-
Sep 1st, 2005, 11:44 AM
#8
Re: Simple Session Problem
it must be something in your code, because that is not normal behavior. I did a simple test myself, and was able to access session variables set on prior pages in the button click event.
You may want to set a break point on the button click event, and step through.. maybe add a watch to the session object to see the number of items it has in it...
-
Sep 1st, 2005, 12:13 PM
#9
Thread Starter
Hyperactive Member
Re: Simple Session Problem
Hi,
I just did another test, very simple.
On Page 1 i have:
Code:
private void Page_Load(object sender, System.EventArgs e)
{
Session["id"] = "1234";
Server.Transfer("page2.aspx");
}
On Page 2 i have:
Code:
private void Button1_Click(object sender, System.EventArgs e)
{
Label1.Text = Session["id"].ToString();
}
I get this error:
Code:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
If access the session in my page_load then this works
Code:
private void Page_Load(object sender, System.EventArgs e)
{
Label1.Text = Session["id"].ToString();
}
Last edited by modernthinker; Sep 1st, 2005 at 12:19 PM.
-
Sep 1st, 2005, 12:22 PM
#10
Re: Simple Session Problem
weird.. here is my code
VB Code:
'page1
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Session("id") = "1234"
Server.Transfer("WebForm2.aspx")
End Sub
'page2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = Session("id").ToString
End Sub
it works fine, Label1 reads 1234. I know im using VB and you are using C#, but I dont see how that should have any effect. I also don't know how it could be a setting since you said it works in the form load, but not the button click...
-
Sep 1st, 2005, 12:37 PM
#11
Thread Starter
Hyperactive Member
Re: Simple Session Problem
Hey, thanks for posting your code. I agree it shouldn't make a difference which language is being used. You have written the same as me and your one works. I have my page's enableSessionState set to true as well.
I really haven't got a clue whats happening here. So if this simple test isn't working for me then my original code isn't at fault, something is wrong elsewhere. I mean this test is soo simple i have checked through every line and can not find any bugs?
I really need this to work, i am passing my session variables my button submission so its paramount that i can get it working.
-
Sep 1st, 2005, 12:48 PM
#12
Re: Simple Session Problem
I just created a C# project and used your code and it worked as well, just as it did for my VB code.
So now I really don't know what it could be. Either a VS setting or maybe an IIS setting...
-
Sep 1st, 2005, 01:01 PM
#13
Thread Starter
Hyperactive Member
Re: Simple Session Problem
Thanks, now thats interesting.
So you have got it to work in both languages. I have tried the code on two seperate machines and i get the same error. I will upload my code to a web server and see it works online, this should be interesting.
-
Sep 1st, 2005, 01:24 PM
#14
Re: Simple Session Problem
you can upload me your project if you like and I can run it on my IIS since I know my test app worked
-
Sep 1st, 2005, 01:31 PM
#15
Thread Starter
Hyperactive Member
Re: Simple Session Problem
I just uploaded the test pages up to my webserver, i got the same error. Click here to see:
http://www.modernthinker.co.uk/test/...ssSession.aspx
passSession.aspx codebehind
Code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace Testing
{
/// <summary>
/// Summary description for makeSession.
/// </summary>
public class makeSession : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
Session["id"] = "1234";
Server.Transfer("accessSession.aspx");
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
accessSession.aspx codebehind:
Code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace Testing
{
/// <summary>
/// Summary description for accessSession.
/// </summary>
public class accessSession : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
Label1.Text = Session["id"].ToString();
}
}
}
Can you see anything different in your c#code?
-
Sep 1st, 2005, 01:43 PM
#16
Re: Simple Session Problem
no everything looks the same to me.. if you want, upload your simple test app and I will load it up here and give it a test
-
Sep 1st, 2005, 01:46 PM
#17
Re: Simple Session Problem
also what does your web.config look like?
-
Sep 1st, 2005, 01:55 PM
#18
Thread Starter
Hyperactive Member
Re: Simple Session Problem
my webconfig file:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<!-- DYNAMIC DEBUG COMPILATION
Set compilation debug="true" to enable ASPX debugging. Otherwise, setting this value to
false will improve runtime performance of this application.
Set compilation debug="true" to insert debugging symbols (.pdb information)
into the compiled page. Because this creates a larger file that executes
more slowly, you should set this value to true only when debugging and to
false at all other times. For more information, refer to the documentation about
debugging ASP.NET files.
-->
<compilation
defaultLanguage="c#"
debug="true"
/>
<!-- CUSTOM ERROR MESSAGES
Set customErrors mode="On" or "RemoteOnly" to enable custom error messages, "Off" to disable.
Add <error> tags for each of the errors you want to handle.
"On" Always display custom (friendly) messages.
"Off" Always display detailed ASP.NET error information.
"RemoteOnly" Display custom (friendly) messages only to users not running
on the local Web server. This setting is recommended for security purposes, so
that you do not display application detail information to remote clients.
-->
<customErrors
mode="RemoteOnly"
/>
<!-- AUTHENTICATION
This section sets the authentication policies of the application. Possible modes are "Windows",
"Forms", "Passport" and "None"
"None" No authentication is performed.
"Windows" IIS performs authentication (Basic, Digest, or Integrated Windows) according to
its settings for the application. Anonymous access must be disabled in IIS.
"Forms" You provide a custom form (Web page) for users to enter their credentials, and then
you authenticate them in your application. A user credential token is stored in a cookie.
"Passport" Authentication is performed via a centralized authentication service provided
by Microsoft that offers a single logon and core profile services for member sites.
-->
<authentication mode="Windows" />
<!-- AUTHORIZATION
This section sets the authorization policies of the application. You can allow or deny access
to application resources by user or role. Wildcards: "*" mean everyone, "?" means anonymous
(unauthenticated) users.
-->
<authorization>
<allow users="*" /> <!-- Allow all users -->
<!-- <allow users="[comma separated list of users]"
roles="[comma separated list of roles]"/>
<deny users="[comma separated list of users]"
roles="[comma separated list of roles]"/>
-->
</authorization>
<!-- APPLICATION-LEVEL TRACE LOGGING
Application-level tracing enables trace log output for every page within an application.
Set trace enabled="true" to enable application trace logging. If pageOutput="true", the
trace information will be displayed at the bottom of each page. Otherwise, you can view the
application trace log by browsing the "trace.axd" page from your web application
root.
-->
<trace
enabled="false"
requestLimit="10"
pageOutput="false"
traceMode="SortByTime"
localOnly="true"
/>
<!-- SESSION STATE SETTINGS
By default ASP.NET uses cookies to identify which requests belong to a particular session.
If cookies are not available, a session can be tracked by adding a session identifier to the URL.
To disable cookies, set sessionState cookieless="true".
-->
<sessionState
mode="InProc"
stateConnectionString="tcpip=127.0.0.1:42424"
sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes"
cookieless="false"
timeout="20"
/>
<!-- GLOBALIZATION
This section sets the globalization settings of the application.
-->
<globalization
requestEncoding="utf-8"
responseEncoding="utf-8"
/>
</system.web>
</configuration>
-
Sep 1st, 2005, 02:23 PM
#19
Thread Starter
Hyperactive Member
Re: Simple Session Problem
kleinma,
I have been very very stupid!!! My apolgoies in advance! The problem was my ZoneAlarm, once i completely shut down ZA it worked.
Man i feel soo dumb.
Thanks for all your feedback. I'm now off to put a hammer to my head!
-
Sep 1st, 2005, 02:25 PM
#20
Re: Simple Session Problem
hahaha... well happy to hear it was that i guess.. because i was beginning to think you might need a reinstall or something..
but how did it work in the page load event, but not in the button click.. that is still odd no?
-
Sep 1st, 2005, 02:34 PM
#21
Thread Starter
Hyperactive Member
Re: Simple Session Problem
Thats right, this is why initially i didn't think it was a ZA issue so i didn't bother shutting it down. When I did shut down ZA on both of my machines the code works fine, if i start it back up then it only works in the Page_Load event. Why, god knows. Thanks again.
-
Sep 1st, 2005, 03:33 PM
#22
Re: Simple Session Problem [RESOLVED]
maybe in the page load the session is still cached or something but after that point it gets wiped out and is kept in a place that zone alarm blocks..
just a guess, but hell why not..
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|