Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I've been asked to fix an issue with existing .NET forms code. There's a combo box populated with server names from a database. When the user selects a different server from the combo, the new server ID number is saved into a field in the 'params' table to record it as the last server selected. That way, when the app is started up the next day, it always displays the same server as when the app was last used.

This worked until the very first entry in the list was selected. My hunch was that this would be due to the index being 0 and sure enough the code in SelectedIndexChanged contains...

if (Convert.ToInt32(cmbServer.SelectedIndex) > 0)
{
   int iServerID = Convert.ToInt32(cmbServer.SelectedValue.ToString());
   PlattyData.SaveDefaultServer(iServerID);
}


The SelectedIndex is set on initialisation to -1 so I changed the IF line to :
if (Convert.ToInt32(cmbServer.SelectedIndex) > -1)


However, when the selectedindex = 0 the cmbServer.SelectedValue.ToString()) gives an error of "Input string was not in a correct value"

So basically as the code stands, any server selected other than the first works fine, but the first server (with an index of 0) causes an error.

Any ideas on a more appropriate check would be greatly appreciated!!

EDIT

The contents of cmbServer.SelectedValue when it's working properly is the ServerID number eg 23. When it fails, cmbServer.SelectedValue = "System.Data.DataView"

The fill code is :
private void SetupServerCombo()
{
    // 1 Fill Combo
    DataTable dt = new DataTable();

    dt = PlattyData.GetServers();
    cmbServer.SelectedIndex = -1;
    cmbServer.DataSource = dt;
    cmbServer.DisplayMember = "ServerName";
    cmbServer.ValueMember = "ServerID";

    // 2 Get default server
    int iDefaultServerID = -1;
    iDefaultServerID = PlattyData.GetDefaultServerID();

    cmbServer.SelectedValue = iDefaultServerID;
}
Posted
Updated 27-Mar-12 11:42am
v5
Comments
[no name] 27-Mar-12 11:16am    
Have you validated what the values is?
sjelen 27-Mar-12 11:37am    
cmbServer.SelectedIndex is already integer, no need to convert it.
You didn't show how you populate combobox items.
And the error probably is not caused by: cmbServer.SelectedValue.ToString()
It's Convert.ToInt32() giving the error, as it can not convert the value to int.
Put a breakpoint on that line and see the value of cmbServer.SelectedValue.

Your problem is here:
C#
cmbServer.SelectedValue = iDefaultServerID;

This is not the way to 'preselect' an item.
Try this instead:
C#
if (cmbServer.Items.IndexOf(iDefaultServerID) > -1)
    cmbServer.SelectedIndex = cmbServer.FindStringExact(iDefaultServerID.ToString());
else
    cmbServer.SelectedIndex = 0; // if serverId not found, default to first entry 
 
Share this answer
 
v2
Comments
aidin Tajadod 27-Mar-12 13:30pm    
you answer seems better than mine, I think you need to check it first if it can be found!
anyway my 5!
sjelen 28-Mar-12 5:41am    
Yes, checking would be a good precaution, but since both iDefaultServerID and list of servers come from database I assumed the data is OK. Anyway you don't need a loop to check this, see updated solution.
aidin Tajadod 29-Mar-12 1:09am    
yes, your are right. I was not aware of that function! I have learnt some thing new.
Thank you.
recoil101 27-Mar-12 14:43pm    
Thanks, I understand your solution and have changed the code. Unfortunately, my original problem still occurs at exactly the same point.
sjelen 28-Mar-12 5:32am    
Put a breakpoint in SetupServerCombo() and look at the data returned by PlattyData.GetServers(), specially the first row. Does anything look strange there?
hi,

this might be your problem:
At the first time when you want to set your combo by this code
cmbServer.SelectedValue = iDefaultServerID;

the ideFaultServerID is not defined in your combo! so It can not set it correct!

So do this


int index=dt.Columns["ServerID"].Ordinal;
bool found=false;
  for (int i = 0; i < comboBox1.Items.Count; i++)
{
 if (((DataRow) comboBox1.Items[i]).ItemArray[i]==iDefaultServerID)
 {
   faound=true;
   comboBox1.selectedIndex=i;
   break;
 }
}
if (!found)    comboBox1.selectedIndex=0;


if it works, you may wnat to improve this code!
 
Share this answer
 
v2
I may see the problem:

C#
if (Convert.ToInt32(cmbServer.SelectedIndex) > 0)
{
   int iServerID = Convert.ToInt32(cmbServer.SelectedValue.ToString());
   PlattyData.SaveDefaultServer(iServerID);
}


Maybe the Convert.ToInt32(cmbServer.SelectedIndex) > 0 should be Convert.ToInt32(cmbServer.SelectedIndex) >= 0
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900