If statements?

  • Thread starter Thread starter bthumber
  • Start date Start date
B

bthumber

My application has a group of four "if" statements. Example:
if(Session["a"] != null)
do something;
if(Session["b"] != null)
do something;
if(Session["c"] != null)
do something;
if(Session["d"] != null)
do something;
Each Session is independent of the other session and each session have
different functionality. The user (on a different page) chooses a device
which is one of the four sessions. My problem is if the user chooses the
last session, Session["d"] then his/hers next choice is any of the other
session (a, b, or c) the program will not change sessions. Session["d"] will
rarely be picked if ever, but Murphy's law... My question is 1) is there a
better way to do this and how? 2) Is there a way to update my "ifs" and how?
Some things I tried:
Session.remove, clear, abondon(). It didn't work because the program starts
with a session to get the user's name. I've tried a switch statement but that
caused more problems that is solved.
 
My problem is if the user chooses the
last session, Session["d"] then his/hers next choice is any of the other
session (a, b, or c) the program will not change sessions.

You probably miss the idea of having a unique choice. If I understood
this correct you have only one choice user has to make. In this case
you don't need to use different values (a, b, c, d), you need to have
just one variable/object where you would save the current state.

For example, if you have a RadioButtonList control where user make his/
her choice

<asp:RadioButtonList
id="RadioButtonList1"
runat="server">
<asp:ListItem value="1">Blue</asp:ListItem>
<asp:ListItem value="2">Red</asp:ListItem>
<asp:ListItem value="3">Green</asp:ListItem>
<asp:ListItem value="4">Purple</asp:ListItem>
</asp:RadioButtonList>

then you would save the choice

void btnSubmit_Click(object sender, EventArgs e)
{
Session["choice"] = RadioButtonList1.SelectedItem.Value;
}

then you would have your IF..THEN case

string choice = (string) Session["choice"];

if (choice == "1")
do something;

if (choice == "2")
do something;

.....

or using switch case

string choice = (string) Session["choice"];
switch (choice )
{
case "1":
do something;
break;

case "2":

....

Hope this helps
 
bthumber said:
My application has a group of four "if" statements. Example:
if(Session["a"] != null)
do something;
if(Session["b"] != null)
do something;
if(Session["c"] != null)
do something;
if(Session["d"] != null)
do something;
Each Session is independent of the other session and each session have
different functionality. The user (on a different page) chooses a device
which is one of the four sessions. My problem is if the user chooses the
last session, Session["d"] then his/hers next choice is any of the other
session (a, b, or c) the program will not change sessions. Session["d"] will
rarely be picked if ever, but Murphy's law... My question is 1) is there a
better way to do this and how? 2) Is there a way to update my "ifs" and how?
Some things I tried:
Session.remove, clear, abondon(). It didn't work because the program starts
with a session to get the user's name. I've tried a switch statement but that
caused more problems that is solved.

You are using the term "session" very loosely, which is prone to
confusion are there are different kinds of sessions...

1. The browser session, which lasts as long as the browser instance runs.

2. The server session, which lasts until the Session object times out or
is abandoned.

3. The Session object, which the server uses to hold session data.

What you are talking about is actually none of these, but session
variables that you store in the Session object.

If you assign a value to two of the session variables, your code will
actually execute the code for both choises. If you only see the effect
of the last one, that means that the code for the last choise overwrites
the effect of the other choise.

If you want it to be a single choise, you should use a single session
variable where the value determines the choise, as Alexey suggested.
 
Alexey Smirnov said:
My problem is if the user chooses the
last session, Session["d"] then his/hers next choice is any of the other
session (a, b, or c) the program will not change sessions.

You probably miss the idea of having a unique choice. If I understood
this correct you have only one choice user has to make. In this case
you don't need to use different values (a, b, c, d), you need to have
just one variable/object where you would save the current state.

For example, if you have a RadioButtonList control where user make his/
her choice

<asp:RadioButtonList
id="RadioButtonList1"
runat="server">
<asp:ListItem value="1">Blue</asp:ListItem>
<asp:ListItem value="2">Red</asp:ListItem>
<asp:ListItem value="3">Green</asp:ListItem>
<asp:ListItem value="4">Purple</asp:ListItem>
</asp:RadioButtonList>

then you would save the choice

void btnSubmit_Click(object sender, EventArgs e)
{
Session["choice"] = RadioButtonList1.SelectedItem.Value;
}

then you would have your IF..THEN case

string choice = (string) Session["choice"];

if (choice == "1")
do something;

if (choice == "2")
do something;

.....

or using switch case

string choice = (string) Session["choice"];
switch (choice )
{
case "1":
do something;
break;

case "2":

....

Hope this helps
Alexey
My application uses a dropdownlist box, your saying I could store the value
of the users choice in a Session object. I think I understand your example,
thanks.
 
Alexey Smirnov said:
On Jan 25, 6:17 am, bthumber <[email protected]>
wrote:
My problem is if the user chooses the
last session, Session["d"] then his/hers next choice is any of the other
session (a, b, or c) the program will not change sessions.
You probably miss the idea of having a unique choice. If I understood
this correct you have only one choice user has to make. In this case
you don't need to use different values (a, b, c, d), you need to have
just one variable/object where you would save the current state.
For example, if you have a RadioButtonList control where user make his/
her choice
<asp:RadioButtonList
    id="RadioButtonList1"
    runat="server">
    <asp:ListItem value="1">Blue</asp:ListItem>
    <asp:ListItem value="2">Red</asp:ListItem>
    <asp:ListItem value="3">Green</asp:ListItem>
    <asp:ListItem value="4">Purple</asp:ListItem>
</asp:RadioButtonList>
then you would save the choice
void btnSubmit_Click(object sender, EventArgs e)
    {
        Session["choice"] = RadioButtonList1.SelectedItem.Value;
    }
then you would have your IF..THEN case
string choice = (string) Session["choice"];
if (choice == "1")
do something;
if (choice == "2")
do something;

or using switch case
string choice = (string) Session["choice"];
switch (choice )
 {
 case "1":
do something;
break;
case "2":

Hope this helps

Alexey
My application uses a dropdownlist box, your saying I could store the value
of the users choice in a Session object. I think I understand your example,
thanks.

Yes, it can be done in many ways. Let me know if you have any
questions.
 
If I am understanding you, the user can only have session a, b, c or d and
not all of them? If this is correct, use a single Session value.

Example:

<asp:DropDownList id="langaugeDropDown">
<asp:ListItem value="English"/>
<asp:ListItem value="Spanish"/>
<asp:ListItem value="German"/>
<asp:ListItem value="French"/>
</asp:DropDownList>

Session["language"] = languageDropDown.SelectedItem.Text;

If a user can have more than one at a time, then you should check values
first and then do an action based on what you have found. This is a simple
validate and act pattern.

Does this make sense, or am I misunderstanding your goal?

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Blog:
http://feeds.feedburner.com/GregoryBeamer

********************************************
| Think outside the box! |
********************************************
 
Yes, you get it, the goal is one session at a time.
Cowboy (Gregory A Beamer) said:
If I am understanding you, the user can only have session a, b, c or d and
not all of them? If this is correct, use a single Session value.

Example:

<asp:DropDownList id="langaugeDropDown">
<asp:ListItem value="English"/>
<asp:ListItem value="Spanish"/>
<asp:ListItem value="German"/>
<asp:ListItem value="French"/>
</asp:DropDownList>

Session["language"] = languageDropDown.SelectedItem.Text;

If a user can have more than one at a time, then you should check values
first and then do an action based on what you have found. This is a simple
validate and act pattern.

Does this make sense, or am I misunderstanding your goal?

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Blog:
http://feeds.feedburner.com/GregoryBeamer

********************************************
| Think outside the box! |
********************************************
bthumber said:
My application has a group of four "if" statements. Example:
if(Session["a"] != null)
do something;
if(Session["b"] != null)
do something;
if(Session["c"] != null)
do something;
if(Session["d"] != null)
do something;
Each Session is independent of the other session and each session have
different functionality. The user (on a different page) chooses a device
which is one of the four sessions. My problem is if the user chooses the
last session, Session["d"] then his/hers next choice is any of the other
session (a, b, or c) the program will not change sessions. Session["d"]
will
rarely be picked if ever, but Murphy's law... My question is 1) is there a
better way to do this and how? 2) Is there a way to update my "ifs" and
how?
Some things I tried:
Session.remove, clear, abondon(). It didn't work because the program
starts
with a session to get the user's name. I've tried a switch statement but
that
caused more problems that is solved.
 
The code for the switch works but when I run the last one the problem get
stuck. Here is the code for submit button:

protected void btnSubmit_Click1(object sender, EventArgs e)
{
// Session cookie to be carried over to Conformation.aspx
lblClassification.Text = DropDownList1.SelectedItem.ToString();
Session["Classification"] = lblClassification.Text;

if (DropDownList2.SelectedIndex == 1)
{
Session["choice"] = DropDownList2.SelectedItem.Value;
lblPage.Text = Convert.ToString(DropDownList2.SelectedIndex == 1);
Session["nPage"] = lblPage.Text;

Response.Redirect("Network.aspx");
}
else if (DropDownList2.SelectedIndex == 2)
{
Session["choice"] = DropDownList2.SelectedItem.Value;
lblPage.Text = Convert.ToString(DropDownList2.SelectedIndex == 2);
Session["sPage"] = lblPage.Text;

Response.Redirect("Server.aspx");
}
else if (DropDownList2.SelectedIndex == 3)
{
Session["choice"] = DropDownList2.SelectedItem.Value;
lblPage.Text = Convert.ToString(DropDownList2.SelectedIndex == 3);
Session["sdPage"] = lblPage.Text;

Response.Redirect("Storage.aspx");
}
else if (DropDownList2.SelectedIndex == 4)
{
DropDownList2.Enabled = false;
Session["choice"] = DropDownList2.SelectedItem.Value;
lblPage.Text = Convert.ToString(DropDownList2.SelectedIndex == 4);
Session["wsPage"] = lblPage.Text;

Response.Redirect("Workstation.aspx");
}
else
Session.Clear();
}

And here is the code for the switch:

public void MainSessionData()
{
string choice = (string)Session["choice"];

switch (choice)
{
case "1":
if (Session["nPage"] != null)
{
// If user chooses (Network) Network.aspx executes the
below code
if (Session["Classification"] != null)
{
ClassificationType.Text =
Session["Classification"].ToString();
if (Session["Classification"].Equals("Unclassified"))
lblU.Text = "U".ToLower();
if (Session["Classification"].Equals("Classified"))
lblU.Text = "C".ToLower();
if (Session["Classification"].Equals("Secret"))
lblU.Text = "S".ToLower();
if (Session["Classification"].Equals("Top Secret"))
lblU.Text = "T".ToLower();
}
lblNetwork.Text = Session["nPage"].ToString();
UpdatePanel1.Visible = true;
nPassSessionData();
UpdatePanel2.Visible = false;
UpdatePanel3.Visible = false;
UpdatePanel4.Visible = false;
}
break;
case "2":
if (Session["sPage"] != null)
{
if (Session["Classification"] != null)
{
ClassificationType.Text =
Session["Classification"].ToString();
if (Session["Classification"].Equals("Unclassified"))
lblC.Text = "U".ToLower();
if (Session["Classification"].Equals("Classified"))
lblC.Text = "C".ToLower();
if (Session["Classification"].Equals("Secret"))
lblC.Text = "S".ToLower();
if (Session["Classification"].Equals("Top Secret"))
lblC.Text = "TS".ToLower();
}
// If user chooses (Serve) Server.aspx executes the
below code
if (Session["Classification"] != null)
{
lblClass.Text = Session["Classification"].ToString();
}
lblServer.Text = Session["sPage"].ToString();
UpdatePanel2.Visible = true;
sPassSessionData();
UpdatePanel1.Visible = false;
UpdatePanel3.Visible = false;
UpdatePanel4.Visible = false;
}
break;
case "3":
if (Session["sdPage"] != null)
{
if (Session["Classification"] != null)
{
ClassificationType.Text =
Session["Classification"].ToString();
if (Session["Classification"].Equals("Unclassified"))
lblS.Text = "U".ToLower();
if (Session["Classification"].Equals("Classified"))
lblS.Text = "C".ToLower();
if (Session["Classification"].Equals("Secret"))
lblS.Text = "S".ToLower();
if (Session["Classification"].Equals("Top Secret"))
lblS.Text = "TS".ToLower();
}
// If user chooses (Storage Devises) Storage.aspx
executes the below code
if (Session["Classification"] != null)
{
lblsdClass.Text =
Session["Classification"].ToString();
}
lblStorage.Text = Session["sdPage"].ToString();
UpdatePanel3.Visible = true;
sdPassSessionData();
UpdatePanel1.Visible = false;
UpdatePanel2.Visible = false;
UpdatePanel4.Visible = false;
}
break;
case "4":
if (Session["wsPage"] != null)
{
if (Session["Classification"] != null)
{
ClassificationType.Text =
Session["Classification"].ToString();
if (Session["Classification"].Equals("Unclassified"))
lblWS.Text = "U".ToLower();
if (Session["Classification"].Equals("Classified"))
lblWS.Text = "C".ToLower();
if (Session["Classification"].Equals("Secret"))
lblWS.Text = "S".ToLower();
if (Session["Classification"].Equals("Top Secret"))
lblWS.Text = "TS".ToLower();
}
// If user choose none of the above program defaults to
workstation
if (Session["Classification"] != null)
{
lblwsClass.Text =
Session["Classification"].ToString();
}
lblWorkstation.Text = Session["wsPage"].ToString();
UpdatePanel4.Visible = true;
wsPassSessionData();
UpdatePanel1.Visible = false;
UpdatePanel2.Visible = false;
UpdatePanel3.Visible = false;
}
break;
default:
break;
}

The program hang if I try to do some after I choose "Workstaion"

Alexey Smirnov said:
Alexey Smirnov said:
On Jan 25, 6:17 am, bthumber <[email protected]>
wrote:
My problem is if the user chooses the
last session, Session["d"] then his/hers next choice is any of the other
session (a, b, or c) the program will not change sessions.
You probably miss the idea of having a unique choice. If I understood
this correct you have only one choice user has to make. In this case
you don't need to use different values (a, b, c, d), you need to have
just one variable/object where you would save the current state.
For example, if you have a RadioButtonList control where user make his/
her choice
<asp:RadioButtonList
id="RadioButtonList1"
runat="server">
<asp:ListItem value="1">Blue</asp:ListItem>
<asp:ListItem value="2">Red</asp:ListItem>
<asp:ListItem value="3">Green</asp:ListItem>
<asp:ListItem value="4">Purple</asp:ListItem>
</asp:RadioButtonList>
then you would save the choice
void btnSubmit_Click(object sender, EventArgs e)
{
Session["choice"] = RadioButtonList1.SelectedItem.Value;
}
then you would have your IF..THEN case
string choice = (string) Session["choice"];
if (choice == "1")
do something;
if (choice == "2")
do something;

or using switch case
string choice = (string) Session["choice"];
switch (choice )
{
case "1":
do something;
break;
case "2":

Hope this helps

Alexey
My application uses a dropdownlist box, your saying I could store the value
of the users choice in a Session object. I think I understand your example,
thanks.

Yes, it can be done in many ways. Let me know if you have any
questions.
 
Hi Mark

Did you see the code I posted Alexey? The Switch handles things much better
I still have the problem of if the user chooses the last one or Workstation
they can not go back and choose to enter some other type of device without
having to start the program over. Any input, work around idea :)
 
Back
Top