-3

I have a problem when load dropdownlist, city instead of id seems on dropdownlist value. What is the problem?

#region CITIES
public List<ListItem> loadCities()
{
    using (SqlConnection conn = new SqlConnection(DataBase.Conn))
    {
        conn.Open();
        string query = "SELECT * FROM Cities";
        using (SqlCommand cmd = new SqlCommand(query, conn))
        {
            SqlDataReader dr = cmd.ExecuteReader();
            List<ListItem> li = new List<ListItem>();
            try
            {
                while (dr.Read())
                {
                    li.Add(new ListItem(dr["Cities"].ToString(), dr["Id"].ToString()));
                }
            }
            catch (Exception)
            {
                throw;
            }
            return li;
        }
    }
}
#endregion
Mat
  • 202,337
  • 40
  • 393
  • 406
Goran Zooferic
  • 371
  • 8
  • 23

3 Answers3

0

If your table looks like this

ID | Cities
 1 | New York
 2 | Paris

Then your list should display "New York" and "Paris", and using the SelectedValue property, you should get 1 or 2.

Does your table look like in my example?

Are you using SelectedValue?

Paolo Tedesco
  • 55,237
  • 33
  • 144
  • 193
0

Since it's not always easy to debug datareaders you could store the contents in a variable which you then inspect:

while (dr.Read())
{
    String value = dr["Id"].ToString();
    String text =  dr["Cities"].ToString();
    li.Add(new ListItem(text, value));
}

Then put a breakpoint on li.Add and check if the value and text variable contain what you require.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • i have just debug.Variables of values appears this value="1" text ="Tbilisi". – Goran Zooferic Nov 01 '11 at 09:08
  • i have just create a sample about the problem.Can you examine plz? hotfile.com/dl/133925859/7a97eba/ListIssue.rar.html – Goran Zooferic Nov 01 '11 at 10:06
  • I'm not downloading code to help. You should show the relevant code in the question. Look at the `SelectedValue` hints you're given to retreive the `Value` for an item. If filling the list works, then it's the retreiving where things go amiss. Look there. – CodeCaster Nov 01 '11 at 10:10
  • Why will i use to SelectedValue?i wanna loading all cities in dropdownlist when runtime. – Goran Zooferic Nov 01 '11 at 10:54
0
    public List<City> GetCities()
    {
        try
        {
            List<City> cityList = new List<City>();

            using (sqlCon = new SqlConnection(Database.GetConnectionString()))
            {
                sqlCmd = new SqlCommand("sp_Cities", sqlCon);

                sqlCmd.Parameters.AddWithValue("@Mode", 1);

                sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;

                if (sqlCon.State != System.Data.ConnectionState.Open)
                {
                    sqlCon.Open();
                }

                sqlReader = sqlCmd.ExecuteReader();

                using (sqlReader)
                {
                    if (sqlReader.HasRows)
                    {
                        City c = new City();

                        while (sqlReader.Read())
                        {
                            c = new City ();

                            c.ID = Convert.ToInt32(sqlReader["ID"]);
                            c.Name = Convert.ToString(sqlReader["Name"]);

                            cityList.Add(c);
                        }
                    }
                }
            }


            return cityList;
        }
        catch (Exception)
        {

            throw;
        }
    }

you can create a class with two properties like ID and CityName and make object of that class and a list of that type then iterate the sqlReader and add objects to the list and then you can assign to Dropdown List like this

    private void LoadCities()
    {
        try
        {
            List<City> cityList = new cityBLL().GetAllcity();                

            ddlGender.DataSource = cityList;
            ddlGender.DataTextField = "Name";
            ddlGender.DataValueField = "ID";
            ddlGender.DataBind();
        }
        catch (Exception)
        {

            throw;
        }
    }
dnxit
  • 7,118
  • 2
  • 30
  • 34