Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 299
  • Last Modified:

dynamic data in Dropdownlist ASP.net 2.0

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
kishan66
Asked:
kishan66
  • 9
  • 8
  • 2
1 Solution
 
kishan66Author Commented:
if i am using try and catch under button click .....
     this.DropDownList1.SelectedItem.Value.ToString()  is returning Null.
0
 
Paul JacksonSoftware EngineerCommented:
try :
this.DropDownList1.SelectedItem.Text.ToString()  
0
 
Carl TawnSystems and Integration DeveloperCommented:
Try the following instead:
string value = this.DropDownList1.SelectedText;

Open in new window

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
kishan66Author Commented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
kishan66Author Commented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
kishan66Author Commented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
kishan66Author Commented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
If you view source when the page is displayed and find the "select" element in the html what does it display as?
0
 
kishan66Author Commented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
kishan66Author Commented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
And that is being hit?
0
 
kishan66Author Commented:
NO.
Page_Load doesn't get fired
0
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
Paul JacksonSoftware EngineerCommented:
What is your class name on your code-behind page?
0
 
kishan66Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 9
  • 8
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now