none
Convert String to Int32

    Question

  • I am working on an inventory project using C# and Sql Server

    In the Insert Command i am getting an error to convert string to Int32.

    I have tried to declare the variables to Convert.ToInt32, int.Parse to no avail.

    The following is my code of the Insert Command Add Button

      

    private void btnAddInventory_Click(object sender, EventArgs e)
            {
                try
                    {
                        da.InsertCommand = new SqlCommand("INSERT INTO Inventory VALUES(@ProductID, @Product, @Manufacturer, @Unit Price, @OnHand, @Ordered)", connection);
                        da.InsertCommand.Parameters.Add("@ProductID", SqlDbType.Int).Value = txtProductID.Text;
                        da.InsertCommand.Parameters.Add("@Product", SqlDbType.VarChar).Value = txtProduct.Text;
                        da.InsertCommand.Parameters.Add("@Manufacturer", SqlDbType.VarChar).Value = txtManufacture.Text;
                        da.InsertCommand.Parameters.Add("@Unit Price", SqlDbType.Int).Value = Convert.ToInt32(txtPrice.Text);
                        da.InsertCommand.Parameters.Add("@OnHand", SqlDbType.Int).Value = Convert.ToInt32(txtOnHand.Text);
                        da.InsertCommand.Parameters.Add("@Ordered", SqlDbType.Int).Value = Convert.ToInt32(txtOrdered.Text);
                        connection.Open();
                        da.InsertCommand.ExecuteNonQuery();
                        connection.Close();
                        btnGetInventory.Enabled = true;
                    }
                    catch (System.Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                    finally
                    {
                        if (connection.State == ConnectionState.Open)
                        {
                            connection.Close();
                            txtProduct.Focus();
                        }
                    }

    I get the System.String.NumberStyles.NumberFormat Exception.

    I have tried a number of ways to resolve this to no avail.

    Can anyone please help?

    Thanks.

                      


    DesiBravo

    Thursday, May 24, 2012 3:39 PM

Answers

  • Which function call fails?  If it is Convert.ToInt32(), then verify the contents of the textboxes.  Also remember to use the proper culture to ensure proper conversion.  Some cultures use the comma as a decimal separator while others use the period.

    Jose R. MCP

    Thursday, May 24, 2012 4:32 PM
  • Make sure your text box doesn't involve any other characters than digits.

    Might also help


    Mark Answered, if it solves your question
    Rohit Arora

    Friday, May 25, 2012 5:46 AM
  • Hi Bravo,

    I think from the textbox name you are entering the price as price cannot be always integer so better have a float/decimal for it like the below and below also fix your issue this is because of your culture..

    float.Parse(txtprice.Text, CultureInfo.InvariantCulture)

    if ur sure that it will always integer use this

    int.Parse(txtprice.Text, CultureInfo.InvariantCulture)

    Hope it will help you..



    Sai Kumar K (MCP)
    Blog: Sai's Stuff.
    WebSite: SantoshTechnologies.
    MCP ID:Microsoft Transcript.

    Friday, May 25, 2012 5:56 AM

All replies

  • Which function call fails?  If it is Convert.ToInt32(), then verify the contents of the textboxes.  Also remember to use the proper culture to ensure proper conversion.  Some cultures use the comma as a decimal separator while others use the period.

    Jose R. MCP

    Thursday, May 24, 2012 4:32 PM
  • Make sure your text box doesn't involve any other characters than digits.

    Might also help


    Mark Answered, if it solves your question
    Rohit Arora

    Friday, May 25, 2012 5:46 AM
  • Hi Bravo,

    I think from the textbox name you are entering the price as price cannot be always integer so better have a float/decimal for it like the below and below also fix your issue this is because of your culture..

    float.Parse(txtprice.Text, CultureInfo.InvariantCulture)

    if ur sure that it will always integer use this

    int.Parse(txtprice.Text, CultureInfo.InvariantCulture)

    Hope it will help you..



    Sai Kumar K (MCP)
    Blog: Sai's Stuff.
    WebSite: SantoshTechnologies.
    MCP ID:Microsoft Transcript.

    Friday, May 25, 2012 5:56 AM
  • There are two possibilities which I thin of

    1. Do the numbers contain decimal point? Then, you can use below version of Parse,

    Int32.Parse(txtPrice.Text, System.Globalization.NumberStyles.AllowDecimalPoint)

    2. The textbox might contain characters other than number (like whitespace!). So, you can use TryParse in such cases.

    Int32 unitPrice;
    Int32.TryParse(txtPrice.Text, out unitPrice);

    I hope these help.

    Please mark this post as answer if it solved your problem. Happy Programming!

    Tuesday, May 29, 2012 5:43 AM
  • Your question is in my idea 100% off topic here as it is not about that value type (which would be on topic) but about the way it is handled in non strongly typed dataadapters.

    http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataset/threads

    If you are not using framework 1.x then somebody in that forum would probably tell you that your current code is ambigious (you should have seen that warning).

    In the forum I show above for sure somebody will gave better code.

     


    Success
    Cor

    Tuesday, May 29, 2012 9:12 AM