We help IT Professionals succeed at work.

Sorting through Cached DataView

BeginningWebDesign
on
Medium Priority
228 Views
Last Modified: 2010-04-16
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
Comment
Watch Question

Commented:

 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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Thanks sumix
Your a star

Caz
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.