Click here to Skip to main content
16,021,115 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello. I want to know, how do I make an exception if the combobox is not selected. So far, it will come out Error: Object reference not set to an instance of an objet. If possible, I want it to come out like "Please select an item from the list." or something like that.
C#
string LabName = comboBoxLabNumber.SelectedItem.ToString();
string LabNumber = LabName;
string LabProblem = comboBoxProblem.SelectedItem.ToString();
string LabProb = LabProblem;


Or should I just do it using if/else? Any idea?

Thanks
Posted
Updated 22-Nov-10 21:21pm
v3

Hi,

using SelectedIndex for combobox.

if(comboBoxLabNumber.SelectedIndex > -1)
{
  LabName = comboBoxLabNumber.SelectedItem.ToString();
}
else
{
  MessageBox.Show("Please select an item!");
}


Try this way...

Cheers :)
 
Share this answer
 
Comments
Little Daffodil 23-Nov-10 5:08am    
I tried this:

if (comboBoxLabNumber.SelectedIndex > -1 | comboBoxProblem.SelectedIndex > -1)
{
string LabName = comboBoxLabNumber.SelectedItem.ToString();
string LabNumber = LabName;

string LabProblem = comboBoxProblem.SelectedItem.ToString();
string LabProb = LabProblem;
}
else
{
MessageBox.Show("Please select an item!");
}


However, it comes out as error : The name 'LabName' and 'LabProblem' does not exist in the current context.
Toniyo Jackson 23-Nov-10 5:17am    
Declare Strings outside the if condition....
Little Daffodil 23-Nov-10 5:24am    
I'm blur. Does it mean this way?

if (comboBoxLabNumber.SelectedIndex > -1 | comboBoxProblem.SelectedIndex > -1)
{

}
else
{
MessageBox.Show("Please select an item!");
}

string LabName = comboBoxLabNumber.SelectedItem.ToString();
string LabNumber = LabName;

string LabProblem = comboBoxProblem.SelectedItem.ToString();
string LabProb = LabProblem;
Little Daffodil 23-Nov-10 5:27am    
I tried this:

if (comboBoxLabNumber.SelectedIndex < 0 | comboBoxProblem.SelectedIndex <0)
{
MessageBox.Show("Please select the lab number and the problem");
}

string LabName = comboBoxLabNumber.SelectedItem.ToString();
string LabNumber = LabName;

string LabProblem = comboBoxProblem.SelectedItem.ToString();
string LabProb = LabProblem;

Yes, it works. However, this message of "Error: Object reference not set to an instance of an objet." still comes out.
T.Saravanann 23-Nov-10 5:47am    
May be your problem occurred in second combobox because first you bind the first combo value and then second combo value. Are you call this condition in combobox selected index event means..try this code...

if(comboBoxLabNumber.SelectedIndex > 0)
{
LabName = comboBoxLabNumber.SelectedItem.ToString();
}
else
{
//LabName = string.Empty;
MessageBox.Show("Please select an item!");
}

if(comboBoxProblem.SelectedIndex > 0)
{
LabProblem = comboBoxProblem.SelectedItem.ToString();
}
else
{
//LabProblem = string.Empty;
MessageBox.Show("Please select an item!");
}
Then handle the exception, for instance
C#
try
{
    LabName = comboBoxLabNumber.SelectedItem.ToString();
}
catch (Exception e)
{
    MessageBox.Show("Please select an item from the list");
    // here take the appropriate action.
}
 
Share this answer
 
v3
Comments
Dalek Dave 23-Nov-10 4:40am    
Good Call.

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