Click here to Skip to main content
15,902,879 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Updated 24-Feb-13 23:22pm
v3
Comments
MalwareTrojan 25-Feb-13 5:18am    
Have you given any condition for Numeric if yes then Let me see that codes.

Simple: don't try to do a convert:
C#
if (txt_CheqNo.Text == "")
    {
    objdal.Cheq_No = 0;
    }
else
    {
    objdal.Cheq_No = Convert.ToInt16(txt_CheqNo.Text);
    }
But a better way would be:
C#
objdal.Cheq_No = 0;
if (!string.IsNullOrWhiteSpace(txt_CheqNo.Text))
    {
    objdal.Cheq_No = Convert.ToInt16(txt_CheqNo.Text);
    }
 
Share this answer
 
Comments
Member 9511889 25-Feb-13 5:35am    
i have to not insert '0' or anything else in it.
OriginalGriff 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 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
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/[^]
 
Share this answer
 
best way is below written code which will not give parser error.

C#
Int16 _cheq_No;
Int16.TryParse(txt_CheqNo.Text, out _cheq_No);
objdal.Cheq_No = _cheq_No;
 
Share this answer
 
v3
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);
}
 
Share this answer
 
v2
try this one
VB
If txtMRNO.Text.Length = 0 Then
          code here
       End If
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900