search various database columns based on user's input RRS feed

  • Question

  • User-409574825 posted

    I have five textboxes(tb1, tb2, ... tb5) on the page, each textbox represent a column in the database table.

    I need to search the database table based on the text in the textboxes. User can either enter text in all five textboxes or none.

    What's the best way to do a search?

    I am thinking for one textbox(tb1), depends on if tb1.text is empty or not, I need to do 2 searches

          select * from databaseTable1
          select * from databaseTable1 where col1 == tb1.Text

    for two textboxes(tb1 & tb2), depends on if tb1.text and/or tb2.text are empty or not, there are 4 combinations, so I need to do 4 searches

    if(string.IsNullorEmpty(tb1.Text) && string.IsNullorEmpty(tb2.Text))     //both textboxes are empty
          select * from databaseTable1
    else if(!string.IsNullorEmpty(tb1.Text) && string.IsNullorEmpty(tb2.Text))     //tb2 is empty
          select * from databaseTable1 where col1 == tb1.Text
    else if(string.IsNullorEmpty(tb1.Text) && !string.IsNullorEmpty(tb2.Text))     //tb1 is empty
          select * from databaseTable1 where col2 == tb2.Text
    else if(!string.IsNullorEmpty(tb1.Text) && !string.IsNullorEmpty(tb2.Text))    //both not empty
          select * from databaseTable1 where col1 == tb1.Text and col2 == tb2.Text

    for three textboxes, there are 8 combinations,

    Does that mean for five textboxes, I need to do 2 to the power of 5, 32 combinations, 32 if statements?   

    Thursday, November 18, 2010 11:31 AM


  • User188494879 posted

    Your question is similar to this post:


    See what I and one of the other member replied.

    You could create a single Stored Procedure and have 5 parameters to it based on the textboxes.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 18, 2010 12:26 PM