Click here to Skip to main content
15,896,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an Sql Data Source with a field named Category which has values like category1, category 2 etc. I want a gridview where user has a choice to select valued in any category or a combination of categories for the filed Category.

for giving this choice to users, I have a dropdownlist with values category1 and category2 etc. My code for selecting a category is
C#
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {

        if(CategoryDropdownList.SelectedValue = "Category1")
            {
                e.Command.Parameters["@Category"].Value = "category1";
            }
        else if (CategoryDropdownList.SelectedValue = "category2")
        {
            e.Command.Parameters["@Category"].Value = "category2";
        }
}

This code works fine when selecting a value for a field Category.

Now I want to introduce a multiple selection choice for users whereby they can select more than one category

for example: if the dropdown list has an value category1or2, the selection of this value should give the user values from database where value of Category field is either category1 or ctegory2
I am not able to find the code for this kind of multiple choice of a parameter. I am also not sure whether it is possible to assign suck multiple values by use of 'OR' and 'AND' as we use in if statements.

Kindly advice me what course of action I should follow.

if this question sounds too elementary, kindly pardon me for asking and help me because I am still a beginner in ASP.NET. I am using C#.
Posted
Updated 6-Feb-14 5:32am
v2
Comments
Richard C Bishop 6-Feb-14 11:24am    
How about using a Switch Case?

1 solution

 
Share this answer
 
Comments
Member 10235977 7-Feb-14 11:38am    
It does not work for me because in my case the number of options for a given filed can vary. One user may choose the option of category1 and category2. The other may choose 3 options like category1, category2 or category3. Still another user may opt for any of the 7 options category 1 to category7. The example given works only when the number of options is fixed like only 2 options in the given example. In my case the field can have 7 values and, thus, there can be many options depending on whether user wants to specify one fields of 2 fields or any other number he likes.
Ahmed Bensaid 7-Feb-14 11:41am    
So you can adapt to your situation. Construct the query with your needs. You've already got logic.
Member 10235977 8-Feb-14 10:08am    
yes that is where I have problem when I specify the values of select command from code behind. For example when I want to specify a parameter value form code behind I use
SqlDataSource1.SelectParameters.Add("@Category", CategoryDropDownList.SelectedValue);
But with this I still get the error
Must declare the scalar variable "@Category".

apparently I am making some mistake in the code for adding the parameter values.
Kindly correct me

My code would be somewhat like this
protected void Button1_Click(object sender, EventArgs e)
{
SqlDataSource1.SelectCommand = "SELECT etc. "
SqlDataSource1.SelectParameters.Add(the above syntax where I have a hunch that I am making a mistake)
Member 10235977 8-Feb-14 10:39am    
For specifying the value of parameter from code behind I also tried
SqlDataSource1.SelectParameters["@Category"].DefaultValue = CategoryDropDownList.SelectedValue;
But in this I get the error:
Object reference not set to an instance of an object.

I have also problem specifying the type of parameter (whether int32 or string), which could be a possible reason for this error. Frankly I do not know the code for specifying the type of parameter.
Kindly help.
Member 10235977 8-Feb-14 10:57am    
I have tried setting type in the aspx page as follows:
<SelectParameters>
<asp:Parameter Name="Category" Type="String" />
</SelectParameters>
but it did not help, as I get the error
Object reference not set to an instance of an object.
Thanks for your help so far
I am almost done but for specifying the parameter values from code behind
Please do help me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900