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());
}
pmetz - 20 Apr 2006 01:46 GMT
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());
}
chrismo - 24 Apr 2006 02:37 GMT
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?
Lars-Inge Tønnessen (VJ# MVP) - 04 Jun 2006 16:52 GMT
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
> try
> {
[quoted text clipped - 11 lines]
> MessageBox.Show(err.getMessage());
> }
George Birbilis - 14 Jun 2006 11:42 GMT
> 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...
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