Can't get a new Session Variable to pass into a method (used Session, TextField and QueryString)

Hi Everyone,

 I have been battling with this for about 18 hours now and coudl use some help...

 I have a form that inserts into a DB table and then calls a method that makes a query to another table to create a Dataset for insert into a third table. ( I haven't done the third table insert yet but first things first.) Step 1 works fine. Step two is the problem. When I call the method I pass in two variables to get  return it will only work if I call an OLD Session variable. The session vaiable i set in the onSelectedChange event won't pass into the parameters... I also tried using queryString and got the same results.

The username is stored as a sesion Variable and if i use that one it works. I alos tried to get the value of the selected item as well and that didn't work either. I can wrote these values to the page using response.Write but it won't go into the method.

Here is the error Exception Details: System.Data.ConstraintException: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.

When I test this DataSet in the DesignView - preview data of Visual Studio I get the correct results by manaully entereing the parameters.

If I use ANY other type of value it doesn't work. I am a newbie and must be making a basic error. Any help you could offer would be greatly appreciated.

Thanks...

Here is the code:

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

 

public partial class admin_BoardSeat_add : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

String BoardNameReferrer = Request.QueryString["BOARD"];Session["sVarSelectedBoard"] = BoardNameReferrer;

lblBoardName_R.Text = BoardNameReferrer;

frmBoardName.Text = Request.QueryString["BOARD"];

MembershipUser user;

 

user =
Membership.GetUser();

frmCreatedBy.Text = user.ToString();

if (IsPostBack)

{

GridView1.DataBind(); //Refresh the grid

}

}

public void btnAdd_Click(object sender, EventArgs e)

{

 

SqlDataSource1.InsertParameters[
"BOARD"].DefaultValue = frmBoardName.Text;

SqlDataSource1.InsertParameters["USER_ID"].DefaultValue = ddlUserName.Text;

SqlDataSource1.InsertParameters["NUMBER"].DefaultValue = ddlNumber.Text;

SqlDataSource1.InsertParameters["created_by"].DefaultValue = frmCreatedBy.Text;

SqlDataSource1.InsertParameters["rec_status_flag"].DefaultValue = ddlRec_status_flag.Text;try {

SqlDataSource1.Insert();

frmBoardName.Text = "";

ddlUserName.Text = "";

ddlNumber.Text = "";

frmCreatedBy.Text = "";ddlRec_status_flag.Text = "";

}

catch (Exception ex){

lblError.Text = "An exception occurred. " +

"The seat was not entered. Please try again <br /> <br />" +

"Message: " + ex.Message;

}

frmBoardName.Text = Request.QueryString[
"BOARD"];

frmCreatedBy.Text = Session["sVarUserName"].ToString(); // may want to re-write this to set it to the membership.getUser() like above

Session["sVar_ddlUsername"] = ddlUserName.Text;

 

if (IsPostBack)

{

GridView1.DataBind(); //Refresh the grid view withthe new seat entered

BoardSeat_onEnter(); // calls the function which inserts the board name into the employee score report record

// where the employee directorate is equal to board directorate.

}

}

public void BoardSeat_onEnter(string userId2)

{

// query records and insert

string directorateFilter;directorateFilter = Request.QueryString["DIRECTORATE"];

 

//string userId;

//userId = Request.QueryString["SEATUSER"];

//userId = Session["sVarUsername"].ToString(); // ---- THIS WORKS !!!!!

//userId = Request.QueryString["SEATUSER"];

//userId = Session["sVar_ddlUsername"].ToString();

//userId = "testor4";

//System.Web.HttpContext.Current.Session

//string User_Id = HttpContext.Current.Session["sVar_ddlUsername"].ToString();

//User_Id = HttpContext.Current.Session["sVar_ddlUsername"].ToString();

//Response.Write(" --- " + Request.QueryString["SEATUSER"].ToString());

string userId = "admin"; // works!!!!!!! this is the hard coded value of the current user ( sVarUsername) WORKS

Response.Write("<br /> userId: -- " + userId + "<br />");

SeatEmployInsertTableAdapters.EMPLOYEEseatInsertTableAdapter SeatEmpDS = new SeatEmployInsertTableAdapters.EMPLOYEEseatInsertTableAdapter();

SeatEmployInsert.EMPLOYEEseatInsertDataTable SeatEmployeesLIST;

SeatEmployeesLIST = SeatEmpDS.GetSeatEmployeeInsert(directorateFilter, userId);

foreach (SeatEmployInsert.EMPLOYEEseatInsertRow SeatEmplRecInsert in SeatEmployeesLIST)

{

Response.Write(
"<br /> Seat Info = : " + SeatEmplRecInsert.SEAT_ID);

Response.Write (" --- " + SeatEmplRecInsert.ID);

// Now write the insert into Score table method or script.

//This works but  is wrong... I need to get the User_ID of the person just entered into the db. on the user id of the current user//

}

//lblError.Text =" - Seat inserted into Database";

//lblError.Text = directorateFilter;

//lblError.Text = ddlUserName.Text;

}

}

0
JeepGuy
8/16/2008 6:42:25 PM
asp.net.state-management 8807 articles. 0 followers. Follow

3 Replies
1028 Views

Similar Articles

[PageSpeed] 16

JeepGuy:

 I have a form that inserts into a DB table and then calls a method that makes a query to another table to create a Dataset for insert into a third table. ( I haven't done the third table insert yet but first things first.) Step 1 works fine. Step two is the problem. When I call the method I pass in two variables to get  return it will only work if I call an OLD Session variable. The session vaiable i set in the onSelectedChange event won't pass into the parameters... I also tried using queryString and got the same results.

 

 

i can't understand what you're saying about step1 or step2. could you specify where these steps are in your code? perhaps when you post use line numbers and specify the lines you are talking about?  but i'll take a stab at it.

 

it sounds like your BoardSeat_onEnter(string userId2) isn't getting a correct userId2 passed to it. seeing how you're able to store this variable as a session variable, probably you're dealing with a .NET page lifecycle problem. i would suggest studying up on this... really important info

but instead of using a session variable, why dont you just use a viewstate one?  if you don't know how to do this just reply. too many session variables and your memory can get out of control... many programmers never use them.


0
micahs
8/17/2008 11:43:02 AM

 Hi  Micahs,

 Thanks for the help. Sorry about being vague about the steps. (I didn't see the option for posting code with line numbers)

 Step 1 is the insert of the new board "seat" into the database and the post back to the page. Which is from the on load event through the posting of the form data to the data base and refreshing the grid view display on the same page. (Lines 1 - 86 in the code below)

Step two is the query to the database in the BoardSeat_onEnter method. (Lines 86 - 148 in the code below) This is the current problem.

Step three will be entering the data from step 2 into another database and I have not done it yet.
(I do have another basic question about this that I was going to put in another post - do I have to initialize an array of the returned values before I can insert them into the data base or can I take the values directly from the returned dataset (Fill) and insert them?)

 
The form I am using has drop down lists that will  won't work unless I turn view state off (in the form.) I don't know alot about view state so I did not add my  values to the views tate. The two books I have been studying have very little info on view state. Basically they say you write and read variables to  view state almost same as you would to the session.

Are you saying that the view state won't have the same lifecycle problems that the session does?
(What's really weird is that I tried to get this input value from a query string i manually added to the URL to see if that would work with no luck that is why I thought is was the way I was passing the value into the method.)
 

 Thanks again for helping me understand this important basic info...

Jim

 (In the mean time I am going to research lifecycle and view state into on this and other sites...) 

**** Code*** 


 

1    using System;
2
3 using System.Data;
4
5 using System.Configuration;
6
7 using System.Collections;
8
9 using System.Web;
10
11 using System.Web.Security;
12
13 using System.Web.UI;
14
15 using System.Web.UI.WebControls;
16 using System.Web.UI.WebControls.WebParts;
17
18
19
20 public partial class admin_BoardSeat_add : System.Web.UI.Page
21
22 {
23 protected void Page_Load(object sender, EventArgs e)
24
25 {
26 String BoardNameReferrer = Request.QueryString["BOARD"];Session["sVarSelectedBoard"] = BoardNameReferrer;
27
28 lblBoardName_R.Text = BoardNameReferrer;
29
30 frmBoardName.Text = Request.QueryString["BOARD"];
31 MembershipUser user;
32
33
34 user = Membership.GetUser();
35
36 frmCreatedBy.Text = user.ToString();
37 if (IsPostBack)
38
39 {
40
41 GridView1.DataBind(); //Refresh the grid
42
43 }
44
45 }
46 public void btnAdd_Click(object sender, EventArgs e)
47
48 {
49
50
51 SqlDataSource1.InsertParameters["BOARD"].DefaultValue = frmBoardName.Text;
52
53 SqlDataSource1.InsertParameters["USER_ID"].DefaultValue = ddlUserName.Text;
54 SqlDataSource1.InsertParameters["NUMBER"].DefaultValue = ddlNumber.Text;
55
56 SqlDataSource1.InsertParameters["created_by"].DefaultValue = frmCreatedBy.Text;
57 SqlDataSource1.InsertParameters["rec_status_flag"].DefaultValue = ddlRec_status_flag.Text;try {
58
59 SqlDataSource1.Insert();
60
61 frmBoardName.Text = "";
62 ddlUserName.Text = "";
63
64 ddlNumber.Text = "";
65 frmCreatedBy.Text = "";ddlRec_status_flag.Text = "";
66
67 }
68
69 catch (Exception ex){
70 lblError.Text = "An exception occurred. " +
71
72 "The seat was not entered. Please try again <br /> <br />" +
73 "Message: " + ex.Message;
74
75 }
76 frmBoardName.Text = Request.QueryString["BOARD"];
77
78 frmCreatedBy.Text = Session["sVarUserName"].ToString(); // may want to re-write this to set it to the membership.getUser() like above
79 Session["sVar_ddlUsername"] = ddlUserName.Text;
80
81
82 if (IsPostBack)
83
84 {
85
86 GridView1.DataBind(); //Refresh the grid view withthe new seat entered
87
88 BoardSeat_onEnter(); // calls the function which inserts the board name into the employee score report record
89
90 // where the employee directorate is equal to board directorate.

91
92 }
93
94 }
95 public void BoardSeat_onEnter(string userId2)
96
97 {
98
99 // query records and insert
100 string directorateFilter;directorateFilter = Request.QueryString["DIRECTORATE"];
101
102
103
104 //string userId;
105
106 //userId = Request.QueryString["SEATUSER"];
107
108 //userId = Session["sVarUsername"].ToString(); // ---- THIS WORKS !!!!!
109
110 //userId = Request.QueryString["SEATUSER"];
111
112 //userId = Session["sVar_ddlUsername"].ToString();
113
114 //userId = "testor4";
115
116 //System.Web.HttpContext.Current.Session
117
118 //string User_Id = HttpContext.Current.Session["sVar_ddlUsername"].ToString();
119
120 //User_Id = HttpContext.Current.Session["sVar_ddlUsername"].ToString();
121
122 //Response.Write(" --- " + Request.QueryString["SEATUSER"].ToString());

123
124 string userId = "admin"; // works!!!!!!! this is the hard coded value of the current user ( sVarUsername) WORKS
125 Response.Write("&lt;br /> userId: -- " + userId + "&lt;br />");
126
127 SeatEmployInsertTableAdapters.EMPLOYEEseatInsertTableAdapter SeatEmpDS = new SeatEmployInsertTableAdapters.EMPLOYEEseatInsertTableAdapter();
128 SeatEmployInsert.EMPLOYEEseatInsertDataTable SeatEmployeesLIST;
129
130 SeatEmployeesLIST = SeatEmpDS.GetSeatEmployeeInsert(directorateFilter, userId);
131 foreach (SeatEmployInsert.EMPLOYEEseatInsertRow SeatEmplRecInsert in SeatEmployeesLIST)
132
133 {
134 Response.Write("&lt;br /> Seat Info = : " + SeatEmplRecInsert.SEAT_ID);
135
136 Response.Write (" --- " + SeatEmplRecInsert.ID);
137
138 // Now write the insert into Score table method or script.
139
140 //This works but is wrong... I need to get the User_ID of the person just entered into the db. on the user id of the current user//

141
142 }
143
144 //lblError.Text =" - Seat inserted into Database";
145
146 //lblError.Text = directorateFilter;
147
148 //lblError.Text = ddlUserName.Text;
 
                      STEP THREE GOES HERE 
149
150 }
151
152 }
 

  
0
JeepGuy
8/17/2008 2:53:29 PM

 OK I got it.... I cheated and used a querystring, not as elegant but it works.

 Thanks for the feedback it sent me in the right direction.

 

Jim

 

0
JeepGuy
8/18/2008 10:55:46 AM
Reply:

Similar Artilces:

After a looong night managed to change Session mode from inProc to SQLServer but still can't get the session variables to be saved
Hi,  I ran my Shopping Cart project and somehow everything worked fine(while in inProc), session variables were saved fine. I made some changes to my project and since then keep getting null session variables, no matter what I do. It took me the whole night to figure out how to turn to SQLServer mode,  marked custom classes as 'Serializable' with [Serializable] before the class definition but everything's in vain. Getting the same damned 'nulls' instead of the saved objects. private void Page_Load(object sender, System.EventArgs e)         {...

I can't get no cache, I can't get no cache. 'Cause I try and I try and I try and, I can't get no, I can't get no cache.
I have fiddled out for days tinkering with the setting in about:config trying to get FireFox 12 to use the disk cache. I have NOT found the trick. This One Trick Pony ain't doing it like it used to up until recently. Pray tell anyone, What information can I share that will point a knowledgeable person to aid me in getting FF to disk.cache? I have also tried restarting FF with add-ons disabled, (There were NOT too many to do this to, so it was quick and easy) Here is my stab at trying to convey the information that MIGHT govern matters Using about:cache Informatio...

Re: I can't get no cache, I can't get no cache. ' Cause I try and I try and I try and, I can't get no, I can't get no cache.
<div>please com e see me about htis<br /> <br /> ------- Original Message f= rom the Global Relay Archive -------<br /> From: Hp &lt;ferd@farkel.net&gt;= <br /> To: "support-firefox@lists.mozilla.org" &lt;support-firefox@lists.mo= zilla.org&gt;<br /> Sent: Sun, 27 May 2012 18:30:18 -0700<br /> Subject: I = can't get no cache, I can't get no cache. 'Cause I try and I try and I try = and, I can't get no, I can't get no cache.<br /> <br /></div> <pre class=3D"gr-maex-body-pre&qu...

Can't get session variable...
Hello again!I've been trying to put up a shopping cart with a items list page and a second one (cart). When I hit the "buy" button from one item I get this error on "cart.aspx":"Object reference not set to an instance of an object" Line 5: Dim ds as DataSet Line 6: ds = CType(Session("myDataSet"), Dataset) Line 7: MyDataGrid.Datasource=ds.Tables("Catalogo").DefaultView Line 8: MyDataGrid.DataBind() Line 9: End Sub I have no clu where the error may come from so I'm gonna post both entire files code.item list.aspx ...

Two things,Why do I keep getting emails that I don't want. can't get rid of them? Why can't I get my email when I am out of town? Or can I?
Name: Ed Leech Email: ELCraftatzoominternetdotnet Product: Thunderbird Summary: Two things,Why do I keep getting emails that I don't want. can't get rid of them? Why can't I get my email when I am out of town? Or can I? Comments: I am getting frusted with all the emails coming in that I do not want, I am using the tools to get thme out but they keep coming. They just use different names or whatever. I have been thinking of just switching to something else but my business intrusts know this email and it is tooo confusing to change. Every time I go out of town on bu...

2010: Can't register - can't download the software - can't register until I have the software
I have active maintenance on Rad Studio: From: Vicky Rassmisaengthong [mailto:Vicky.Rassmisaengthong@EMBARCADERO.COM] Sent: Wednesday, February 25, 2009 1:58 PM To: rgrossman Cc: amer.supportadmin@codegear.com; Ashley Cosentino Subject: Software Assurance Support for Tech III Inc PO# Credit Card Dear Embarcadero Technologies Support Customer, Welcome! You have been registered as the primary contact on support account number AM####### Herewith we confirm your Embarcadero Technologies support agreement covering: Qty 1 RAD Studio Enterprise Named User licens...

How to pass value between aspx page ,popup page don't use session , Querystring
i need to pass(or get) value beteen page  and popup are opening But i don't want use session and Querystring because sometimes   the value was datatable or object    Here are different approaches. http://www.codeproject.com/aspnet/DataPassingBtPages.asp http://msdn2.microsoft.com/en-us/library/6c3yckfw(VS.80).aspx http://aspnet.4guysfromrolla.com/articles/020205-1.aspx Thanks, FarooqFarooq Kaiser, MCTS, MCPPlease Mark As Answer if my answer helped you.http://www.Fairnet.com http://aspnet.4guysfromrolla.com/articles/020205-1.aspx  this&nb...

Session passing variables Using Session ID
I am passing variables using session ID . When multiple people sign-on to the server, the session ID is passed corectly, but the name and user ID are for the person that signed in first. That is both people get the same information, when it should be customized for each. I am using IIS version 7. SendingHttpContext.Current.Session("Session_ID") = Session.SessionID HttpContext.Current.Session("StudentName") = FirstLastHttpContext.Current.Session("StudentNumber") = StudentNumber Response.Write(FirstLast) Response.Write(StudentNumber) Response.Redirect(&quo...

why this happening
Hi Guys, Look at the two codes, 1) public partial class StackHolders_Admin_GenSettings : System.Web.UI.Page{       GenSettings BOObj = new GenSettings();    protected void Page_Load(object sender, EventArgs e)    {       if (Session["admin"] == null || Session["Id"] == null)          {               ScriptManager.RegisterStartupScript(Page, typeof(Page), "SessionExp&q...

getting a new session id and losing session variables
prb: I have two pages in my app - one on redirect and the other as a new window - that get a new session id from every other page in the app (they both get the same session id) and obviously the session state variables are lost and, therefore, the pages do not look like they should.  All pages are within the same domain and virtual directory. 1. a redirect; session variables are lost; session id is different from the previous page; if you click the BACK button you return to the previous page and the session id is the same as before and the session variables are as they should be2. ...

Can't use Sessions
Hello everybody, I've got a problem with the use of sessions since this morning.Actually, I've created a class: Suscriber, this class has got ten properties I use to store data belonging to suscribers such as lastname, forname, id, mail, etc.On the LoggedIn event of my Login Control,  i fill this properties in order to get a Suscriber object containing the information i'll need further.Then I do: Session.Add("Sucriber",Suscriber)THIS WORKS WELLBut when i call the session item "Suscriber", I've got this error message (translated from french ...) :...

Can't get it to work. Can't get help.
Name: Tony Clarke Email: minionataondotat Product: Thunderbird Summary: Can't get it to work. Can't get help. Comments: Installed T'bird. Had to change defunct server that T'bird selected by default. Kept asking for passwords associated with previous email (Outlook Express ), when passwords entered then got error messages saying aon responded with 're-authentification failure'. Searched your knowledge base & help forums to no avail. Found them very user-unfriendly & ended up more confused than before. Would like to try T'bird, but I think ...

Unable to serialize the session state. Please note that non-serializable objects or MarshalByRef objects are not permitted when session state mode is 'StateServer' or 'SQLServer'
 Hi,We are trying to implement Network loading for our asp.net (1.1) application for which we are storing the session in State Server.Following is the error is thrown while page is rendered when ever is a DTO is stored in to SEssion in any of the page.Unable to serialize the session state. Please note that non-serializable objects or MarshalByRef objects are not permitted when session state mode is 'StateServer' or 'SQLServer' Following the code I written for DTO (C# Asp.net 1.1).     using System;    using System.Runtime.Serializa...

Can't logon to a new session
Hello all, Ever since openSUSE 11 I can't login to a new session when using ctrl+alt+f.. I just get a black screen with nowhere to type. I'm not sure how these console logins are called so its hard for me to do a successful search on it. Since I'm messing around a lot I sometimes make X hang on a service/application. Usually I logged in to this new session and killed an app. I can't do that anymore and ctrl+alt+backspace is so crude :) -- Ricochet ------------------------------------------------------------------------ [QUOTE=Ricochet] Hello all, ...

Web resources about - Can't get a new Session Variable to pass into a method (used Session, TextField and QueryString) - asp.net.state-management

Resources last updated: 11/27/2015 6:13:17 PM