Check if int from textbox is empty + cast [duplicate]
This question already has an answer here:
How can I convert String to Int?
28 answers
I have the following form:

My field Quantity should be an int but I want to implement error checking.
This is my code:
private void btn_Create_Click(object sender, EventArgs e)
{
bool exit = false;
if (String.IsNullOrEmpty(tb_Quantity.Text))
{
lbl_Error.Visible = true;
lbl_Error.Text = "Check required values !";
exit = true;
}
int Quantity = int.Parse(tb_Quantity.Text.Trim());
if (!exit)
{
MessageBox.Show("Ready to be created!");
}
}
The error that I get if my quantity is empty:
An unhandled exception of type 'System.FormatException' occurred in
mscorlib.dll
Additional information: Input string was not in a correct format.
What am I doing wrong?
c# validation
marked as duplicate by HimBromBeere
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 10:16
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How can I convert String to Int?
28 answers
I have the following form:

My field Quantity should be an int but I want to implement error checking.
This is my code:
private void btn_Create_Click(object sender, EventArgs e)
{
bool exit = false;
if (String.IsNullOrEmpty(tb_Quantity.Text))
{
lbl_Error.Visible = true;
lbl_Error.Text = "Check required values !";
exit = true;
}
int Quantity = int.Parse(tb_Quantity.Text.Trim());
if (!exit)
{
MessageBox.Show("Ready to be created!");
}
}
The error that I get if my quantity is empty:
An unhandled exception of type 'System.FormatException' occurred in
mscorlib.dll
Additional information: Input string was not in a correct format.
What am I doing wrong?
c# validation
marked as duplicate by HimBromBeere
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 10:16
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
What framework are you using?
– mahlatse
Nov 23 '18 at 10:15
1
useTryParseinstead ofParse.
– HimBromBeere
Nov 23 '18 at 10:16
WinForms is what i'm using
– user3127554
Nov 23 '18 at 10:16
are you sure you are passingnumberlike this"12"
– Saif
Nov 23 '18 at 10:18
add a comment |
This question already has an answer here:
How can I convert String to Int?
28 answers
I have the following form:

My field Quantity should be an int but I want to implement error checking.
This is my code:
private void btn_Create_Click(object sender, EventArgs e)
{
bool exit = false;
if (String.IsNullOrEmpty(tb_Quantity.Text))
{
lbl_Error.Visible = true;
lbl_Error.Text = "Check required values !";
exit = true;
}
int Quantity = int.Parse(tb_Quantity.Text.Trim());
if (!exit)
{
MessageBox.Show("Ready to be created!");
}
}
The error that I get if my quantity is empty:
An unhandled exception of type 'System.FormatException' occurred in
mscorlib.dll
Additional information: Input string was not in a correct format.
What am I doing wrong?
c# validation
This question already has an answer here:
How can I convert String to Int?
28 answers
I have the following form:

My field Quantity should be an int but I want to implement error checking.
This is my code:
private void btn_Create_Click(object sender, EventArgs e)
{
bool exit = false;
if (String.IsNullOrEmpty(tb_Quantity.Text))
{
lbl_Error.Visible = true;
lbl_Error.Text = "Check required values !";
exit = true;
}
int Quantity = int.Parse(tb_Quantity.Text.Trim());
if (!exit)
{
MessageBox.Show("Ready to be created!");
}
}
The error that I get if my quantity is empty:
An unhandled exception of type 'System.FormatException' occurred in
mscorlib.dll
Additional information: Input string was not in a correct format.
What am I doing wrong?
This question already has an answer here:
How can I convert String to Int?
28 answers
c# validation
c# validation
asked Nov 23 '18 at 10:14
user3127554
65112
65112
marked as duplicate by HimBromBeere
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 10:16
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by HimBromBeere
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 10:16
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
What framework are you using?
– mahlatse
Nov 23 '18 at 10:15
1
useTryParseinstead ofParse.
– HimBromBeere
Nov 23 '18 at 10:16
WinForms is what i'm using
– user3127554
Nov 23 '18 at 10:16
are you sure you are passingnumberlike this"12"
– Saif
Nov 23 '18 at 10:18
add a comment |
What framework are you using?
– mahlatse
Nov 23 '18 at 10:15
1
useTryParseinstead ofParse.
– HimBromBeere
Nov 23 '18 at 10:16
WinForms is what i'm using
– user3127554
Nov 23 '18 at 10:16
are you sure you are passingnumberlike this"12"
– Saif
Nov 23 '18 at 10:18
What framework are you using?
– mahlatse
Nov 23 '18 at 10:15
What framework are you using?
– mahlatse
Nov 23 '18 at 10:15
1
1
use
TryParse instead of Parse.– HimBromBeere
Nov 23 '18 at 10:16
use
TryParse instead of Parse.– HimBromBeere
Nov 23 '18 at 10:16
WinForms is what i'm using
– user3127554
Nov 23 '18 at 10:16
WinForms is what i'm using
– user3127554
Nov 23 '18 at 10:16
are you sure you are passing
number like this "12"– Saif
Nov 23 '18 at 10:18
are you sure you are passing
number like this "12"– Saif
Nov 23 '18 at 10:18
add a comment |
1 Answer
1
active
oldest
votes
Instead use TryParse() like below which won't throw exception in-case the casting fails (if it's NaN)
int.TryParse(tb_Quantity.Text.Trim(), out int quantity);
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Instead use TryParse() like below which won't throw exception in-case the casting fails (if it's NaN)
int.TryParse(tb_Quantity.Text.Trim(), out int quantity);
add a comment |
Instead use TryParse() like below which won't throw exception in-case the casting fails (if it's NaN)
int.TryParse(tb_Quantity.Text.Trim(), out int quantity);
add a comment |
Instead use TryParse() like below which won't throw exception in-case the casting fails (if it's NaN)
int.TryParse(tb_Quantity.Text.Trim(), out int quantity);
Instead use TryParse() like below which won't throw exception in-case the casting fails (if it's NaN)
int.TryParse(tb_Quantity.Text.Trim(), out int quantity);
answered Nov 23 '18 at 10:16
Rahul
62k124381
62k124381
add a comment |
add a comment |
What framework are you using?
– mahlatse
Nov 23 '18 at 10:15
1
use
TryParseinstead ofParse.– HimBromBeere
Nov 23 '18 at 10:16
WinForms is what i'm using
– user3127554
Nov 23 '18 at 10:16
are you sure you are passing
numberlike this"12"– Saif
Nov 23 '18 at 10:18