473,474 Members | 1,822 Online
Bytes | Software Development & Data Engineering Community
Create 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 = sqlCommandGetTableList.ExecuteReader();
ListBoxTableName.DataSource = oDr;
ListBoxTableName.DataValueField = "TABLE_NAME";
ListBoxTableName.DataBind();

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

Aug 15 '06 #1
3 4213
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 = sqlCommandGetTableList.ExecuteReader();
ListBoxTableName.DataSource = oDr;
ListBoxTableName.DataValueField = "TABLE_NAME";
ListBoxTableName.DataBind();

But when the WebForm is displayed and I highlight a TABLE_NAME in the
listbox the ListBoxTableName_SelectedIndexChanged 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:

sqlConnectionTableList.Open();
SqlDataReader oDr = sqlCommandGetTableList.ExecuteReader();
ListBoxTableName.DataSource = oDr;
ListBoxTableName.DataValueField = "TABLE_NAME";
if (!IsPostBack)
{
ListBoxTableName.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 = sqlCommandGetTableList.ExecuteReader();
ListBoxTableName.DataSource = oDr;
ListBoxTableName.DataValueField = "TABLE_NAME";
ListBoxTableName.DataBind();

But when the WebForm is displayed and I highlight a TABLE_NAME in the
listbox the ListBoxTableName_SelectedIndexChanged 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.CloseConnection
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:

sqlConnectionTableList.Open();
SqlDataReader oDr = sqlCommandGetTableList.ExecuteReader();
ListBoxTableName.DataSource = oDr;
ListBoxTableName.DataValueField = "TABLE_NAME";
if (!IsPostBack)
{
ListBoxTableName.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 = sqlCommandGetTableList.ExecuteReader();
ListBoxTableName.DataSource = oDr;
ListBoxTableName.DataValueField = "TABLE_NAME";
ListBoxTableName.DataBind();
>
But when the WebForm is displayed and I highlight a TABLE_NAME in the
listbox the ListBoxTableName_SelectedIndexChanged 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
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...
1
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...
1
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...
3
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...
5
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...
6
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...
21
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...
7
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...
5
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...
2
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...
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
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...
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,...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.