Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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:14pm
Edited 24-Feb-13 23:22pm
v3
Comments
Vardhan Desai at 25-Feb-13 5:18am
   
Have you given any condition for Numeric if yes then Let me see that codes.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 at 25-Feb-13 5:35am
   
i have to not insert '0' or anything else in it.
OriginalGriff at 25-Feb-13 5:55am
   
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 at 25-Feb-13 5:48am
   
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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

try this one
If txtMRNO.Text.Length = 0 Then
          code here
       End If
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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  

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

  Print Answers RSS
0 OriginalGriff 210
1 Richard MacCutchan 100
2 kbrandwijk 90
3 ProgramFOX 80
4 Mukesh Bhagat 65
0 Sergey Alexandrovich Kryukov 9,050
1 OriginalGriff 8,151
2 CPallini 2,613
3 Richard MacCutchan 2,221
4 Abhinav S 1,928


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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100