all groups > vj# > april 2006 >
You're in the

vj#

group:

Try/catch exception using Combobox


Try/catch exception using Combobox pmetz
4/19/2006 5:43:57 PM
vj#:
I'm attempting exception handling for a combobox. I wish an exception
to be raised if the user does not select an item from the combobox, but
I'm not sure of the correct code. Here is what I have now:

try
{
if (cboDwellingType.get_SelectedItem().ToString == null);
{
MessageBox.Show("Please select the Dwelling Type from the
dropdown menu.");
cboDwellingType.Focus();
throw new Exception ("Error updating Local database");
}
}
catch
{
if (err.getMessage() == "Error updating Local database.")
MessageBox.Show(err.getMessage());
}
Re: Try/catch exception using Combobox pmetz
4/19/2006 5:46:42 PM
SORRY...FORGOT THE PARAMETER IN THE CATCH...HERE'S THE UPDATED CODE

try
{
if (cboDwellingType.get_SelectedItem().ToString == null);
{
MessageBox.Show("Please select the Dwelling Type from the
dropdown menu.");
cboDwellingType.Focus();
throw new Exception ("Error updating Local database");
}
}
catch (Exception err)
{
if (err.getMessage() == "Error updating Local database.")
MessageBox.Show(err.getMessage());
}
Re: Try/catch exception using Combobox chrismo
4/23/2006 6:37:34 PM
Your code looks okay (except it's probably not get_SelectedItem -
rather getSelectedItem) ... but there's no point in throwing an
exception just to catch it and then display the message -- unless
there's a lot more code inside the try before the catch.

Are you having a specific problem with it?
Re: Try/catch exception using Combobox Lars-Inge Tønnessen (VJ# MVP)
6/4/2006 5:52:19 PM

Your "throw new Exception ("Error updating Local database");" could be
better implemented. Exceptions should only be thrown when there are
something very vrong you can not handle with your code. Your code should not
throw an exception when the database cannot be updated.

Best Regards,
Lars-Inge Tønnessen
VJ# MVP

[quoted text, click to view]

Re: Try/catch exception using Combobox George Birbilis
6/14/2006 12:00:00 AM
[quoted text, click to view]

Use validator controls instead to "force" the user to select something at
the combobox. There are some from MS (for both WinForms and WebForms) and
some from third parties too (for example see metabuilders.com for some cool
ASP.net ones)

---------------
George Birbilis <birbilis@kagi.com>
Microsoft MVP J# 2004-2006
Borland Spirit of Delphi
http://www.kagi.com/birbilis

AddThis Social Bookmark Button