473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

retrieving ListBox data when populating with TableAdapter

3
Hello, I'm a Java developer but brand new to ASP (i have done some of the tutorials) so I have some ASP programming background however I cannot find the cause or the fix to this problem.

I am writing a web page in ASP (using C#) that needs to take data from a Database and put it into a ListBox, then have a button retrieve the selected item and then do some stuff. The problem I am having is retrieving the values in the ListBox.

Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(object sender, EventArgs e)
  2.     {
  3.         CategoriesTableAdapter categories = new CategoriesTableAdapter();
  4.         DataTable table1 = categories.GetCategories();
  5.         ListBox1.DataSource = table1;
  6.         ListBox1.DataTextField = "CategoryName";
  7.         ListBox1.DataValueField = "CategoryID";
  8.         ListBox1.DataBind();
  9.     }
  10.  
  11. protected void SubmitBtn_Click(Object sender, EventArgs e)
  12.     {
  13.         Label1.Text = ( "Index: " + ListBox1.SelectedIndex );
  14.         Label2.Text = ( "Selection Text: " + ListBox1.SelectedItem );
  15.         Label3.Text = ( "Selection Value: " + ListBox1.SelectedValue );
  16.     }
  17.  
If it matters, I am formating my code using a class (.asxp.cs file) to run the code. I tried putting it all into one file and it has the same results. When I run the code, my ListBox is populated exactly the way I need it to be. However, when I select something and click the button that fires the SubmitBtn_Click function, it always displays the exact same thing:

Index: -1
Selection Text:
Selection Value:

when trying to debug and trace my problem, I found that it works perfectly fine when I manually put in values via:
Expand|Select|Wrap|Line Numbers
  1. <asp:ListItem Value="1" Text="Item 1"/>
  2.  
Any help at all would be greatly appreciated. If I missed anything important, let me know.
Jul 10 '08 #1
0 1428

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: cloverme | last post by:
Hi, I need help populating a listbox from a database on a webform. I created a an access database with a table, fields and data. Then I created a WebForm in vb.net and added a DropDownList...
2
by: JMorrell | last post by:
I’m trying to populate a listbox with data that comes from my SQL server. I’ve created the connection, filled the dataset and loaded the data adapter. Apparently, I’m missing something...
2
by: collie | last post by:
Hi, I have 2 listboxes. The first gets populated from the db as soon as the page loads. The second listbox get populated based on the user's selection from the first listbox. However,...
6
by: Chris Leuty | last post by:
I am populating a multiselect Listbox from a dataset, with the content of the listbox filled by one table, and the selections determined from another table. So far, I have been keeping the dataset...
6
by: Janaka | last post by:
Help! I have two ListBox controls on my web form. The first one gets populated on entry with values from the DB. I then use JavaScript to copy options from this ListBox to my second one. (I...
5
by: Dave | last post by:
Hi All, I have a windows form that contains 2 listboxes and 2 buttons. The listbox on the right is populated by a database routine (This is the easy part). The listbox on the left is populated...
1
by: dbuchanan | last post by:
Hello, A section in Data Sources window is mystifying to me. In the case of my code the Data Sources window shows my references to the data access layer. First here is what I see in my Data...
10
by: Ben | last post by:
Hi Im trying to get the value from a listbox, i have the below code to get the text: txtEntity.Text = ListBox1.Text But do not seem to be able to retrieve the data that i set as the value...
7
by: Randy | last post by:
Hi, I've got a listbox on a form that gets updated by opening another form for data entry. Once the user enters the new item name and clicks "accept", the data entry form closes and the original...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.