?
Solved

case statement

Posted on 2011-10-04
9
Medium Priority
?
322 Views
Last Modified: 2012-06-22
how can I use case statement on this code?

 comm.Parameters.Add(new SqlParameter("@id", SqlDbType.Int));
                if (Convert.ToInt32(Request.QueryString["a"]) != 0)                
                    comm.Parameters["@id"].Value = Convert.ToInt32(Request.QueryString[a"]);                
                if (Convert.ToInt32(Request.QueryString["b"]) != 0)                
                    comm.Parameters["@id"].Value = Convert.ToInt32(Request.QueryString["b"]);                
                if (Convert.ToInt32(Request.QueryString["c"]) != 0)                
                    comm.Parameters["@id"].Value = Convert.ToInt32(Request.QueryString["c"]);              
0
Comment
Question by:1jaws
  • 4
  • 3
  • 2
9 Comments
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 36913588
Hello, can you restate your question?
0
 
LVL 7

Accepted Solution

by:
Bill Nolan earned 1336 total points
ID: 36913591
I realize this doesn't answer your question, however:

A switch statement is not optimal for your logic.  Your action is the same in each case, depending only on the index.  Instead, simply remove the redundant calls by looping through your indeces (a-z?), and set an integer for each.  E.g.:


int value = 0;
string sIndex = "";

for (i = 0; i < numIndeces; i++)
{
   sIndex = GetYourIndexString(i);
   value = Convert.ToInt32(Request.QueryString[sIndex]);

   if (value != 0)
   {
      comm.Parameters["@id"].Value = value;
   }
}

// switch/case version:

int value = 0;

switch (
for (i = 0; i < numIndeces; i++)
{
   sIndex = GetYourIndexString(i);
   value = Convert.ToInt32(Request.QueryString[sIndex]);

   if (value != 0)
   {
      comm.Parameters["@id"].Value = value;
   }
}

Open in new window

0
 
LVL 7

Assisted Solution

by:Bill Nolan
Bill Nolan earned 1336 total points
ID: 36913608
(Note: In the code section above, I meant to delete rows 14 and up.)

Am I able to edit this somewhere?
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 36913622
I see, based in the Slimfinger idea, maybe you want something like this:
string myParams = "abc";
foreach (char p in myParams)
{
     if (Convert.ToInt32(Request.QueryString[p.ToString()]) != 0)
     {                
          comm.Parameters["@id"].Value = Convert.ToInt32(Request.QueryString[p.ToString()]);
           break;
     }
}

Open in new window

0
 
LVL 7

Expert Comment

by:Bill Nolan
ID: 36913629
Btw, I'm not sure what you mean to do exactly, but in your code example above, if more than one of those QueryString values are present you will only save the last one.  If that's what you want, then you should use if/else.  Otherwise, you have to add new parameters.
0
 

Author Comment

by:1jaws
ID: 36913651
my QueryString will be sometimes comes from other page like  Request.QueryString[a"]  or Request.QueryString[b"], or Request.QueryString[something"] .... I just want to be able to not keep repeting the lines I did , I was asking is it anyway to use case statement that I wont be able to keep repeating this lines like that  if (Convert.ToInt32(Request.QueryString["a"]) != 0)     ...  btw.. this a, bi, ... is just a example, will come a different strings name instead of a, b,....
0
 
LVL 17

Assisted Solution

by:Carlos Villegas
Carlos Villegas earned 664 total points
ID: 36913672
So you expect only one QueryString parameter no matter the name? if is in that way you can use an index, like Request.QueryString[0] ?
0
 
LVL 7

Expert Comment

by:Bill Nolan
ID: 36913678
Switch/case is not the best method when all cases have a consistent logic.  You need to:

a) find the best way to move through the indeces (if in some manageable or enumerable form, then yv989c's type of suggestion is good; if not, you have to figure it out - that is why it is abstracted in my example).
b) change your logic to stop after you assign the SQL param, or add a new param if you need to support more than one.
0
 

Author Comment

by:1jaws
ID: 36913680
OK.. I think I am going to leave my code as it is... thanks all for the help...
0

Featured Post

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month14 days, 2 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question