1

i am getting this error don`t know why...

here is Code (values in comments)..

protected void FormView1_ItemCommand(object sender, System.Web.UI.WebControls.FormViewCommandEventArgs e)
    {

        if (e.CommandName == "Update")
        {

            dsServicesTableAdapters.Select_NegotiateTableAdapter obj = new dsServicesTableAdapters.Select_NegotiateTableAdapter();

      //here i am getting the input string is not in a correct format error
            obj.Insert_Negotiate_New(Int32.Parse(stockid), //1
                Decimal.Parse(txtfobcustomer.Text),  //10
                Decimal.Parse(txtfrieghtcustomer.Text), //10
                Decimal.Parse(txtvanningcustomer.Text), //10
                Decimal.Parse(txtinspectioncustomer.Text), //10
                Decimal.Parse(txttotal_costcustomer.Text), //10
                Int32.Parse(ddlCurrency.SelectedValue), //  0.0105   
                Int32.Parse(ddlcountry.SelectedValue), //5 
                Int32.Parse(customerid),//1
                txtcustomername.Text, // Anglo  
                txtcustomerEmail.Text, // [email protected]  
                txtcustomer_phone.Text, // 03313752499  
                txtComments.Text, //test
                Int32.Parse(ddlshipmenttye.SelectedValue)); //2

        }
    }

and here is my stored procedure

ALTER PROCEDURE [dbo].[Insert_Negotiate_New]
(

    @stock_ID int,
    @client_FOB_Price money,
    @Client_FrieghtPrice money,
    @Client_Vanning_Price money,
    @Client_Inspection_Price money,
    @Client_Total_Cost money,
    @Currency_ID int,
    @Country_ID int,
    @Customer_ID int,
    @Client_Name varchar(50),
    @Client_Email varchar(50),
    @Client_Phone varchar(50),
    @Client_Comments varchar(3000),
    @ShipmentType int
)
AS
    SET NOCOUNT OFF;


declare @negotiation_ID int
set dateformat dmy


Begin---insert negotiation

SELECT @negotiation_ID=ISNULL(MAX(negtiation_ID),0)+1 FROM negotiate


INSERT INTO negotiate (negtiation_ID,Time_Stamp, stock_ID,client_FOB_Price, Client_FrieghtPrice, Client_Vanning_Price,
 Client_Inspection_Price, Client_Total_Cost, Currency_ID,Country_ID,
  Customer_ID, Client_Name, Client_Email, Client_Phone, Client_Comments, ShipmentType) 
  VALUES (@negotiation_ID,GETDATE(),@stock_ID, @client_FOB_Price, @Client_FrieghtPrice, @Client_Vanning_Price,
   @Client_Inspection_Price, @Client_Total_Cost, @Currency_ID, @Country_ID,@Customer_ID, 
   @Client_Name, @Client_Email, @Client_Phone, @Client_Comments, @ShipmentType);

End

Table

here is my Negotiate talbe

Any Idea why i am getting this error although i am guessing that there are correct values

5
  • How sure are you that ddlshipmenttye.SelectedValue is 2? Could it be "2 "? (ie. with a space) And that goes for all the other parse calls as well. How sure are you that all of those will work? Commented Apr 30, 2014 at 7:34
  • Also, what is this? Int32.Parse(ddlCurrency.SelectedValue), // 0.0105 Is 0.0105 the value in that field? Then why are you trying to parse it as an int? You should create validation to handle this sort of thing if that is the case, but my guess is that you wanted to use decimal.Parse instead. Commented Apr 30, 2014 at 7:35
  • hi @LasseV.Karlsen i am sure there is no space i have checked it in debugger Commented Apr 30, 2014 at 7:38
  • yes LasseV.karlsen it was ddlCurrency.SelectedValue thanks Commented Apr 30, 2014 at 7:42
  • Btw, here you see a good reason to use variables with meaningful names instead of passing the parameters directly from the control. That makes it also easier to debug. Commented Apr 30, 2014 at 8:54

2 Answers 2

2

Int32.Parse(...) or Decimal.Parse(...) throws the exception. The first and only i see is:

Int32.Parse(ddlCurrency.SelectedValue), //  0.0105   

This is clearly not an int so use

Decimal.Parse(ddlCurrency.SelectedValue), //  0.0105   

However, in your stored-procedure this seems to be also an int because it's a currency-ID. Then you have to fix this bug. The DropDownList.SelectedValue should not be 0.0105 when it's an int-ID.

Apart from that you would also get the exception if your current culture is not using the dot . as decimal separator. Then you have to enforce this separator by using InvariantCulture:

Decimal.Parse(ddlCurrency.SelectedValue, CultureInfo.InvariantCulture)  
Sign up to request clarification or add additional context in comments.

Comments

0

you are using Int32.Parse for parsing value like 0.0105. As you know this is not an integer value so it will throw an exception. One more thing as I can see here you are using int for @Currency_ID int. It should be also money or decimal.

1 Comment

Currency_ID sounds as if it's a foreign key to a currency-table so money or decimal would be inappropriate. The prices are already money but other columns.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.