case statement

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"]);              
1jawsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Carlos VillegasFull Stack .NET DeveloperCommented:
Hello, can you restate your question?
0
Bill NolanOwner, Lead Technology ProgrammerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bill NolanOwner, Lead Technology ProgrammerCommented:
(Note: In the code section above, I meant to delete rows 14 and up.)

Am I able to edit this somewhere?
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Carlos VillegasFull Stack .NET DeveloperCommented:
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
Bill NolanOwner, Lead Technology ProgrammerCommented:
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
1jawsAuthor Commented:
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
Carlos VillegasFull Stack .NET DeveloperCommented:
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
Bill NolanOwner, Lead Technology ProgrammerCommented:
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
1jawsAuthor Commented:
OK.. I think I am going to leave my code as it is... thanks all for the help...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.