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

Posted on 2006-04-20
Last Modified: 2012-06-27
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.
Question by:sbornstein2
    LVL 5

    Accepted Solution

    How about puting the parts in an array and looping through it, testing each one for blank?

    Author Comment

    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 ().
    LVL 5

    Expert Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…

    794 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now