Domanda

I have a dropdownlist of zipcodes.

It is a really large list and takes too long to load from database. So, first I was looking for the best solution to try and cache or save this data when the website is first loaded so I can use it whenever needed. I tried a list and dictionary then setting the datasource in code behind but it wouldn't let me set the selected value.

It kept telling me that it couldn't bind to a control with no datasource. To make things more difficult the dropdownlist is inside a formview.

I am just not sure the best way to go about this. I am setting the datasource and values in the formview created method but my selected value comes from the formview datasource.

 private Dictionary<int, string> CreateZipcodeList()
{
    string connStr = ConfigurationManager.ConnectionStrings["KlamathAlgaeEntityDevConnectionString"].ConnectionString;
    SqlConnection conn = new SqlConnection(connStr);
    Dictionary<int,string> ziplist = new Dictionary<int,string>();

    //Create CoreEntity Record
    SqlCommand cmd1 = new SqlCommand(@"select zipcode.ZipCodeId, zipcode.zip + ', ' + city.name + ', ' + ISNULL(GeographicState.name,'') + ', ' + Country.name as zipstring
                                    from zipcode left outer join City on ZipCode.CityId = city.CityId
                                    left outer join GeographicState on ZipCode.GeographicStateId = GeographicState.GeographicStateId
                                    left outer join Country on ZipCode.CountryId = Country.CountryId
                                    order by country.name, GeographicState.name, city.name",
                                    conn);

    try
    {
        conn.Open();

        SqlDataReader reader = cmd1.ExecuteReader(CommandBehavior.CloseConnection);
        while (reader.Read())
        {
            if (!reader.IsDBNull(0))
            {
                ziplist.Add(reader.GetInt32(0), reader["zipstring"].ToString());
            }
        }
    }

    finally
    {
        if (conn != null)
        {
            conn.Close();
        }
    }
    return ziplist;
}

protected void AddressForm_ItemCreated(Object sender, EventArgs e)
{


         ((DropDownList)AddressForm.FindControl("Zipcodeddl")).DataSource = CreateZipcodeList();
        ((DropDownList)AddressForm.FindControl("Zipcodeddl")).DataTextField = "Value";
        ((DropDownList)AddressForm.FindControl("Zipcodeddl")).DataValueField = "Key";
        ((DropDownList)AddressForm.FindControl("Zipcodeddl")).DataBind();
        //((DropDownList)AddressForm.FindControl("Zipcodeddl")).SelectedValue = Eval("zipcodeid").ToString();
}

This populates the dropdown fine but when I try setting the selected value it says the control is not databound. This also doesn't store the dictionary anywhere so I need to call the function to load dictionary when ever I need it.

È stato utile?

Soluzione

Ok, that is way too many records for a drop down. Why don't you allow the user to enter 5 numberic characters, and then do an search on your Database by that number. I can't think of any reason you would have to put the entire zip code list in a drop down since no one is going to manually click down to find the one they want. I don't think it servers a usefull purpose, but if there is one let me know and I will try to figure out a solution.

Thats how the post office does it.

Update

To make this work, you would have a TextBox where a user could enter the zip code they think it is. Give them a button to click to search for that zip code.

Do an sql query to your database looking for that Zipcode.Zip, and if found, bring back all the data you need.

If it is not found what I would do is start removing the last character and do another search with a modified query with "Zipcode.Zip like '%modifiedZipCode%', and bring back the closest 10 or so options, and put those in a drop down menu.

If you remove one digit and the database could not find it you could go as far as you want removing characters, and rerunning the query until you got 10 or however many records you want. At some point removing digits will become pointless because the user obviously entered an incorrect zip code.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top