?
Solved

dynamic data in Dropdownlist ASP.net 2.0

Posted on 2011-03-09
19
Medium Priority
?
294 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

777 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