Sorting through Cached DataView

Hi
Can anyone help with this problem.
I have the code below, when I run a search on a country, it display the correct data.
But if I then run a search on another country it still displays the cities of the 1st search due to the dataview being cached.

Question is how can I get to filter a cached dataview so that it shows the correct cities per country.

DataView GetListOfCities()

    ///<Comments>
    ///Get the cities from the database and display them in the ddlWeatherCity control
    ///</comments>
    {
        DataSet objDS_GetListOfCities;
        // Declare a Dataset
        objDS_GetListOfCities = new DataSet();

       
        //Database Connection
        SqlConnection dbConnection;

        //SQLDataAdapter
        SqlDataAdapter objDA_GetListOfCities;

        //Declare DataView
        DataView objDV_GetListOfCities;


        dbConnection = new SqlConnection();

        dbConnection.ConnectionString = ConfigurationManager.ConnectionStrings["dbConn"].ConnectionString;
        objDV_GetListOfCities = (DataView)Cache["ddlWeatherCities"];
        try
        {
            if (objDV_GetListOfCities == null)
            {
                objDA_GetListOfCities = new SqlDataAdapter("sp_GetListOfCities", dbConnection);
                objDA_GetListOfCities.SelectCommand.CommandType = CommandType.StoredProcedure;


                objDA_GetListOfCities.Fill(objDS_GetListOfCities, "tblGetListOfCities");
                objDV_GetListOfCities = objDS_GetListOfCities.Tables["tblGetListOfCities"].DefaultView;
                Cache.Insert("ddlWeatherCities", objDV_GetListOfCities, null, DateTime.Now.AddHours(24), TimeSpan.Zero);
                objDV_GetListOfCities.RowFilter = "country = '" + ddlWeather.SelectedValue.ToString() +"'";

            }
        }
        catch (Exception)
        {
            //return;
        }
        finally
        {
            // check if the connection was open before trying to close it
            if (dbConnection.State == ConnectionState.Open)
            {
                dbConnection.Close();
            }
        }
        return objDV_GetListOfCities;

    }

Any help would be appreciated
Caz
BeginningWebDesignAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sumixCommented:

 If the dataview is in cache you don't need to access the database again, but in either case you should refresh the filter. You have to put the line where you set the filter outside the 'if' block:
   if (objDV_GetListOfCities == null)
            {
                objDA_GetListOfCities = new SqlDataAdapter("sp_GetListOfCities", dbConnection);
                objDA_GetListOfCities.SelectCommand.CommandType = CommandType.StoredProcedure;


                objDA_GetListOfCities.Fill(objDS_GetListOfCities, "tblGetListOfCities");
                objDV_GetListOfCities = objDS_GetListOfCities.Tables["tblGetListOfCities"].DefaultView;
                Cache.Insert("ddlWeatherCities", objDV_GetListOfCities, null, DateTime.Now.AddHours(24), TimeSpan.Zero);
            }

    objDV_GetListOfCities.RowFilter = "country = '" + ddlWeather.SelectedValue.ToString() +"'";

 sumix

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BeginningWebDesignAuthor Commented:
Thanks sumix
Your a star

Caz
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.