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. Problem with Dropdownlist (DDL) box...

Problem with Dropdownlist (DDL) box...

Scheduled Pinned Locked Moved ASP.NET
csharphelpdatabase
9 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
    Tash18
    wrote on last edited by
    #1

    Hi guys.. Im working on a web application on C#.net with VS2003(Framework1.1).. in my page i have a DDL which populates the items from the database... now on my page the user fills a form by selecting the items from the DDL. The user can also edit previous froms, at this stage the datas come from the database and gets filled on the textbox,DDL, radio buttons etc to edit the fields. Now when a data from the database is not present in the DDL list i get an exception to handle this situation i need to set a condition such that it checks if the data from the database is present in the DDL list then it shows in the DDL otherwise it must show the Fist item in the DDL which is "Select an option". so i put up the following condition:

    if(ddl_Benef.Items.FindByValue(myDr["BenefName"].ToString().Trim()).Value == myDr["BenefName"].ToString())
    {
    ddl_Benef.Items.FindByValue(myDr["BenefName"].ToString().Trim()).Selected=true;
    }
    else
    {
    ddl_Benef.Items.FindByText("Select an option").Selected=true;
    }

    here if the data from the database is present in the DDL the code works fine in case the data is not present in the DDL then it doesnt go to the else condition instead gives me "System.NullReferenceException: Object reference not set to an instance of an object." Any help or advice is appreciated... Thanx in advance, Tash

    A M 2 Replies Last reply
    0
    • T Tash18

      Hi guys.. Im working on a web application on C#.net with VS2003(Framework1.1).. in my page i have a DDL which populates the items from the database... now on my page the user fills a form by selecting the items from the DDL. The user can also edit previous froms, at this stage the datas come from the database and gets filled on the textbox,DDL, radio buttons etc to edit the fields. Now when a data from the database is not present in the DDL list i get an exception to handle this situation i need to set a condition such that it checks if the data from the database is present in the DDL list then it shows in the DDL otherwise it must show the Fist item in the DDL which is "Select an option". so i put up the following condition:

      if(ddl_Benef.Items.FindByValue(myDr["BenefName"].ToString().Trim()).Value == myDr["BenefName"].ToString())
      {
      ddl_Benef.Items.FindByValue(myDr["BenefName"].ToString().Trim()).Selected=true;
      }
      else
      {
      ddl_Benef.Items.FindByText("Select an option").Selected=true;
      }

      here if the data from the database is present in the DDL the code works fine in case the data is not present in the DDL then it doesnt go to the else condition instead gives me "System.NullReferenceException: Object reference not set to an instance of an object." Any help or advice is appreciated... Thanx in advance, Tash

      A Offline
      A Offline
      Ankur m
      wrote on last edited by
      #2

      <> wrote:

      i have a DDL which populates the items from the database

      <> wrote:

      ddl_Benef.Items.FindByText("Select an option").Selected=true;

      When there is no item in the database, how do you think that it will find the text "Select an option". Instead of finding the text, you need to add an item like this:

      ddl_Benef.Items.Add("Select an option");

      Hope this helps! :thumbsup:

      ..Go Green..

      T 1 Reply Last reply
      0
      • A Ankur m

        <> wrote:

        i have a DDL which populates the items from the database

        <> wrote:

        ddl_Benef.Items.FindByText("Select an option").Selected=true;

        When there is no item in the database, how do you think that it will find the text "Select an option". Instead of finding the text, you need to add an item like this:

        ddl_Benef.Items.Add("Select an option");

        Hope this helps! :thumbsup:

        ..Go Green..

        T Offline
        T Offline
        Tash18
        wrote on last edited by
        #3

        Hi thanks for ur reply.. actually in my DDL i have set the 1st item as "Select an option" and the remaining items gets populated from the database... now thats not the issue... when i try 2 edit an Existing form, the page loads with the datas from the database at that time it picks up an item from the database and tries to locate that item in my DDL.. what i exactly want is it should not throw an exception if that item is not present in my DDL but instead should display my 1st item which is -- "Select an option"... Hope u understand rite now.. Thanx in advance.. Tash

        N A 2 Replies Last reply
        0
        • T Tash18

          Hi thanks for ur reply.. actually in my DDL i have set the 1st item as "Select an option" and the remaining items gets populated from the database... now thats not the issue... when i try 2 edit an Existing form, the page loads with the datas from the database at that time it picks up an item from the database and tries to locate that item in my DDL.. what i exactly want is it should not throw an exception if that item is not present in my DDL but instead should display my 1st item which is -- "Select an option"... Hope u understand rite now.. Thanx in advance.. Tash

          N Offline
          N Offline
          nagendrathecoder
          wrote on last edited by
          #4

          You have to do it in your error handling code.

          T 1 Reply Last reply
          0
          • T Tash18

            Hi thanks for ur reply.. actually in my DDL i have set the 1st item as "Select an option" and the remaining items gets populated from the database... now thats not the issue... when i try 2 edit an Existing form, the page loads with the datas from the database at that time it picks up an item from the database and tries to locate that item in my DDL.. what i exactly want is it should not throw an exception if that item is not present in my DDL but instead should display my 1st item which is -- "Select an option"... Hope u understand rite now.. Thanx in advance.. Tash

            A Offline
            A Offline
            Ankur m
            wrote on last edited by
            #5

            Debug the code to see if any item exists in the DDL when there is no item received from database. The exception you mentioned tells that you do not have any item in the DDL (not even "Select an Option").

            ..Go Green..

            modified on Wednesday, April 28, 2010 6:11 AM

            T 1 Reply Last reply
            0
            • A Ankur m

              Debug the code to see if any item exists in the DDL when there is no item received from database. The exception you mentioned tells that you do not have any item in the DDL (not even "Select an Option").

              ..Go Green..

              modified on Wednesday, April 28, 2010 6:11 AM

              T Offline
              T Offline
              Tash18
              wrote on last edited by
              #6

              thanx for ur reply.. my DDL gets loaded with the items on pageload. so the DDL has items in it.. the If condition that im using is working fine if the item retrieved from the database is present in my DDL, it throws an exception only if that item is not present in my DDL...

              if(ddl_Benef.Items.FindByValue(myDr["BenefName"].ToString().Trim()).Value == myDr["BenefName"].ToString())
              {
              ddl_Benef.Items.FindByValue(myDr["BenefName"].ToString().Trim()).Selected=true;
              }
              else
              {
              ddl_Benef.Items.FindByText("Select an option").Selected=true;
              }

              Im not sure if my condition in the if statement is properly framed or not... plz advise.. Thanx in advance, Tash..

              N 1 Reply Last reply
              0
              • N nagendrathecoder

                You have to do it in your error handling code.

                T Offline
                T Offline
                Tash18
                wrote on last edited by
                #7

                I think im not framing my if condition in a proper manner... plz advise.. Thanx in advance, Tash

                1 Reply Last reply
                0
                • T Tash18

                  thanx for ur reply.. my DDL gets loaded with the items on pageload. so the DDL has items in it.. the If condition that im using is working fine if the item retrieved from the database is present in my DDL, it throws an exception only if that item is not present in my DDL...

                  if(ddl_Benef.Items.FindByValue(myDr["BenefName"].ToString().Trim()).Value == myDr["BenefName"].ToString())
                  {
                  ddl_Benef.Items.FindByValue(myDr["BenefName"].ToString().Trim()).Selected=true;
                  }
                  else
                  {
                  ddl_Benef.Items.FindByText("Select an option").Selected=true;
                  }

                  Im not sure if my condition in the if statement is properly framed or not... plz advise.. Thanx in advance, Tash..

                  N Offline
                  N Offline
                  nagendrathecoder
                  wrote on last edited by
                  #8

                  IMO, you should first check whether your condition in if is returning or not. Try like this:

                  if(ddl_Benef.Items.FindByValue(myDr["BenefName"].ToString().Trim()) != null)
                  {
                  if(ddl_Benef.Items.FindByValue(myDr["BenefName"].ToString().Trim()).Value == myDr["BenefName"].ToString()) { ddl_Benef.Items.FindByValue(myDr["BenefName"].ToString().Trim()).Selected=true; } else
                  { ddl_Benef.Items.FindByText("Select an option").Selected=true; }
                  }
                  else
                  {
                  // Select default
                  }

                  If that value is not null, then proceed. Otherwise, set your default option as selected

                  1 Reply Last reply
                  0
                  • T Tash18

                    Hi guys.. Im working on a web application on C#.net with VS2003(Framework1.1).. in my page i have a DDL which populates the items from the database... now on my page the user fills a form by selecting the items from the DDL. The user can also edit previous froms, at this stage the datas come from the database and gets filled on the textbox,DDL, radio buttons etc to edit the fields. Now when a data from the database is not present in the DDL list i get an exception to handle this situation i need to set a condition such that it checks if the data from the database is present in the DDL list then it shows in the DDL otherwise it must show the Fist item in the DDL which is "Select an option". so i put up the following condition:

                    if(ddl_Benef.Items.FindByValue(myDr["BenefName"].ToString().Trim()).Value == myDr["BenefName"].ToString())
                    {
                    ddl_Benef.Items.FindByValue(myDr["BenefName"].ToString().Trim()).Selected=true;
                    }
                    else
                    {
                    ddl_Benef.Items.FindByText("Select an option").Selected=true;
                    }

                    here if the data from the database is present in the DDL the code works fine in case the data is not present in the DDL then it doesnt go to the else condition instead gives me "System.NullReferenceException: Object reference not set to an instance of an object." Any help or advice is appreciated... Thanx in advance, Tash

                    M Offline
                    M Offline
                    Mohammed Hameed
                    wrote on last edited by
                    #9

                    Thank you so much....................... http://www.codeproject.com/Members/Mohammed-Hameed[^]

                    Be a good professional who shares programming secrets with others.

                    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