1

I have a checkboxlist on my asp webpage which lists a number of skills required for a project. What i wish to do is to check multiple items from this checkboxlist and then create a search function to list all users who have the relevant skill to the ones checked. I can kinda get this to work, but instead of showing all users, it only shows one user.

For example, if i check "Planning" and "Designing" from my checkboxlist it should list users "Peter" and "Vicki" as they have the required skills but my code only shows Peter. My code is below:

StringBuilder sb2 = new StringBuilder();

foreach (ListItem item in chkGeneralSkills.Items)
        {
            if (item.Selected)
            {
                sb2.AppendFormat("{0}  ", item.Text);


                    using (SqlConnection oConn = new SqlConnection(ConfigurationManager.ConnectionStrings["connString"].ToString()))
                    {
                        string sql = "SELECT us.FName + ' ' + us.SName As 'Name', " +
                                 "sk.SkillsID, sl.SkillTitle " +
                                 "FROM Users us " +
                                 "LEFT JOIN Skills sk ON sk.UserID = us.UserID " +
                                 "LEFT JOIN SkillsListing sl ON sl.SkillsListingID = sk.SkillsListingID " +
                                 "WHERE sl.SkillTitle LIKE '" + item.Text + "'";
                        try
                        {
                            oConn.Open();
                            SqlCommand cmd = new SqlCommand(sql, oConn);
                            SqlDataReader reader = cmd.ExecuteReader();
                            chkMatchedUsers.DataTextField = "Name";
                            chkMatchedUsers.DataValueField = "Name";
                            chkMatchedUsers.DataSource = reader;

                            chkMatchedUsers.DataBind();

                            oConn.Close();
                        }
                        catch (Exception)
                        {
                            throw;
                        }
                    }
            }
        }
1
  • Can you try to put % sign to your like query and see if it works? Commented Feb 16, 2013 at 13:38

2 Answers 2

1

In your loop where you are looping through items just add variable and assign that value to variable. Then use that variable in where clause. Also end that foreach loop before you start initiating sql connection. Hope its clear to you.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi AshReva, I'm a bit confused on what you mean?
I think in your where clause you use like item.text but you should use s1.skilltitle in and just use that variable in the "IN" clause. Did you understood?
Hi AshReva, I'm a little confused could you provide sample code?
0

It's hitting the database for each selected item, but if my assumption is correct that Peter has one of the to skills and Vicky has the other, you're ignoring the first response and overriding with the second.

Using what you have already, you should build up a where clause looping through the items and then Call the database just once, rather than per selected item. This will cause your database round trips to decrease too and should return both Vicky and Peter in the results, rather than one or the other.

Untested code - to give you the idea how to accomplish.

var sb = new StringBuilder();
    foreach (ListItem item in chkGeneralSkills.Items)
    {
        if (item.selected)
        {       
           sb.Append("'" + item.text + "',");
        }
    }

    using (SqlConnection oConn = new SqlConnection(ConfigurationManager.ConnectionStrings["connString"].ToString()))
    {
        string sql = "SELECT us.FName + ' ' + us.SName As 'Name', " +
                    "sk.SkillsID, sl.SkillTitle " +
                    "FROM Users us " +
                    "LEFT JOIN Skills sk ON sk.UserID = us.UserID " +
                    "LEFT JOIN SkillsListing sl ON sl.SkillsListingID = sk.SkillsListingID " + 
                    " WHERE sl.SkillTitle IN (" + sb.ToString() + ")";
        try
        {
            oConn.Open();
            SqlCommand cmd = new SqlCommand(sql, oConn);
            SqlDataReader reader = cmd.ExecuteReader();
            chkMatchedUsers.DataTextField = "Name";
            chkMatchedUsers.DataValueField = "Name";
            chkMatchedUsers.DataSource = reader;

            chkMatchedUsers.DataBind();

            oConn.Close();
        }
        catch (Exception)
        {
            throw;
        }
    }

6 Comments

Thanks for your response, I'm a newbie at programming, are there any code examples that I could reference for the while clause loop and how to call the database once rather than querying each time?
@spacebison cant we just use in clause and add items to variable? Why we need to build where clause?
@AshReva Yes, that's an alternative and probably less code. Updated answer as it's an easier/simpler solution.
many thanks for the example code, i now have this working. Thank you
Excellent - good stuff - @AshReva also deserves thanks (and a vote) as I adapted my suggestion with his simpler, more effective solution. Glad it helped :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.