locked
Error : Input string was not in correct format RRS feed

  • Question

  • Hi All !!

    I'm getting an Error "Input string was not in correct format", When i tried to insert empty Age textbox & Empty Phone No textBox to the SQL Table, Pls Help me to solve this !!

    I'l post the Full Code as Below !!

    1.Patient Class :
     
    public class Patient_Class
      {
         string _Id;
         string _Name = string.Empty;
         int _Age=0;
         string _Sex = string.Empty;
         string _Problem= string.Empty;
         Int64 _Phone=0;
         string _Address= string.Empty;
         string _Comments= string.Empty;
    
        public string Id
        {  
          get { return _Id; }
          set { _Id = value; }
        }    
        public string Name
        {      
          get { return _Name; }
          set { _Name = value; }
        }    
        public int Age
        {      
          get { return _Age; } 
          set { _Age = value; }
        }    
        public string Sex
        {      
          get { return _Sex; }
          set { _Sex = value; }
        }    
        public string Problem
        {      
          get { return _Problem; } 
          set { _Problem = value; }
        }    
        public Int64 Phone
        {     
          get { return _Phone; } 
          set { _Phone = value; }
        }    
        public string Address
        {      
          get { return _Address; } 
          set { _Address = value; }
        }
        public string Comments
        {
          get { return _Comments; }
          set { _Comments = value; }
        }      
    
      }
    
    2.Nure Form Behind.cs
    
     public partial class Nurse_LogIn : Form
      {
        public Nurse_LogIn()
        {
          InitializeComponent();
        }
     private void btn_Save_Click(object sender, EventArgs e)
        {
          DB_Access dbaccess = new DB_Access();
          Patient patient = new Patient();
          
    
          int intResult = 0;
    
          string rdb_Sex = string.Empty;
    
          if (rdb_Male.Checked)
          {
            rdb_Sex = "MALE";
          }
          else if (rdb_feMale.Checked)
          {
            rdb_Sex = "FEMALE";
          }
    
          patient.Id = lbl_GUID.Text;
          patient.Name = text_NAME.Text;
          patient.Age = int.Parse(text_AGE.Text);
          patient.Sex = rdb_Sex;
          patient.Problem = text_PROBLEM.Text;
          patient.Phone = Int64.Parse(text_PHONE.Text);
          patient.Address = text_ADDRESS.Text;     
    
          try
          {
            intResult = dbaccess.Insert(patient);
            if (intResult > 0)
            {
              MessageBox.Show("Saved in Database");          
              ClearTextBoxes();         
              GenerateGUID();
            }
          }
    
          catch (Exception ex)
          {
            MessageBox.Show(ex.Message.ToString());
          }
          finally
          {
            patient = null;
            dbaccess = null;
          }
        }
    
    3.DB_Access .cs (Here only Insert Method s there)
    
     public class DB_Access
      {
        string connStr = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;
        SqlConnection conn;
        SqlCommand cmd;
        SqlDataAdapter da;
        
        public DB_Access()
        {
        }
    
     public int Insert(Patient_Class patient)
        {
          conn = new SqlConnection(connStr);
          conn.Open();
          cmd = new SqlCommand("SAVING_PAT_DETAILS", conn);
          cmd.CommandType = CommandType.StoredProcedure;
          try
          {
            cmd.Parameters.AddWithValue("@ID", patient.Id);
            cmd.Parameters.AddWithValue("@NAME", patient.Name);
            cmd.Parameters.AddWithValue("@AGE", patient.Age);
            cmd.Parameters.AddWithValue("@SEX", patient.Sex);
            cmd.Parameters.AddWithValue("PROBLEM", patient.Problem);
            cmd.Parameters.AddWithValue("PHONE", patient.Phone);
            cmd.Parameters.AddWithValue("ADDRESS", patient.Address);
            cmd.Parameters.AddWithValue("COMMENTS", patient.Comments);
    
    
            return cmd.ExecuteNonQuery();
          }
    
          catch
          {
            throw;
          }
    
          finally
          {
            cmd.Dispose();
            conn.Close();
            conn.Dispose();
    
          }
        }
    My Table :PATIENT_DETAILS
    
    CREATE TABLE PATIENT_DETAILS
    (
    ID UNIQUEIDENTIFIER,
    NAME VARCHAR(30) NOT NULL,
    AGE INT, 
    SEX VARCHAR(10) NOT NULL,
    PROBLEM VARCHAR(255) NOT NULL,
    PHONE BIGINT,
    [ADDRESS] VARCHAR(30),
    COMMENTS VARCHAR(255),
    PRIMARY KEY(ID)
    )
    
    My Stored Procedure for Inserting :INSERT_PAT_DETAILS
    
    CREATE PROCEDURE INSERT_PAT_DETAILS
    (
    @ID UNIQUEIDENTIFIER,
    @NAME VARCHAR(30),
    @AGE INT,
    @SEX VARCHAR(30),
    @PROBLEM VARCHAR(100),
    @PHONE BIGINT,
    @ADDRESS VARCHAR(30),
    @COMMENTS VARCHAR(100)
    )
    AS 
    INSERT INTO [PATIENT_DETAILS] ([ID],[NAME],[AGE],[SEX],[PROBLEM],[PHONE],[ADDRESS],[COMMENTS]) 
    Values
    (@ID,@NAME,@AGE,@SEX,@PROBLEM,@PHONE,@ADDRESS,@COMMENTS)
    
    Pls Tel me Wher & How to Eradicate that Error, Input String was not in Correct Format !! Thanks a Lot !!
    :> Known is a Drop, UnKnown is an Ocean <:
    • Moved by Aspen VJ Wednesday, February 23, 2011 9:43 AM (From:Windows Forms Data Controls and Databinding)
    Monday, February 21, 2011 10:34 AM

Answers

  • Here is an example how to use TryParse method with int type. You can use same method with int64 type

    http://msdn.microsoft.com/en-us/library/f02979c7.aspx

    Basically it allows you to check if string value can be converted into the integer value first and if it is successful then you can assign it to your parameter. If not then just assign NULL value or show some message to user, depending on what you need to achieve in your case.

     


    Val Mazur (MVP)

    http://www.xporttools.net

    • Proposed as answer by Jackie-Sun Thursday, February 24, 2011 6:56 AM
    • Marked as answer by Jackie-Sun Tuesday, March 1, 2011 3:25 AM
    Wednesday, February 23, 2011 11:48 PM

All replies

  • My Requirements for Age & Phone ::

    Age Can be of 3 Nos Ex : It can b 1 to 999,

    & Phone No can b of Max 5 Nos upto 99999,

    I'm getting Error in

    patient.Age = int.Parse(text_AGE.Text); &

    patient.Phone = Int64.Parse(text_PHONE.Text);


    So only i used int & BigInt for Age & Phone No, & condition given to me is I shuld not use Varchar for age & Phone No !!


    :> Known is a Drop, UnKnown is an Ocean <:
    Monday, February 21, 2011 10:38 AM
  • Hi tamilan2233,

    ·         Base on your description,  I think it is little about winform.

    This forum is dedicated to Windows Forms technology. I'm moving this thread from Base "Windows Forms" to "ADO.net Data Provider" forum, since this issue is related to date. There are more date experts in that forum.

    Thank you for understanding and supporting.

     

    Best Regards,

    Vin Jin


    Vin Jin [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, February 23, 2011 9:42 AM
  • The reason you receive those errors is because when you try to parse empty string into number, converter has no idea what to do with it. I would recommend using TryParse method of integer to check if value could be converted into number first. I would also declare Age and Phone parameters as nullable in a case if they are optional and you do not pass anything
    Val Mazur (MVP)

    http://www.xporttools.net

    • Edited by VMazur Wednesday, February 23, 2011 11:54 PM
    Wednesday, February 23, 2011 11:27 AM
  • Thanks Mazur, Can u Please Show me in Code "How to use TryParse Programmatically" to avoid that Error, It wil b Really Helpful & Verymuch Understandable for me. Thanks in Advance !!


    :> Known is a Drop, UnKnown is an Ocean <:
    Wednesday, February 23, 2011 12:15 PM
  • Here is an example how to use TryParse method with int type. You can use same method with int64 type

    http://msdn.microsoft.com/en-us/library/f02979c7.aspx

    Basically it allows you to check if string value can be converted into the integer value first and if it is successful then you can assign it to your parameter. If not then just assign NULL value or show some message to user, depending on what you need to achieve in your case.

     


    Val Mazur (MVP)

    http://www.xporttools.net

    • Proposed as answer by Jackie-Sun Thursday, February 24, 2011 6:56 AM
    • Marked as answer by Jackie-Sun Tuesday, March 1, 2011 3:25 AM
    Wednesday, February 23, 2011 11:48 PM