ASP.Net/C# - Dynamic Where Clause Should be Easy

Hello all.  I have a dynamic SQL string I am creating and the Where Clause has an IN in the statement.  What I need to do is some of the parts in this IN statement can be blank "".  I need to omit those in the IN statement and only have the IN statement run with parts that have a value.  For example,

Where part_num IN ('" + partNum1 + "','" + partNum2 + "','" + partNum3 + "','" + partNum4 + "','" + partNum5 + "')

partNum4 could be blank so I need to leave that out.  Any idea on how to easily build this with something clean.  Thanks all.
sbornstein2Asked:
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.

GENTPCommented:
How about puting the parts in an array and looping through it, testing each one for blank?
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
sbornstein2Author Commented:
Yep that is what I can do I actually already have an array for it.  I am just trying to find out the cleanest least code to now check for each one being blank and building just the inner part of the IN statement ().
0
GENTPCommented:
I'd just basically loop through, if the partnum isn't null, concatenate it into a string with a comma on the end of it. so using the string sqlIn:

sqlIn+=partNum.ToString() + ",";

Then after the loop I would check if the entire sqlIn was null or not. If it's not, strip off the last character (that trailing comma, you never know how many parts will be in there) and then stick the sqlIn string right into the brackets of your original sql string.

It sounds like you're probably already doing this, or something very similar though.
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
ASP.NET

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.