Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

dynamic data in Dropdownlist ASP.net 2.0

Posted on 2011-03-09
19
Medium Priority
?
295 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 2000 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
 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

610 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