473,545 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Listbox SelectedIndex set to -1

I populate an unbound ListBox on a WebForm with the following C# code.

SqlDataReader oDr = sqlCommandGetTa bleList.Execute Reader();
ListBoxTableNam e.DataSource = oDr;
ListBoxTableNam e.DataValueFiel d = "TABLE_NAME ";
ListBoxTableNam e.DataBind();

But when the WebForm is displayed and I highlight a TABLE_NAME in the
listbox the ListBoxTableNam e_SelectedIndex Changed method fires, but the
SelectedIndex value is -1. What am I doing wrong?

Aug 15 '06 #1
3 4220
Are you closing your SqlDataReader and / or the underlying SqlConnection?
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Greg Larsen" wrote:
I populate an unbound ListBox on a WebForm with the following C# code.

SqlDataReader oDr = sqlCommandGetTa bleList.Execute Reader();
ListBoxTableNam e.DataSource = oDr;
ListBoxTableNam e.DataValueFiel d = "TABLE_NAME ";
ListBoxTableNam e.DataBind();

But when the WebForm is displayed and I highlight a TABLE_NAME in the
listbox the ListBoxTableNam e_SelectedIndex Changed method fires, but the
SelectedIndex value is -1. What am I doing wrong?
Aug 15 '06 #2
No.

I fixed the problem by changing my code to read like this:

sqlConnectionTa bleList.Open();
SqlDataReader oDr = sqlCommandGetTa bleList.Execute Reader();
ListBoxTableNam e.DataSource = oDr;
ListBoxTableNam e.DataValueFiel d = "TABLE_NAME ";
if (!IsPostBack)
{
ListBoxTableNam e.DataBind();
}


"Peter Bromberg [C# MVP]" wrote:
Are you closing your SqlDataReader and / or the underlying SqlConnection?
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Greg Larsen" wrote:
I populate an unbound ListBox on a WebForm with the following C# code.

SqlDataReader oDr = sqlCommandGetTa bleList.Execute Reader();
ListBoxTableNam e.DataSource = oDr;
ListBoxTableNam e.DataValueFiel d = "TABLE_NAME ";
ListBoxTableNam e.DataBind();

But when the WebForm is displayed and I highlight a TABLE_NAME in the
listbox the ListBoxTableNam e_SelectedIndex Changed method fires, but the
SelectedIndex value is -1. What am I doing wrong?
Aug 15 '06 #3
good, but you still need to CLOSE your DataReader when you are done, because
it holds the connection open. You can use CommandBehavior .CloseConnectio n
overload, and the closing of the reader will also close the connection.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Greg Larsen" wrote:
No.

I fixed the problem by changing my code to read like this:

sqlConnectionTa bleList.Open();
SqlDataReader oDr = sqlCommandGetTa bleList.Execute Reader();
ListBoxTableNam e.DataSource = oDr;
ListBoxTableNam e.DataValueFiel d = "TABLE_NAME ";
if (!IsPostBack)
{
ListBoxTableNam e.DataBind();
}


"Peter Bromberg [C# MVP]" wrote:
Are you closing your SqlDataReader and / or the underlying SqlConnection?
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Greg Larsen" wrote:
I populate an unbound ListBox on a WebForm with the following C# code.
>
SqlDataReader oDr = sqlCommandGetTa bleList.Execute Reader();
ListBoxTableNam e.DataSource = oDr;
ListBoxTableNam e.DataValueFiel d = "TABLE_NAME ";
ListBoxTableNam e.DataBind();
>
But when the WebForm is displayed and I highlight a TABLE_NAME in the
listbox the ListBoxTableNam e_SelectedIndex Changed method fires, but the
SelectedIndex value is -1. What am I doing wrong?
>
Aug 16 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
13442
by: David Ichilov | last post by:
I've class derived from "Object" class, with ToString() method overriden itc. , now i add object of this class to Windows.Forms.ListBox.Items collection, and it works fine, displaying what ToString() should return... then the program is in the runtime, i chage fields of this object in thet way ( (MyClass)ListBox.SelectedItem ).SomeProperty...
1
3207
by: Josema | last post by:
Hi to all, I have a class (persons) that derives from collection base: and another class (person) with this properties: -ID -Name When i have complete filled the object Persons with all the persons, i put each persons inside a ListBox following this:
1
7871
by: jez123456 | last post by:
Hi, I have a windows form with a listbox control. My code all works correctly when deleting an item from the listbox except the last item. I get the following message when trying to delete the last item:- Specified argument was out of the range of valid values. Parameter name: '64' is not a valid value for 'Value'. The 64 reduces...
3
4778
by: Joey | last post by:
Hi, I'm trying to add a default item to my listbox but when I do it tells me that it's not defined, could someone tell me the syntax I need to use to get the listbox control to display a default item? I have tried the following: lstStates.Items.Insert(0, new ListItem("By State","0"))
5
3517
by: Lie | last post by:
Hi all, I have problem in getting selectedindex of multiple listbox selection in a datagrid. I have a listbox with multiple selection mode inside datagrid. In Edit mode, I need to get back all selected items of that listbox and display it. can anyone help? Thanks
6
6127
by: David De Cotis | last post by:
Hello all, I am trying to go through a ListBox and verify if am item was selected. If an item was selected, I would like to get a handle of the item and simply do a response.write on the selected handle. The issue that I am facing is that I am going each item, but my code does not catch the selected item. Can anyone please take a look...
21
2242
by: Bilal Abbasi | last post by:
I realize that you can add items to a list box as objects so you can have access to more than just one property like the itemindex in vb6. Question I have is how do I cause the listbox to show a different text after the object has already been added.
7
4521
by: Dave | last post by:
Hi all, After unsuccessfully trying to make my own dual listbox control out of arraylists, I decided to look for a 3rd party control. I've looked for over a week now and can't find anything but ASP.Net stuff when I need a Windows Form control. I've seen dual listbox populators in countless Windows applications, and have seen them run very...
5
2836
by: Academia | last post by:
(If you've seen this in the drawing NG, sorry. I inadvertently sent it there.) I have a listbox populated with Objects. The Class has a String field that ToString returns. I assume that is what the ListBox uses for its display. Correct?
2
4388
by: erbear | last post by:
Hi all, I am new to VB, and I am having some troubles. I have a listbox that when you click on the item it displays a picture in a picture box and a message in a label. It works fine when an item is manually selected, but I want forward and back buttons that will do this. It works once but it doesn't highlight the new item in the listbox, so...
0
7411
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7926
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7439
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7773
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5987
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4962
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3468
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1028
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
722
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.