Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 216
  • Last Modified:

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
0
BeginningWebDesign
Asked:
BeginningWebDesign
1 Solution
 
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
 
BeginningWebDesignAuthor Commented:
Thanks sumix
Your a star

Caz
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now