allanmark
asked on
ListBox.SelectedValue not working
Greetings all
I, today, attempted a simple WIndows based application and ran into problems with the "SelectedValue" proeprty of the ListBox (see attached snippet).
In "frmMain_Load" the code executes fine. I copied the same code into "LoadAddresses" and "GetSelectedClientId" and it fails there with "Input string was not in a correct format."
Any ideas?
In advance, thanks!!
allanmark
I, today, attempted a simple WIndows based application and ran into problems with the "SelectedValue" proeprty of the ListBox (see attached snippet).
In "frmMain_Load" the code executes fine. I copied the same code into "LoadAddresses" and "GetSelectedClientId" and it fails there with "Input string was not in a correct format."
Any ideas?
In advance, thanks!!
allanmark
public partial class frmMain : Form
{
private RealPeople.DataServices GetData;
private DataSet dsAddress;
private DataSet dsClient;
private DataView dvAddresses;
//---------------------------------------------------------------------------------------------
public frmMain()
{
InitializeComponent();
}
//---------------------------------------------------------------------------------------------
private void frmMain_Load(object sender, EventArgs e)
{
GetData = new RealPeople.DataServices();
LoadClients();
// These next 3 statements work fine here.
int rrr = Convert.ToInt32(lblClients.SelectedValue.ToString());
string yodd = lblClients.SelectedValue.ToString();
int iodd = Convert.ToInt32(yodd);
}
//---------------------------------------------------------------------------------------------
private void LoadClients()
{
//DataSet dsClient = GetData.GetClients();
dsClient = GetData.GetClients();
lblClients.DataSource = dsClient.Tables["Clients"];
lblClients.DisplayMember = "Client_Name";
lblClients.ValueMember = "C_Id";
lblClients.SelectedIndex = 0;
}
//---------------------------------------------------------------------------------------------
private void LoadAddresses()
{
// These next 3 statements cause a problem here!!
int rrr = Convert.ToInt32(lblClients.SelectedValue.ToString());
string yodd = lblClients.SelectedValue.ToString();
int iodd = Convert.ToInt32(yodd);
// int iClient = GetSelectedClientId();
dsAddress = GetData.GetAddresses();
}
//---------------------------------------------------------------------------------------------
private void lblClients_SelectedIndexChanged(object sender, EventArgs e)
{
LoadAddresses();
}
//---------------------------------------------------------------------------------------------
private int GetSelectedClientId()
{
// These next 3 statements cause a problem here!!
int rrr = Convert.ToInt32(lblClients.SelectedValue.ToString());
string yodd = lblClients.SelectedValue.ToString();
int iodd = Convert.ToInt32(yodd);
// These next 2 also cause an error!
string valueString = lblClients.SelectedValue.ToString();
int result = Convert.ToInt32(valueString); // TODO: look at Int32.Parse instead of Convert.ToInt32
return result;
}
//---------------------------------------------------------------------------------------------
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Well... Then simply the input was not in the correct format. Go and debug you could and see what that SelectedItem.ToString() is.
obviously, the Convert.ToInt32 method is receiving a bad formatted parameter from the listbox
Check for me if the ListBox's SelectedIndex is not -1 at that line... could maybe be causing problems because of that.
ASKER
SelectedIndex is 0.
What confuses me is taht the code works in frm_Load.
What confuses me is taht the code works in frm_Load.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
If I look at Load_Clients() -- lblClients.ValueMember = "C_Id" and lblClients.SelectedIndex = 0 --- this causes the SelectedIndexChanged to fire, which calls LoadAddresses() -- where it fails.
If I comment out the code in LoadAddresses, then execution retuirns to frm_Load, when the SAME 3 lines execute ok -- which surely shows that the values are ok??
If I comment out the code in LoadAddresses, then execution retuirns to frm_Load, when the SAME 3 lines execute ok -- which surely shows that the values are ok??
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I have had many bugs when I changed the selected Index, and it caused like infinite loops...
I bet you anything that your @ selectedIndex 0, it says something like "Please select something". And then that "please select something" gets sent to the Convert function, which throws an error
I better approach would do this then:
private void lblClients_SelectedIndexCh anged(obje ct sender, EventArgs e)
{
if(!lockSelectedIndex && lblClients.SelectedIndex != 0){
LoadAddresses();
}
}
private void lblClients_SelectedIndexCh
{
if(!lockSelectedIndex && lblClients.SelectedIndex != 0){
LoadAddresses();
}
}
ASKER
I did as suggested (see snippet) and it still tried to execute. Alos, I need to execute that routine - when the Index is set to 0 (the first entry), the user needs to see the corresponding data.
Me = seriously confused!! :( :( :(
Me = seriously confused!! :( :( :(
public partial class frmMain : Form
{
private RealPeople.DataServices GetData;
private DataSet dsAddress;
private DataSet dsClient;
private DataView dvAddresses;
private bool lockSelectedIndex = false;
private void LoadClients()
{
//DataSet dsClient = GetData.GetClients();
dsClient = GetData.GetClients();
lblClients.DataSource = dsClient.Tables["Clients"];
lblClients.DisplayMember = "Client_Name";
lblClients.ValueMember = "C_Id";
lockSelectedIndex = true;
lblClients.SelectedIndex = 0;
lockSelectedIndex = false;
}
private void LoadAddresses()
{
int ttt = lblClients.SelectedIndex;
int tye = Convert.ToInt32(textBox1.Text);
int rrr = Convert.ToInt32(lblClients.SelectedValue.ToString());
string yodd = lblClients.SelectedValue.ToString();
int iodd = Convert.ToInt32(yodd);
int iClient = GetSelectedClientId();
dsAddress = GetData.GetAddresses();
}
private void lblClients_SelectedIndexChanged(object sender, EventArgs e)
{
if (!lockSelectedIndex)
LoadAddresses();
}
I need to understand what actually is in your ListBox
Can you please like tell us what is in it?
Thanks
Can you please like tell us what is in it?
Thanks
ASKER
In the selectbox is a name (eg. "Jones, Peter") and teh ValueMemebr conatisn the corresponding id (eg. 2).
As an experiment, I commented out the "SelectedIndex_Changed" event and manually called the LoadAdresses after LoadClients was done --- it works! So "SelectedIndex_Changed" is causing the problem and I'm not sur ehow to get around it -- I need to Load the addresses each time the user moves to another name in the listbox.
As an experiment, I commented out the "SelectedIndex_Changed" event and manually called the LoadAdresses after LoadClients was done --- it works! So "SelectedIndex_Changed" is causing the problem and I'm not sur ehow to get around it -- I need to Load the addresses each time the user moves to another name in the listbox.
As I asked earlier, could you, type out, the list of the listbox data... ie like this
<item1>
<item2>
..
..
I can't really help you unless I have an understand of what is going on...
<item1>
<item2>
..
..
I can't really help you unless I have an understand of what is going on...
ASKER
Tks 4 your patience :) :) :)
Sorry .. wasn;t sure what you wanted.
Here goes ...
<Bathalot, Brian>
<Brown, Andy>
<Jones, Kim>
<Sanderson, Sally>
<Smith, Paul>
<Van Der Merwe, Allan>
<Williams, George>
Sorry .. wasn;t sure what you wanted.
Here goes ...
<Bathalot, Brian>
<Brown, Andy>
<Jones, Kim>
<Sanderson, Sally>
<Smith, Paul>
<Van Der Merwe, Allan>
<Williams, George>
I am still quite confused where the SelectedValue Property was set for each of these elements
Could you do this:
type out:
<SelectedItem 1's String> <SelectedItem 1's Value>
<SelectedItem 2's String> <SelectedItem 2's Value>
.. .. etc
type out:
<SelectedItem 1's String> <SelectedItem 1's Value>
<SelectedItem 2's String> <SelectedItem 2's Value>
.. .. etc
I am sure you will find your problem when you do this, since that methods problem is from the selectedValue being in bad format
ASKER
Once again, many thanks for your patience!
On the SelectedValue issue -- my understanding was that when the user selects an entry in the listbox, or the index is set programamtically, the SelectedValue is automatically set. Maybe I'm wrong here .
At Breakpoint5, the SelectItem (sSI) contains "System.Data.DataRowView".
I sat down this morning and tidied up the code (more meaningful names, etc. Also placed breakpoints all over the show and some code on the lblClients_ValueMemberChan ged event.
Point of interest from this:
1. The lblClients_SelectedIndexCh anged event fires twice!. I thought that perhaps it was triggered by
default, when I assign the 0 value, so I commented this out. It still fires twice!!
2. At BREAKPOINT4 the string contains "System.Data.DataRowView"
3. At BREAKPOINT3 the string contains "System.Data.DataRowView"
4. At BREAKPOINT4 the string contains "System.Data.DataRowView"
5. At BREAKPOINT3 the string contains "System.Data.DataRowView"
6. At BREAKPOINT5 the string contains "4" (correct value)
7. At BREAKPOINT2 the string contains "4" (correct value)
8. At BREAKPOINT1 the string contains "4" (correct value)
On the SelectedValue issue -- my understanding was that when the user selects an entry in the listbox, or the index is set programamtically, the SelectedValue is automatically set. Maybe I'm wrong here .
At Breakpoint5, the SelectItem (sSI) contains "System.Data.DataRowView".
I sat down this morning and tidied up the code (more meaningful names, etc. Also placed breakpoints all over the show and some code on the lblClients_ValueMemberChan
Point of interest from this:
1. The lblClients_SelectedIndexCh
default, when I assign the 0 value, so I commented this out. It still fires twice!!
2. At BREAKPOINT4 the string contains "System.Data.DataRowView"
3. At BREAKPOINT3 the string contains "System.Data.DataRowView"
4. At BREAKPOINT4 the string contains "System.Data.DataRowView"
5. At BREAKPOINT3 the string contains "System.Data.DataRowView"
6. At BREAKPOINT5 the string contains "4" (correct value)
7. At BREAKPOINT2 the string contains "4" (correct value)
8. At BREAKPOINT1 the string contains "4" (correct value)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace RealPeopleAddresses
{
public partial class frmMain : Form
{
private RealPeople.DataServices GetData;
private DataSet dsAddress;
private DataSet dsClient;
private DataView dvAddresses;
// private bool lockSelectedIndex = false;
//---------------------------------------------------------------------------------------------
public frmMain()
{
InitializeComponent();
}
//---------------------------------------------------------------------------------------------
private void frmMain_Load(object sender, EventArgs e)
{
GetData = new RealPeople.DataServices();
LoadClients();
int iSelectedValue = Convert.ToInt32(lblClients.SelectedValue.ToString());
string sSelectedValue = lblClients.SelectedValue.ToString();
int iConverted = Convert.ToInt32(sSelectedValue);
int tt = 0; // BREAKPOINT1
}
//---------------------------------------------------------------------------------------------
private void LoadClients()
{
//DataSet dsClient = GetData.GetClients();
dsClient = GetData.GetClients();
lblClients.DataSource = dsClient.Tables["Clients"];
lblClients.DisplayMember = "Client_Name";
lblClients.ValueMember = "C_Id";
string sSelVal = lblClients.SelectedValue.ToString();
//lblClients.SelectedIndex = 0;
string sSelVal2 = lblClients.SelectedValue.ToString();
int yyyyy = 0; // BREAKPOINT2
}
//---------------------------------------------------------------------------------------------
private void LoadAddresses()
{
int iInd = lblClients.SelectedIndex;
string sSelectedValLA = lblClients.SelectedValue.ToString();
// int iClient = GetSelectedClientId();
// dsAddress = GetData.GetAddresses(1);
int t = 0; //BREAKPOINT3
}
//---------------------------------------------------------------/------------------------------
private void lblClients_SelectedIndexChanged(object sender, EventArgs e)
{
// if (!lockSelectedIndex)
string sSelectedValSI = lblClients.SelectedValue.ToString();
LoadAddresses(); // BREAKPOINT4
}
//---------------------------------------------------------------------------------------------
//private void PopulateAddresses()
//{
// dvAddresses = new DataView(dsAddress.Tables["Addresses"]);
// dvAddresses.Sort = "R_Name, A_AddressLine1";
// dvAddresses.RowFilter = "A_ClientId = " + lblClients.ValueMember.ToString();
// dgvAddress.DataSource = dvAddresses;
// //gvAnnualLeave.DataBind();
//}
//---------------------------------------------------------------------------------------------
private int GetSelectedClientId()
{
int rrr = Convert.ToInt32(lblClients.SelectedValue.ToString());
string yodd = lblClients.SelectedValue.ToString();
int iodd = Convert.ToInt32(yodd);
string valueString = lblClients.SelectedValue.ToString();
int result = Convert.ToInt32(valueString); // TODO: look at Int32.Parse instead of Convert.ToInt32
return result;
}
private void btnInsert_Click(object sender, EventArgs e)
{
Form frmDataWork = new frmDataWork();
frmDataWork.Show();
}
//---------------------------------------------------------------------------------------------
private void lblClients_ValueMemberChanged(object sender, EventArgs e)
{
string sSI = lblClients.SelectedItem.ToString();
string tttt = lblClients.SelectedValue.ToString(); ;
int rrr = Convert.ToInt32(lblClients.SelectedValue.ToString());
int t = 0; // BREAKPOINT 5
}
}
}
Why is "4" the correct selected value? I thought you said that the listbox contains:
<Bathalot, Brian>
<Brown, Andy>
<Jones, Kim>
<Sanderson, Sally>
<Smith, Paul>
<Van Der Merwe, Allan>
<Williams, George>
<Bathalot, Brian>
<Brown, Andy>
<Jones, Kim>
<Sanderson, Sally>
<Smith, Paul>
<Van Der Merwe, Allan>
<Williams, George>
ASKER
The 4 is the value stored in C_Id (the field asssigned to the ValueMember). It passed to a service that retrieves a dataset for that particular client.
ASKER
All I want out is the value (not the text dispalyed) - this is driving me banannas!! :( :( :(