Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. insert dropdownliste

insert dropdownliste

Scheduled Pinned Locked Moved ASP.NET
databasehelp
14 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tek 2009
    wrote on last edited by
    #1

    I have a problem of integration of the city code in the table of the employer database, it always inserts the last record in the table and here is the city code used:

    protected void Page_Load(object sender, EventArgs e)
    {
    foreach (short vKey in city.Keys)
    {
    dropdownlist1.Items.Add(city[vKey].cityName);
    dropdownlist1.DataTextField = city[vKey].cityName;
    dropdownlist1.DataValueField = (city[vKey].id_city).ToString();

                }
    

    }

    and the code in the button is:

    employer.cityName= short.Parse(dropdownliste1.DataValueField);
    employer.insert();

    R S T 5 Replies Last reply
    0
    • T tek 2009

      I have a problem of integration of the city code in the table of the employer database, it always inserts the last record in the table and here is the city code used:

      protected void Page_Load(object sender, EventArgs e)
      {
      foreach (short vKey in city.Keys)
      {
      dropdownlist1.Items.Add(city[vKey].cityName);
      dropdownlist1.DataTextField = city[vKey].cityName;
      dropdownlist1.DataValueField = (city[vKey].id_city).ToString();

                  }
      

      }

      and the code in the button is:

      employer.cityName= short.Parse(dropdownliste1.DataValueField);
      employer.insert();

      S Offline
      S Offline
      saini arun
      wrote on last edited by
      #2

      if(!IsPostBack)
      {
      foreach (short vKey in city.Keys)
      {
      dropdownlist1.Items.Add(city[vKey].cityName);
      dropdownlist1.DataTextField = city[vKey].cityName;
      dropdownlist1.DataValueField = (city[vKey].id_city).ToString();

                  }
      

      }

      1 Reply Last reply
      0
      • T tek 2009

        I have a problem of integration of the city code in the table of the employer database, it always inserts the last record in the table and here is the city code used:

        protected void Page_Load(object sender, EventArgs e)
        {
        foreach (short vKey in city.Keys)
        {
        dropdownlist1.Items.Add(city[vKey].cityName);
        dropdownlist1.DataTextField = city[vKey].cityName;
        dropdownlist1.DataValueField = (city[vKey].id_city).ToString();

                    }
        

        }

        and the code in the button is:

        employer.cityName= short.Parse(dropdownliste1.DataValueField);
        employer.insert();

        R Offline
        R Offline
        R Giskard Reventlov
        wrote on last edited by
        #3

        Wrap in IsPostback.

        protected void Page_Load(object sender, EventArgs e)
        {
        if (!IsPostBack)
        {
        foreach (short vKey in city.Keys)
        {
        dropdownlist1.Items.Add(city[vKey].cityName);
        dropdownlist1.DataTextField = city[vKey].cityName;
        dropdownlist1.DataValueField = (city[vKey].id_city).ToString();
        }
        }
        }

        me, me, me "The dinosaurs became extinct because they didn't have a space program. And if we become extinct because we don't have a space program, it'll serve us right!" Larry Niven nils illegitimus carborundum

        S 1 Reply Last reply
        0
        • R R Giskard Reventlov

          Wrap in IsPostback.

          protected void Page_Load(object sender, EventArgs e)
          {
          if (!IsPostBack)
          {
          foreach (short vKey in city.Keys)
          {
          dropdownlist1.Items.Add(city[vKey].cityName);
          dropdownlist1.DataTextField = city[vKey].cityName;
          dropdownlist1.DataValueField = (city[vKey].id_city).ToString();
          }
          }
          }

          me, me, me "The dinosaurs became extinct because they didn't have a space program. And if we become extinct because we don't have a space program, it'll serve us right!" Larry Niven nils illegitimus carborundum

          S Offline
          S Offline
          saini arun
          wrote on last edited by
          #4

          Ignoring the number of seconds, we both replied exactly on the same time :)

          R T 2 Replies Last reply
          0
          • S saini arun

            Ignoring the number of seconds, we both replied exactly on the same time :)

            R Offline
            R Offline
            R Giskard Reventlov
            wrote on last edited by
            #5

            Great minds think alike! I've noticed this before though it doesn't mean anything. I think. Or maybe... :)

            me, me, me "The dinosaurs became extinct because they didn't have a space program. And if we become extinct because we don't have a space program, it'll serve us right!" Larry Niven nils illegitimus carborundum

            1 Reply Last reply
            0
            • S saini arun

              Ignoring the number of seconds, we both replied exactly on the same time :)

              T Offline
              T Offline
              tek 2009
              wrote on last edited by
              #6

              ok :) , thank you very much for both of you, but :( it does not work with IsPostBack, it always inserts the last record in the table table in town employer, as in adding the new num employer must record the index of the choice of city and not the name of the city

              1 Reply Last reply
              0
              • T tek 2009

                I have a problem of integration of the city code in the table of the employer database, it always inserts the last record in the table and here is the city code used:

                protected void Page_Load(object sender, EventArgs e)
                {
                foreach (short vKey in city.Keys)
                {
                dropdownlist1.Items.Add(city[vKey].cityName);
                dropdownlist1.DataTextField = city[vKey].cityName;
                dropdownlist1.DataValueField = (city[vKey].id_city).ToString();

                            }
                

                }

                and the code in the button is:

                employer.cityName= short.Parse(dropdownliste1.DataValueField);
                employer.insert();

                R Offline
                R Offline
                R Giskard Reventlov
                wrote on last edited by
                #7

                Try: employer.cityName = dropdownliste1.SelectedItem.Value;

                me, me, me "The dinosaurs became extinct because they didn't have a space program. And if we become extinct because we don't have a space program, it'll serve us right!" Larry Niven nils illegitimus carborundum

                T 1 Reply Last reply
                0
                • R R Giskard Reventlov

                  Try: employer.cityName = dropdownliste1.SelectedItem.Value;

                  me, me, me "The dinosaurs became extinct because they didn't have a space program. And if we become extinct because we don't have a space program, it'll serve us right!" Larry Niven nils illegitimus carborundum

                  T Offline
                  T Offline
                  tek 2009
                  wrote on last edited by
                  #8

                  Unfortunately, it doesn't work :(

                  R 1 Reply Last reply
                  0
                  • T tek 2009

                    Unfortunately, it doesn't work :(

                    R Offline
                    R Offline
                    R Giskard Reventlov
                    wrote on last edited by
                    #9

                    Hmm: well, I was intrigued so I knocked up a wee app to simulate what you are doing and, for me, it works. That is, wrapping with both bits of code. Have you checked to see what it is you're actually outputting to the list; is each id unique or are they all the same? Can't really see a reason why this should not work as advertised. (Course I can't actually see what you are doing!)

                    me, me, me "The dinosaurs became extinct because they didn't have a space program. And if we become extinct because we don't have a space program, it'll serve us right!" Larry Niven nils illegitimus carborundum

                    1 Reply Last reply
                    0
                    • T tek 2009

                      I have a problem of integration of the city code in the table of the employer database, it always inserts the last record in the table and here is the city code used:

                      protected void Page_Load(object sender, EventArgs e)
                      {
                      foreach (short vKey in city.Keys)
                      {
                      dropdownlist1.Items.Add(city[vKey].cityName);
                      dropdownlist1.DataTextField = city[vKey].cityName;
                      dropdownlist1.DataValueField = (city[vKey].id_city).ToString();

                                  }
                      

                      }

                      and the code in the button is:

                      employer.cityName= short.Parse(dropdownliste1.DataValueField);
                      employer.insert();

                      T Offline
                      T Offline
                      T M Gray
                      wrote on last edited by
                      #10

                      A lot of people have given you the IsPostBack answer, but no one has commented on your loop. What is in your collection and what are you using for your keys? If you had a List<T> you wouldn't have to generate the list of keys and you wouldn't need to loop at all. You could just set the DataSource for the dropdown. If you set a datasource instead of doing Items.Add every time that would also eliminate your duplicates and it would be more efficient the one time it does run (once you had the IsPostBack check).

                      T 1 Reply Last reply
                      0
                      • T T M Gray

                        A lot of people have given you the IsPostBack answer, but no one has commented on your loop. What is in your collection and what are you using for your keys? If you had a List<T> you wouldn't have to generate the list of keys and you wouldn't need to loop at all. You could just set the DataSource for the dropdown. If you set a datasource instead of doing Items.Add every time that would also eliminate your duplicates and it would be more efficient the one time it does run (once you had the IsPostBack check).

                        T Offline
                        T Offline
                        tek 2009
                        wrote on last edited by
                        #11

                        must use the loop to fill the dropdownlist, and since I work in n-tier gift is not easy to call the database in the datasource in this layer, so you should always call the dictionary but the problem I can not solve is that how to save the id of the city selction instead of city name chosen in the dropdownlist ??????

                        T 1 Reply Last reply
                        0
                        • T tek 2009

                          I have a problem of integration of the city code in the table of the employer database, it always inserts the last record in the table and here is the city code used:

                          protected void Page_Load(object sender, EventArgs e)
                          {
                          foreach (short vKey in city.Keys)
                          {
                          dropdownlist1.Items.Add(city[vKey].cityName);
                          dropdownlist1.DataTextField = city[vKey].cityName;
                          dropdownlist1.DataValueField = (city[vKey].id_city).ToString();

                                      }
                          

                          }

                          and the code in the button is:

                          employer.cityName= short.Parse(dropdownliste1.DataValueField);
                          employer.insert();

                          R Offline
                          R Offline
                          R Giskard Reventlov
                          wrote on last edited by
                          #12

                          Okay: think this will do it:

                          dropdownlist1.Items.Add(new ListItem(city[vKey].cityName, (city[vKey].id_city).ToString()));

                          I guess we missed that one - you were setting both the text and value to the same thing. If you had stepped through the code or viewed the source on the page you would have probably picked this up - but don't feel bad - several of us missed it as well!

                          me, me, me "The dinosaurs became extinct because they didn't have a space program. And if we become extinct because we don't have a space program, it'll serve us right!" Larry Niven nils illegitimus carborundum

                          T 1 Reply Last reply
                          0
                          • R R Giskard Reventlov

                            Okay: think this will do it:

                            dropdownlist1.Items.Add(new ListItem(city[vKey].cityName, (city[vKey].id_city).ToString()));

                            I guess we missed that one - you were setting both the text and value to the same thing. If you had stepped through the code or viewed the source on the page you would have probably picked this up - but don't feel bad - several of us missed it as well!

                            me, me, me "The dinosaurs became extinct because they didn't have a space program. And if we become extinct because we don't have a space program, it'll serve us right!" Larry Niven nils illegitimus carborundum

                            T Offline
                            T Offline
                            tek 2009
                            wrote on last edited by
                            #13

                            yes you are absolutely right forgot this step, in any case it works very well I thank you all for your cooperation :) :) :)

                            modified on Wednesday, June 16, 2010 6:03 AM

                            1 Reply Last reply
                            0
                            • T tek 2009

                              must use the loop to fill the dropdownlist, and since I work in n-tier gift is not easy to call the database in the datasource in this layer, so you should always call the dictionary but the problem I can not solve is that how to save the id of the city selction instead of city name chosen in the dropdownlist ??????

                              T Offline
                              T Offline
                              T M Gray
                              wrote on last edited by
                              #14

                              I have no idea what that means. I use n-tier architectures all the time and looping is not necessary and neither are dictionaries. A good n-tier architecture has a business layer that contains objects that represent business entities. Using dictionaries is not good practice because you lose some of the benefits from strong typing in C#.

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups