Click here to Skip to main content
Sign Up to vote bad
good
See more: C#ASP.NET
if (txt_CheqNo.Text == "")
                {
                    objdal.Cheq_No =Convert.ToInt16("");
                }
                else
                {
                    objdal.Cheq_No = Convert.ToInt16(txt_CheqNo.Text);
                }
i have textbox wich store numeric value.
but when textbox i leave empty,it creates error.
error:Input string was not in a correct format.
 
i have to not put requierfield to fill textbox.i have the situation in many case to leave it empty.i have to keep it complatly blank.Not '0' or anything else to enter in it.
 
how to solve this?
 
Posted 24 Feb '13 - 23:14
Edited 24 Feb '13 - 23:22

Comments
Vardhan Desai - 25 Feb '13 - 5:18
Have you given any condition for Numeric if yes then Let me see that codes.

5 solutions

Simple: don't try to do a convert:
if (txt_CheqNo.Text == "")
    {
    objdal.Cheq_No = 0;
    }
else
    {
    objdal.Cheq_No = Convert.ToInt16(txt_CheqNo.Text);
    }
But a better way would be:
objdal.Cheq_No = 0;
if (!string.IsNullOrWhiteSpace(txt_CheqNo.Text))
    {
    objdal.Cheq_No = Convert.ToInt16(txt_CheqNo.Text);
    }
  Permalink  
Comments
Member 9511889 - 25 Feb '13 - 5:35
i have to not insert '0' or anything else in it.
OriginalGriff - 25 Feb '13 - 5:55
Either you assign it a value, or you don't - int datatypes *always* have a value, unless they are declared as nullable (which is very unusual). You could just "leave it alone" and not assign it a value, but that is likely to cause even more problems as it will retain the previous value.
Jegan Thiyagesan - 25 Feb '13 - 5:48
Hi, "int" is a primitive type and it will always have a value, you cannot assign null to a primitive type. if you want you can make your "objdal" to a nullable "Integer" type or a "string" type. Regards Jegan
visit this link it might help you.
this also traps other non numeric inputs
 
http://social.msdn.microsoft.com/forums/en-US/winforms/thread/84990ad2-5046-472b-b103-f862bfcd5dbc/[^]
  Permalink  
best way is below written code which will not give parser error.
 
Int16 _cheq_No;
Int16.TryParse(txt_CheqNo.Text, out _cheq_No);
objdal.Cheq_No = _cheq_No;
  Permalink  
If you want to set null values then you can do like this
but objdal.Cheq_No should be nullable.
if (txt_CheqNo.Text == "")
{
objdal.Cheq_No =null;
}
else
{
objdal.Cheq_No = Convert.ToInt16(txt_CheqNo.Text);
}
 
Other wise dont convert when text box is null or empty.
if (txt_CheqNo.Text != "")
{
objdal.Cheq_No = Convert.ToInt16(txt_CheqNo.Text);
}
  Permalink  
try this one
If txtMRNO.Text.Length = 0 Then
          code here
       End If
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 475
1 OriginalGriff 315
2 Maciej Los 260
3 Slacker007 240
4 Aarti Meswania 185
0 Sergey Alexandrovich Kryukov 8,953
1 OriginalGriff 7,134
2 CPallini 3,758
3 Rohan Leuva 3,066
4 Maciej Los 2,528


Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 25 Feb 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid