Loading ...

Input string was not in a correct format in .net | CodeAsp.Net

Input string was not in a correct format in .net

 /5
0 (0votes)

I have seen many times a question on Internet which is “Input String was not in a correct format”. I have also faced this error. This is run time error.

Types of Error

  1. Compile time error (Syntax error, etc.)
  2. Runtime error (Division by zero, etc.)
As I told you that “Input string was not in a correct format” is a runtime error. Mostly it comes when we convert a string to a number or convert a NULL value to other type. Below I am giving some example when this error comes and solutions also.
Example 1.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        
        <asp:TextBox ID="txtYourAge" runat="server"></asp:TextBox>
        <asp:Button ID="btnTest" runat="server" onclick="btnTest_Click" Text="click" />
   
    </div>
    </form>
</body>
</html>


using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page 
{
       
    protected void btnTest_Click(object sender, EventArgs e)
    {
        int age = Int32.Parse(txtYourAge.Text);
    }
}

Error - There is one textbox and one button. If user enter decimal value in textbox and click on button, it will give same error “Input string was not in a correct format”.

Solution - There is one solution which will work perfectly in integer or decimal case.

    protected void btnTest_Click(object sender, EventArgs e)
    {
        double age = double.Parse(txtYourAge.Text);
    }


Error - If user click on button without enter any value in textbox. It will give same error “Input string was not in a correct format”.
Solution - you have to check textbox value is empty or not.

    protected void btnTest_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(txtYourAge.Text))
        {
            double age = double.Parse(txtYourAge.Text);
        }
    }


Error - If user enters string value in textbox. It will give same error “Input string was not in a correct format”.
Solution - For this situation, you can put validation on textbox. By taking advantages of validation, user can’t enter a string value in textbox.
Example 2.
Whenever we get value from the database, it would give same error “Input string was not in a correct format” because of NULL value. Programmers should check values whether this is null or not null, here we can use string.IsNullOrEmpty().
Hope this helps you.

Comments (1)

   
sanjayjgoswami
This one is also work        double age ;        if(Double.TryParse(txtYourAge.Text,age)) // Return true if Valid        {            // Valid Input assing value into "age" variable                   }        else        {            // Invalid Input        }
8/19/2010
 · 
by
  • :*
  • :*
  • :
 *

Top Posts