M
Mani25
Guest
Hi,
I am hoping some one can help me. I am writing a code where I am using an input box to get values. if the value is <= 0, I should get an error message indicating invalid number. If the value is left blank or in a string format then I should get another input error message. The first part of my code is working but If I leave the input field blank or input string I get an error message. I am not sure what I am doing wrong.
Here is my code:
intProductNumber = int.Parse(Interaction.InputBox("Enter the Product Number"));
try
{
if (intProductNumber <= 0)
{
MessageBox.Show("Invalid Product Number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, ex.GetType().ToString());
}
Here are details of the exception message:
System.FormatException
HResult=0x80131537
Message=Input string was not in a correct format.
Source=mscorlib
StackTrace:
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Int32.Parse(String s)
at test11.Form1.btnCreateStdSki_Click(Object sender, EventArgs e) in C:\Users\Manisha\source\repos\test11\Form1.cs:line 40
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at test11.Program.Main() in C:\Users\Manisha\source\repos\test11\Program.cs:line 19
This exception was originally thrown at this call stack:
[External Code]
test11.Form1.btnCreateStdSki_Click(object, System.EventArgs) in Form1.cs
[External Code]
test11.Program.Main() in Program.cs
Continue reading...
I am hoping some one can help me. I am writing a code where I am using an input box to get values. if the value is <= 0, I should get an error message indicating invalid number. If the value is left blank or in a string format then I should get another input error message. The first part of my code is working but If I leave the input field blank or input string I get an error message. I am not sure what I am doing wrong.
Here is my code:
intProductNumber = int.Parse(Interaction.InputBox("Enter the Product Number"));
try
{
if (intProductNumber <= 0)
{
MessageBox.Show("Invalid Product Number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, ex.GetType().ToString());
}
Here are details of the exception message:
System.FormatException
HResult=0x80131537
Message=Input string was not in a correct format.
Source=mscorlib
StackTrace:
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Int32.Parse(String s)
at test11.Form1.btnCreateStdSki_Click(Object sender, EventArgs e) in C:\Users\Manisha\source\repos\test11\Form1.cs:line 40
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at test11.Program.Main() in C:\Users\Manisha\source\repos\test11\Program.cs:line 19
This exception was originally thrown at this call stack:
[External Code]
test11.Form1.btnCreateStdSki_Click(object, System.EventArgs) in Form1.cs
[External Code]
test11.Program.Main() in Program.cs
Continue reading...