Check if int from textbox is empty + cast [duplicate]












0















This question already has an answer here:




  • How can I convert String to Int?

    28 answers




I have the following form:



enter image description here



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?










share|improve this question













marked as duplicate by HimBromBeere c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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




    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










  • are you sure you are passing number like this "12"
    – Saif
    Nov 23 '18 at 10:18
















0















This question already has an answer here:




  • How can I convert String to Int?

    28 answers




I have the following form:



enter image description here



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?










share|improve this question













marked as duplicate by HimBromBeere c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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




    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










  • are you sure you are passing number like this "12"
    – Saif
    Nov 23 '18 at 10:18














0












0








0








This question already has an answer here:




  • How can I convert String to Int?

    28 answers




I have the following form:



enter image description here



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?










share|improve this question














This question already has an answer here:




  • How can I convert String to Int?

    28 answers




I have the following form:



enter image description here



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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 10:14









user3127554

65112




65112




marked as duplicate by HimBromBeere c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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 c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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




    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










  • are you sure you are passing number like this "12"
    – Saif
    Nov 23 '18 at 10:18


















  • What framework are you using?
    – mahlatse
    Nov 23 '18 at 10:15






  • 1




    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










  • are you sure you are passing number like 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












1 Answer
1






active

oldest

votes


















2














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);





share|improve this answer




























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    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);





    share|improve this answer


























      2














      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);





      share|improve this answer
























        2












        2








        2






        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);





        share|improve this answer












        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);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 10:16









        Rahul

        62k124381




        62k124381















            Popular posts from this blog

            Catalogne

            Violoncelliste

            Héron pourpré