complex dynamic query - best practice/approach?

Last post 11-08-2006 9:22 PM by Iori_Jay. 4 replies.

Sort Posts:

  • complex dynamic query - best practice/approach?

    10-31-2006, 11:02 AM
    • Loading...
    • lcountee
    • Joined on 08-28-2006, 1:31 PM
    • Posts 29

    I'm coding something pretty common - using form input to create a dynamic database query and displaying the results. However, some of the form controls are multi-select boxes, so my query winds up containing a lot of loops and looking pretty messy.

     Any suggestions/example code on the best approach? Any links to a good tutorial on this? C# preferred but other languages OK.

    Filed under: , ,
  • Re: complex dynamic query - best practice/approach?

    10-31-2006, 11:24 PM
    Answer
    • Loading...
    • ReyN
    • Joined on 07-28-2002, 6:53 AM
    • Pilipinas
    • Posts 413

    not sure if this may be considered best practice Big Smile but here's what I do

    you can use good old Request.Form [ formControlID ]  to get a comma-separated list of selected values in a multi-select control, such as a ListBox.

    this sample may not be exactly what you need, but at least you can get the idea


    Reynald V. Nuñez
    aspxtreme
  • Re: complex dynamic query - best practice/approach?

    11-01-2006, 11:23 AM
    • Loading...
    • lcountee
    • Joined on 08-28-2006, 1:31 PM
    • Posts 29
    Thanks ReyN. My original post wasn't very clear. What I need to do is output user-selected rows based on several multi-select listboxes.

    Here is what my query looks like so far, based on input from the first multi-select listbox. Is there a way to do this without all the looping?

     

    // Declare the query string.
    String queryString = "SELECT my_ID, my_Name FROM my_tbl";
    // if mylist contains at least one item
    if (PreviousPage.myList.Count > 0)
    {
        // loop through array and add to query
        foreach (int myIDInt in myArray)
            {
                int i = 0;
                i++;
    
                if (i == 1)
                    {
                        queryString += " AND (my_ID = @myIDInt";
                    }
                    else
                    {
                        queryString += " OR my_ID = @myIDInt";
                    }
    
                    if ((myArray.GetLength(0) + 1) == i)
                    {
                        queryString += ")";
                    }
            }
    }
     
  • Re: complex dynamic query - best practice/approach?

    11-07-2006, 6:19 AM
    • Loading...
    • korzh
    • Joined on 12-05-2005, 3:44 PM
    • Ukraine
    • Posts 22

    Did you try our EasyQuery library: http://devtools.korzh.com/eq/dotnet/

    It is free for non-commercial use.

     

    EasyQuery.NET - user friendly query builder for your web-site
    Filed under: , , ,
  • Re: complex dynamic query - best practice/approach?

    11-08-2006, 9:22 PM
    Answer
    Or you can try ISNULL function in T-SQL, instead of dynamical query. You can find an example here:

    http://forums.asp.net/thread/1371125.aspx
    Welcome to my SQL/ASPNET forum for Chinese
    http://51up.org/bbs/forumdisplay.php?fid=38
Page 1 of 1 (5 items)
Microsoft Communities
Page view counter