none
TextBox.Text to Int Conversion

    Question

  • How do I add TextBox.Text info to an INT column of a new row to a table?  My problems seems to be in the line:

    newRow["empid"] = int.Parse(txtEmpID.Text);

    The error message is: Input string was not in a correct format.

    The "empid" column is declared in SQL simply as INT NOT NULL and then added as CONSTRAINT PK_Employees PRIMARY KEY(empid).

    Any help will be greatly appreciated.

    Bill

    • Moved by CoolDadTxMVP Wednesday, February 17, 2010 3:07 PM Winforms related (From:Visual C# General)
    Wednesday, February 17, 2010 1:15 PM

Answers

  • It finally clicked.  The code was ok.
    Problem was the fact that I included it in the Form_Load event handler where all the text boxes where still empty.
    I moved the code to a Button_Click event handler and it worked perfectly.

    Thanks for the help.
    • Marked as answer by byschaupp Wednesday, February 17, 2010 4:53 PM
    Wednesday, February 17, 2010 4:51 PM

All replies

  • What is the value of txtEmpID.Text?

    The string you are passing to Int32.Parse can only contain the digits 0 to 9, an option +/- sign before the series of digits, and whitespace at the beginning or end of the string.

    Reference: http://msdn.microsoft.com/en-us/library/b3h1hf19.aspx
    Wednesday, February 17, 2010 1:33 PM
  • Does your textbox contain an integer?
    Maybe try parsing it to an int before you place it in the datarow.

    Just a question:
    Do you really want to be able to set the primary key with a textbox?
    Wednesday, February 17, 2010 1:38 PM
  • hi,

    make sure of txtEmpID.Text  is a int value and not an empty string value.........u cannot pass empty string r any char .........it should only be an integer value.
     

    Nagarjuna Dilip
    Wednesday, February 17, 2010 1:39 PM
  • At this point the textbos is empty.  Here's the code I am using.  Should I add another line somehow?

     

    string connStringP = @"

    server = .\sqlexpress;

    integrated security = true;

    database = Performance"

     

    ;

     

    string sqlSelectEmps = "SELECT * FROM dbo.Employees";

     

    string sqlInsertEmps = @"

    INSERT INTO Employees

    (

    empid,

    firstname,

    lastname

    )

    values

    (

    @empid,

    @firstname,

    @lastname

    )

    "

     

    ;

     

    private void NewEmployeeInsert_Load(object sender, EventArgs e)

    {

     

    button1.Enabled =

    false;

     

    SqlConnection conn = new SqlConnection(connStringP);

     

    SqlDataAdapter da = new SqlDataAdapter();

    da.SelectCommand =

    new SqlCommand(sqlSelectEmps, conn);

     

    DataSet ds = new DataSet();

    da.Fill(ds,

    "Employees");

     

    DataTable dt = ds.Tables["Employees"];

     

    DataRow newRow = dt.NewRow();

    newRow[

    "empid"] = int.Parse(txtEmpID.Text);

    newRow[

    "firstname"] = txtFirstName.Text;

    newRow[

    "lastname"] = txtLastName.Text;

    dt.Rows.Add(newRow);

     

    SqlCommand cmd = new SqlCommand(sqlInsertEmps, conn);

    cmd.Parameters.Add(

    "@empid", SqlDbType.Int, 5, "empid");

    cmd.Parameters.Add(

    "@firstname", SqlDbType.NVarChar, 25, "firstname");

    cmd.Parameters.Add(

    "@lastname", SqlDbType.NVarChar, 25, "lastname");

    da.InsertCommand = cmd;

    da.Update(ds,

    "Employees");

    }



    Wednesday, February 17, 2010 1:47 PM
  • You can't convert an empty string to an integer so if you textbox might be empty do the following check:

    if(!String.IsNullOrEmpty(txtEmpID.Text))
    {
       newRow["empid"] = int.Parse(txtEmpID.Text);
    }

    And as a side note, typically you let the database generate your PK number by using an Indentity column.  For more information on Identity columns see: http://msdn.microsoft.com/en-us/library/aa933196%28SQL.80%29.aspx
    Wednesday, February 17, 2010 1:58 PM
  • hi,

    In ur DB is empid   auto increment.........? if it is then dont pass the empid value from the sqlInsertEmps. If it is not an autoincrement then please check the input of ur empid from UI (it should be an int value)and dont pass the duplicate value from UI which alredy exist in ur DB.

    ex:

    empID    firstname     lastname
      

    1000       Sam           Alex
    1001       Jack           D
    1000       Steve         M  (dont try to input 1000 r 1001 from UI. it throws an exception)
    Nagarjuna Dilip
    Wednesday, February 17, 2010 2:00 PM
  • You can try int.TryParse

    It converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the conversion succeeded.
    (as found on MSDN)


    Dimitri C.
    Wednesday, February 17, 2010 2:05 PM
  • New error message:  'Performance.dbo.Employees'; column does not allow nulls.  INSERT fails.
    Wednesday, February 17, 2010 2:06 PM
  • That is because you must supply a value for the EmpID column because it is declared as "NOT NULL" (and has to be if it is the PK).  Either change EmpID to an Identity Column, or change your user interface to require a value for txtEmpID.
    Wednesday, February 17, 2010 2:09 PM
  • int.TryParse did not work.  C# will not accept it.

    empid does not autoincrement.  If I exclude it totally, I get the same error message:
      'Performance.dbo.Employees': columns does not allow nulls. INSERT fails.
    Wednesday, February 17, 2010 2:14 PM
  • hi,

    tell this first........all ur code is in form load event..........? and make sure pass int value in txtEmpId........!
    Nagarjuna Dilip
    Wednesday, February 17, 2010 2:16 PM
  • Are you other columns declared as "NOT NULL"? If so then you must supply non-Null values for them as well.
    Wednesday, February 17, 2010 2:16 PM
  • Drop the textbox.  Use a NumericUpDown instead, and use it's Value property.  NumericUpDown will ensure there's a value in the box, and there's no conversion necessary.
    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
    Wednesday, February 17, 2010 2:17 PM
  • that means the value you try to insert is NULL and your column definition doesn't allow to insert NULL-values.
    You can change the column definition to allow NULL-values.
    Dimitri C.
    Wednesday, February 17, 2010 2:19 PM
  • I tried most of the fixes, and this worked the best.  I changed the 'empid' column to IDENTITY in SQL and it seems to run okay.

    I'll also try the numeric up-down idea.  Didn't think of that.
    • Marked as answer by byschaupp Wednesday, February 17, 2010 4:51 PM
    • Unmarked as answer by byschaupp Wednesday, February 17, 2010 4:53 PM
    Wednesday, February 17, 2010 2:43 PM
  • It finally clicked.  The code was ok.
    Problem was the fact that I included it in the Form_Load event handler where all the text boxes where still empty.
    I moved the code to a Button_Click event handler and it worked perfectly.

    Thanks for the help.
    • Marked as answer by byschaupp Wednesday, February 17, 2010 4:53 PM
    Wednesday, February 17, 2010 4:51 PM