Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 7th, 2008, 07:35 AM
Registered User
 
Join Date: Jan 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default which control is firing the exception

hi....
i am using a code like this :

double total = double.Parse(txtPF.Text) + double.Parse(txtIT.text);

where txtPF and txtIT are two text boxes on a win forrm.
if txt box doesn't contain any value it is firing exception.

i want :

try
{
double total = double.Parse(txtPF.Text) + double.Parse(txtIT.text);
}
catch(Exception exc)
{
  //some code through which i can find out exactly which text box fired the exception.so tht i can use :
  MessageBox.Show("please enter a value in "+ <name of tht txt box > +"field.");
//....
}

i've tried much bt nothing seems to work.
please help.

 
Old January 7th, 2008, 08:02 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Neither textbox is 'firing the exception'. Each textbox is quite happily returning its Text value as a string (an empty string in one instance). Its only after that that the exception is firing.

I would use validators if I were you, one, or even two for each box (on to check if the box is empty, the other to check it is a number).

/- Sam Judson : Wrox Technical Editor -/
 
Old January 7th, 2008, 08:07 AM
Authorized User
 
Join Date: Jul 2007
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to mujahidkhaleel
Default

Try this code

double dblPF;
try
{
 dblPF = double.Parse(txtPF.Text);
}
catch(Exception ex)
{
  MessageBox.Show("please enter a value in PF field.");
}

try
{
 //Now we are sure that there is no problem in txtPF sso try to add, if it throws exception then the problem must be in txtIT.
 total = double.Parse(txtPF.Text) + double.Parse(txtIT.Text);
}
catch(Exception exc)
{
  MessageBox.Show("please enter a value in IT field.");
}



Mujahid Khaleel
Web designing, development, E-commerce applications.
 
Old January 7th, 2008, 09:29 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Take a look at the Double.TryParse function.

--

Joe (Microsoft MVP - XML)
 
Old January 8th, 2008, 05:47 AM
Registered User
 
Join Date: Jan 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by samjudson
 Neither textbox is 'firing the exception'. Each textbox is quite happily returning its Text value as a string (an empty string in one instance). Its only after that that the exception is firing.

I would use validators if I were you, one, or even two for each box (on to check if the box is empty, the other to check it is a number).

/- Sam Judson : Wrox Technical Editor -/
thanks for a quick reply...
i've used validator for a number...but not for an empty string...
your idea seems to be a good one....
thanks again...

now i think i shud've ask the question like this..:

if a statement contains more than one control's participation, and then fires an exception...then is there any way to know exactly which control's action was responsible for the exception....

thanks in advance for this one....

 
Old January 8th, 2008, 05:55 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

And the answer is, no. As I said, the exception is not firing from inside the control, therefore the stack trace would not contain the control anywhere in its history. Even then, trying to analyse the stack trace would be the wrong way to do this.

On an aside, if you expecting an action to fail then using exceptions to trap the error is not the most performant way to perform this - far better to do two TryParse operations, check if either fails, then add the two resulting numbers.

/- Sam Judson : Wrox Technical Editor -/
 
Old January 8th, 2008, 07:24 AM
Registered User
 
Join Date: Jan 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by samjudson
 And the answer is, no. As I said, the exception is not firing from inside the control, therefore the stack trace would not contain the control anywhere in its history. Even then, trying to analyse the stack trace would be the wrong way to do this.

On an aside, if you expecting an action to fail then using exceptions to trap the error is not the most performant way to perform this - far better to do two TryParse operations, check if either fails, then add the two resulting numbers.

/- Sam Judson : Wrox Technical Editor -/
aha....
tht seems to be exactly the defination i was looking for....
thanks a lot....


 
Old January 8th, 2008, 07:27 AM
Registered User
 
Join Date: Jan 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by joefawcett
 Take a look at the Double.TryParse function.

--

Joe (Microsoft MVP - XML)

hi....
tryParse is working perfectly....
thanks for the support....

 
Old January 8th, 2008, 07:30 AM
Registered User
 
Join Date: Jan 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by mujahidkhaleel
 Try this code

double dblPF;
try
{
dblPF = double.Parse(txtPF.Text);
}
catch(Exception ex)
{
MessageBox.Show("please enter a value in PF field.");
}

try
{
//Now we are sure that there is no problem in txtPF sso try to add, if it throws exception then the problem must be in txtIT.
total = double.Parse(txtPF.Text) + double.Parse(txtIT.Text);
}
catch(Exception exc)
{
MessageBox.Show("please enter a value in IT field.");
}



Mujahid Khaleel
Web designing, development, E-commerce applications.

hi....
interesting solution...and working perfectly also...
thanks for ur support....

 
Old January 8th, 2008, 02:17 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

raykaash - A forum tip: It's not necessary to quote every response when replying. We can follow the thread on the forum itself. If you want to address a specific statement in a previous post then by all means quote just that statement to provide response context.

-Peter





Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception handling in a custom control flegel ASP.NET 2.0 Professional 0 February 20th, 2007 10:26 AM
Windows control events not firing bmains C# 2005 2 June 5th, 2006 09:11 PM
DropDownList not firing pbyrum ASP.NET 1.0 and 1.1 Basics 7 October 12th, 2004 11:58 AM
Events firing for control in one instance, but... thanie2001 ASP.NET 1.0 and 1.1 Professional 3 January 23rd, 2004 03:01 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.