Solved

dynamic data in Dropdownlist ASP.net 2.0

Posted on 2011-03-09
19
278 Views
Last Modified: 2012-05-11
hi all,

i am trying to populate a Dropdownlist1 with values from XML file. loads fine.
            this.DropDownList1.DataSource = arrayAcct;
            this.DropDownList1.DataBind();
                                    where arrayAcct = {Acct1,Acct2,Acc3} values from XML( for example.)
Now when i am trying to check the selected value from dropdownlist1
               protected void Button1_Click(object sender, EventArgs e)
                      {
                           this.lblmsg.Text = this.DropDownList1.SelectedItem.Value.ToString();
                      }
Exception at above line : NullReferenceException was unhandled by user code

pls correct me if my databinding is wrong in first place or something else.

ASP.2.0, C#
0
Comment
Question by:kishan66
  • 9
  • 8
  • 2
19 Comments
 

Author Comment

by:kishan66
ID: 35085458
if i am using try and catch under button click .....
     this.DropDownList1.SelectedItem.Value.ToString()  is returning Null.
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 35085516
try :
this.DropDownList1.SelectedItem.Text.ToString()  
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35085518
Try the following instead:
string value = this.DropDownList1.SelectedText;

Open in new window

0
 

Author Comment

by:kishan66
ID: 35085596
Hi Jacko72,
i tried ur suggestion ---  this.DropDownList1.SelectedItem.Text.ToString()  
Though no exception , there is no value returned ..its empty.

HI carl Tawn,
      string value = this.DropDownList1.SelectedText; is not valid as there is no Selected Text associated with dropdownlist i changed to value.

as mentioned above ,  my this.DropDownList1.Selectedvalue returns empty string.

can you guys pls check if there is any problem with the way i'm populating dropdownlist
 
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35085645
What precisely is it you are using as your data source? I know you said it is coming from XML, but are you putting that into a string array or using an XmlNodeList or something else?
0
 

Author Comment

by:kishan66
ID: 35085846
from XMl, i am constructing a string(xmlSTR) with coma separated values and creating a String array ..using Split(',');
sample code----  String[] arrayAcct =  (xmlSTR).Split(',');
                            this.DropDownList1.DataSource = arrayAcct;
                          this.DropDownList1.DataBind();
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35085982
Where in your code are you binding the dropdown? As a test i am using the following and SelectedText, SelectedValue and SelectedItem.ToString() all return the correct item:
        <asp:DropDownList ID="DD1" runat="server">
        </asp:DropDownList>

        <asp:Button ID="Button1" runat="server" Text="Click Me" OnBlick="Button1_Click" />

        protected void Page_Load(object sender, EventArgs e)
        {
            string[] str = "Hello, World, This, Is, Me".Split(new char[] { ',' });

            DD1.DataSource = str;
            DD1.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }

Open in new window

0
 

Author Comment

by:kishan66
ID: 35086149
I'm really sorry.... cannot figure out this basic thing...


<asp:DropDownList ID="DropDownList1" runat="server" align="right" Height="19px" 
                        Width="131px" onselectedindexchanged="DropDownList1_SelectedIndexChanged">
                    </asp:DropDownList>
------------------------------------------------------------------

page_load()
{
   loadDropDown()
}
public void loadDropDown()
        {
            LoadAccountsClass lda = new LoadAccountsClass();
            String[] arrayAcct =  (lda.getAllAccounts()).Split(new char[]{','});
            int i = 0;
            this.DropDownList1.Items.Clear();
            this.DropDownList1.DataSource = arrayAcct;
            this.DropDownList1.DataBind();
}

 protected void Button1_Click(object sender, EventArgs e)
{
    try
      {
this.lblmsg.Text =  this.DropDownList1.SelectedItem.Value.ToString();
}
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
        }

Open in new window

0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 35086268
Ah ok. It is likely to be because you are rebinding the dropdown each time the page loads. Instead of just calling "loadDropDown()" do this instead:
if (!Page.IsPostback)
{
    loadDropDown();
}

Open in new window

0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:kishan66
ID: 35086477
Carl tawn,

I did try the above sugestion... still no value is returned
 this.DropDownList1.SelectedItem.Text.ToString() -> always Empty.
i tried to check the values by mouse hover -- found index = -1

any idea

0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35086512
If you view source when the page is displayed and find the "select" element in the html what does it display as?
0
 

Author Comment

by:kishan66
ID: 35086976
u know what , my  page_load for default page is not getting called .. the reason it was working all these time is i am calling the function from Default.aspx page like  <% getAccts(); %>.


this is what in my Default.aspx page
<%@ Page Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="false" CodeBehind="Default.aspx.cs" Inherits="ASPNET_XML._Default" %>

look at the< Inherits > in above line ---  my pages names are "Default.aspx & Default.aspx.cs".
if i change the Inherit="ASPNET_XML.Default" it throes errors.

Not sure whether this has got anything to do with our current problem

pls again i apolozise for my minimum knowledge.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35087086
It will be. I noticed you had "page_load" but assumed you just had your own method that you called from the Page_Load event. If you change:
page_load()
{
   loadDropDown()
}

Open in new window

To:
protected void Page_Load(object sender, EventArgs e)
{
    loadDropDown();
}

Open in new window

And put a break point in it, it should hopefully fire correctly.
0
 

Author Comment

by:kishan66
ID: 35087283
Carl,

its the same what i had ...but did not fully mention the Page_load syntax in my previous post....
Answer is-:  Yes. i have the Page_Load already declared as

protected void Page_Load(object sender, EventArgs e)
{
    loadDropDown();
}
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35087859
And that is being hit?
0
 

Author Comment

by:kishan66
ID: 35088438
NO.
Page_Load doesn't get fired
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35092732
OK, just for testing purposes add a second button to your page, then in the click event for the button call the "loadDropDown()" method. At least that way you will be able to see if the dropdown stuff is actually working or not.
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 35101318
What is your class name on your code-behind page?
0
 

Author Comment

by:kishan66
ID: 35103856
i figured out....
in the default.aspx at the very begning line i have AutoEventWireup="False"
when i changed to AutoEventWireup="True" all works as expected.. ho.hooo :-)

i know its really a tiny one...sorry due to my limited knowledge i could not eyeball it earlier.

thank you "Carl tawn" .i really appreciate ur efforts in helping me....
i increased points ..as i learnt few things in the above process
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now