473,785 Members | 2,830 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can't read SelectedValue from Listbox

Maybe it's simple, but I can't get it to work.
I'm originally a webdeveloper, and just started to work on a C# windows
application.
I want a list, something similar to a <select><option > object, where the
displayed value is different from the real value.
I populate the list like this, where I assume that SelectedValue is
something like the <option value="ID">

while (sql.Read())
{
currentItem=thi s.List.Items.Ad d(sql["name"]);
List.SelectedIn dex=currentItem ;
List.SelectedVa lue=sql["id"];
}

Now when I select an item in the list and do something like
textbox.Text=Li st.SelectedValu e.ToString();
it gives me an error.
Debugging gives me a <undisplayabl e value> for the SelectedValue.
What am I doing wrong here?
Nov 16 '05 #1
4 6270
Here is an examples drop down as generate by Visual Studio .NET 2003 ...

<form id="Form1" method="post" runat="server">
<asp:dropdownli st id="ddlA" style="Z-INDEX: 101; LEFT: 16px; POSITION:
absolute; TOP: 184px" runat="server" AutoPostBack="T rue" Width="217">
<asp:ListItem Value="Computer Industry" Selected="True" >Computer
Industry</asp:ListItem>
<asp:ListItem Value="msdn.mic rosoft.com/msdnmag/">MSDN
Magazine</asp:ListItem>
<asp:ListItem Value="www.nwfu sion.com/">NetworkWo rld</asp:ListItem>
<asp:ListItem Value="www.zdne t.com/pcmag/">PC Magazine</asp:ListItem>
<asp:ListItem Value="www.web-developer.com/">Web
Developer</asp:ListItem>
<asp:ListItem Value="www.webt echniques.com/">Web
Techniques</asp:ListItem>
<asp:ListItem Value="www.win2 000mag.com/">Windows 2000
Magazine</asp:ListItem>
<asp:ListItem Value="www.ntsy stems.com/">Windows NT
Systems</asp:ListItem>
<asp:ListItem Value="www.wire d.com/">Wired Magazine</asp:ListItem>
</asp:dropdownlis t>
</form>

--
-- Thom Little -- www.tlaNET.net -- Thom Little Associates, Ltd.
--

"Mantova" <fl******@hotma il.com> wrote in message
news:41******** *************@d reader18.news.x s4all.nl...
Maybe it's simple, but I can't get it to work.
I'm originally a webdeveloper, and just started to work on a C# windows
application.
I want a list, something similar to a <select><option > object, where the
displayed value is different from the real value.
I populate the list like this, where I assume that SelectedValue is
something like the <option value="ID">

while (sql.Read())
{
currentItem=thi s.List.Items.Ad d(sql["name"]);
List.SelectedIn dex=currentItem ;
List.SelectedVa lue=sql["id"];
}

Now when I select an item in the list and do something like
textbox.Text=Li st.SelectedValu e.ToString();
it gives me an error.
Debugging gives me a <undisplayabl e value> for the SelectedValue.
What am I doing wrong here?

Nov 16 '05 #2
On Sun, 12 Sep 2004 12:33:26 +0200, Mantova <fl******@hotma il.com> wrote:

Hi Mantova,

I'm sorry, but I don't know why your code works as the documents seem to indicate you can set the value manually without a ValueMember. I also could not set the Value. It may be that a DataSource is required.

An alternate solution is to use an ArrayList as a source (also see the USState example in the documents), or a DataSet

ArrayList dataList = new ArrayList();

while(sql.Read( ))
{
dataList.Add(ne w MyObject(sql["name"], sql["id"]));
}

List.DisplayMem ber = "MyDisplay" ;
List.ValueMembe r = "MyValue";
List.DataSource = dataList;

....

public class MyObject
{
private string disp;
private int val;

public string MyDisplay
{
get{return disp;}
}
public int MyValue
{
get{return val;}
}

public MyObject(string display, int id}
{
disp = display;
val = id;
}
}

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #3

"Thom Little" <th**@tlanet.ne t> wrote in message
news:uH******** *****@TK2MSFTNG P11.phx.gbl...
Here is an examples drop down as generate by Visual Studio .NET 2003 ...

<form id="Form1" method="post" runat="server">
<asp:dropdownli st id="ddlA" style="Z-INDEX: 101; LEFT: 16px; POSITION:
absolute; TOP: 184px" runat="server" AutoPostBack="T rue" Width="217">
<asp:ListItem Value="Computer Industry" Selected="True" >Computer
Industry</asp:ListItem>
<asp:ListItem Value="msdn.mic rosoft.com/msdnmag/">MSDN
Magazine</asp:ListItem>
<asp:ListItem Value="www.nwfu sion.com/">NetworkWo rld</asp:ListItem>
<asp:ListItem Value="www.zdne t.com/pcmag/">PC Magazine</asp:ListItem>
<asp:ListItem Value="www.web-developer.com/">Web
Developer</asp:ListItem>
<asp:ListItem Value="www.webt echniques.com/">Web
Techniques</asp:ListItem>
<asp:ListItem Value="www.win2 000mag.com/">Windows 2000
Magazine</asp:ListItem>
<asp:ListItem Value="www.ntsy stems.com/">Windows NT
Systems</asp:ListItem>
<asp:ListItem Value="www.wire d.com/">Wired Magazine</asp:ListItem>
</asp:dropdownlis t>
</form>

--
-- Thom Little -- www.tlaNET.net -- Thom Little Associates, Ltd.
--

"Mantova" <fl******@hotma il.com> wrote in message
news:41******** *************@d reader18.news.x s4all.nl...
Maybe it's simple, but I can't get it to work.
I'm originally a webdeveloper, and just started to work on a C# windows
application.
I want a list, something similar to a <select><option > object, where the
displayed value is different from the real value.
I populate the list like this, where I assume that SelectedValue is
something like the <option value="ID">

while (sql.Read())
{
currentItem=thi s.List.Items.Ad d(sql["name"]);
List.SelectedIn dex=currentItem ;
List.SelectedVa lue=sql["id"];
}

Now when I select an item in the list and do something like
textbox.Text=Li st.SelectedValu e.ToString();
it gives me an error.
Debugging gives me a <undisplayabl e value> for the SelectedValue.
What am I doing wrong here?



It's a Windows application, not web application..bu t thanx anyway
Nov 16 '05 #4

"Morten Wennevik" <Mo************ @hotmail.com> wrote in message
news:opsd7p48g8 klbvpo@stone...
On Sun, 12 Sep 2004 12:33:26 +0200, Mantova <fl******@hotma il.com> wrote:

Hi Mantova,

I'm sorry, but I don't know why your code works as the documents seem to indicate you can set the value manually without a ValueMember. I also could
not set the Value. It may be that a DataSource is required.
An alternate solution is to use an ArrayList as a source (also see the USState example in the documents), or a DataSet
ArrayList dataList = new ArrayList();

while(sql.Read( ))
{
dataList.Add(ne w MyObject(sql["name"], sql["id"]));
}

List.DisplayMem ber = "MyDisplay" ;
List.ValueMembe r = "MyValue";
List.DataSource = dataList;

...

public class MyObject
{
private string disp;
private int val;

public string MyDisplay
{
get{return disp;}
}
public int MyValue
{
get{return val;}
}

public MyObject(string display, int id}
{
disp = display;
val = id;
}
}

--
Happy coding!
Morten Wennevik [C# MVP]


hmmm...maybe I'll try that.
But I hoped there would be a more elegant solution, like in <select><option >

Thanx for yer help

Greets,
DIck
Nov 16 '05 #5

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

Similar topics

0
3916
by: Karin | last post by:
Hi! I have two ListBoxes, lbFood and lbClothes, bound to two different ArrayLists of Stores. DisplayMember = "Name" and ValueMember = "Id". I would like to select a rows in the lists using ids from a SqlDataReader but so far I have only managed to set SelectedValue = null.... Any Ideas? Thanks! /K
5
5922
by: John O'Donnell | last post by:
Hello on my asp.net code I load up a listbox on the page_load method. The problem is that while the items I have added in the page_load do appear when i run the page, i am unable to get the value of the selected item using listbox.selectedvalue etc. somehow the server side is unaware that i have selected an item. In fact if i set the listbox.selecteditem=0; it always returns this item
12
622
by: Andy Sutorius | last post by:
Hi, I am getting the error "Invalid attempt to read when no data is present" when I run the following code. I have checked the sql and it is returning data. What am I missing? string sqlGetEmail = "SELECT content, subject FROM tblContent WHERE id = " + Convert.ToInt16(ddlChooseEmail.SelectedValue.ToString()); SqlCommand cmdGetEmail = new SqlCommand(sqlGetEmail, strConnString); SqlDataReader dtrEmail = cmdGetEmail.ExecuteReader();
4
2675
by: CrimeMaster | last post by:
HI I have an Exe file when it runs ,there are just two controls on the window.One is a ListBox and another is a Multi line Rich Edit control.When i click on an item in the list some text is displayed in the Edit control.Exe have a Hunderds of items in the List controls,which display some text in edit control against an item selection. I want to read that data which is displayed in edit control against the selection of an item in list...
1
3232
by: fiaolle | last post by:
Hi I Can't get the MSDN's example for Listbox to work.The example is below. I get an error in Listbox's sub ListBox1_SelectedValueChanged at row textBox1.Text = ListBox1.SelectedValue and it happens after the line ListBox1.ValueMember = "ShortName" is run. The error says Cast from type 'USState' to type 'String' is not valid. I don't know why the code doesn't work, can anyone please explain that for me.
1
3188
by: IMK | last post by:
Hello all, Sorry if this issue has come up already but I am new to vb.net. Thanks. I am trying to retrieve the selectedvalues from a multiselect list box in a vb 2005 winform. Here is the code I am using so far: dim i as object For Each i In Me.lstUnitPicklist.SelectedItems
0
1053
by: Franchdream | last post by:
I use the AccessDataSource to fill the data for Listbox. I doesn't use the DataSet. But when I check button to update the database, the Listbox can't fresh the data at onece. And the Code which behind is no use: private void setDataBind() { AccessDataSource2.Select(DataSourceSelectArguments.Empty); AccessDataSource3.Select(DataSourceSelectArguments.Empty); ListBox2.DataBind();
2
9959
by: sree reddy | last post by:
..cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls;
1
2005
by: =?Utf-8?B?TWFoZXNoIE5pbWJhbGthcg==?= | last post by:
On my page, I have ObjectDataSource and ListView. In Oninit method, I am setting selected value of Listbox. In above scenario, it throws error if I set SelectedValue property of ListBox and if that item does not exist in ListBox collection. "has a SelectedValue which is invalid because it does not exist " I want to suppress this error if that item to be have selected does not exist in collection.
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10093
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9952
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8976
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7500
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2880
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.