views:

40

answers:

3

Hi All-

I am relatively new to winforms and C# and I am developing an application that will allow users to perform a search. To search the data they can use the following:

-Combobox with 6 options

-text box that they will enter info based on the combobox search selected.

I have a stored procedure with a parameter for each of the search options. The procedure works for searching on any of the options. This procedure has been added to the project and I am connecting to it via a TableAdapter.

What I am struggling with is the best way to pass each of these search criteria to the code. I started it this way:

public void DataRefresh(string searchCombo, string searchValue)
    {
        string returnMessage = string.Empty;
        switch (searchCombo)
        {
            case "Acct":
                Data.Manager.TAM.SearchDataTableAdapter.Fill(DataSet.spSearchData, ref returnMessage, searchValue, null, null, null, null, null);
                break;
        }
        SearchDataBindingSource.DataSource = DataSet.spSearchData;
    }

I was initially thinking I could use the switch/case to pass the parameters based on what has been sent by the user.

Is there a better way to do this? I am thinking yes, but I can't seem to think of a way.

Any suggestions would be great!

A: 

You can creat a class with two properties

1- Option

2- Additional Criteria.

And create a object of this class in your UI /Controller, assign the properties and send this object to the Data Access Layer.

saurabh
+1  A: 

I figured out another way to do it, instead of passing 6 parameters to the stored procedure I changed it to only pass two the combobox value and the text box value. Then I put If statements in the stored proc to handle the values being passed through.

Thanks for your help.

bluefeet
+1  A: 

Your code sample suggests that you did not separate your GUI, your business logic, your domain objects and your data layer from each other. This would be a more flexible architecture design, but anyway, that is not the concern of your question.

In my opinion, the searchCriteria could be an object, thus making it reusable for other searches.

Following @saurabh's suggestion, could make it simpler to use, as you could, if you're using .NET 3.5, use Linq queries in your search.

Then, your ComboBox shall contain the property names of this search object, then you could just go like:

public void DataRefresh(string comboBoxPropertyName, object value) {
    var query = from s in searchResults
                where (s.GetType().GetProperties()[comboBoxPropertyName].GetValue(s) = value)
                select s

    SearchDataBindingSource.DataSource = query.ToList();
}

Where searchResults represents the result of your SP. Perhaps a cast will be necessary from your TableAdapter, I don't know. I have never used this class.

Disclaimer: This code is provided as-is uncompiled right from the top of my head. This code purpose is only to express a point of view and shall not be considered the absolute solution, as I don't know if this would be possible for you to work that way with the objects you decided to work with, but I do hope this simplifies your way through.

Will Marcouiller