Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
when i try to save the invoice to sql server i get that error "Input string was not in a correct format."
cause the value float
i need to solve it
please help

What I have tried:

C#
for (int i = 0; i < dgv_products_view.Rows.Count-1; i++)
            {
                order.ADD_ORDER_DETAILS(Convert.ToInt32(dgv_products_view.Rows[i].Cells[0].Value),
                    Convert.ToInt32(txt_order_id.Text),
                    Convert.ToInt32(dgv_products_view.Rows[i].Cells[3].Value),
                    dgv_products_view.Rows[i].Cells[2].Value.ToString(),
                    float.Parse(dgv_products_view.Rows[i].Cells[5].Value.ToString()),
                    dgv_products_view.Rows[i].Cells[4].Value.ToString(),
                    dgv_products_view.Rows[i].Cells[6].Value.ToString());
            }
Posted
Updated 24-Oct-19 2:27am
v2
Comments
srko 24-Oct-19 6:28am    
dgv_products_view.Rows[i].Cells[5].Value what value does it contain. What is the exact type? can you provide me an example?

basically, one of your strings isn't an int string.

To work out which one, try separating out your code:
C#
for (int i = 0; i < dgv_products_view.Rows.Count-1; i++)
 {
   var cell0 = Convert.ToInt32(dgv_products_view.Rows[i].Cells[0].Value);
   var orderId = Convert.ToInt32(txt_order_id.Text);
   var cell3 = Convert.ToInt32(dgv_products_view.Rows[i].Cells[3].Value);
   var cell5  = float.Parse(dgv_products_view.Rows[i].Cells[5].Value.ToString());

   order.ADD_ORDER_DETAILS(cell0,orderId,cell3,
         dgv_products_view.Rows[i].Cells[2].Value.ToString(),
         cell5,
         dgv_products_view.Rows[i].Cells[4].Value.ToString(),
         dgv_products_view.Rows[i].Cells[6].Value.ToString());
}


hope that helps ^_^
 
Share this answer
 
C#
order.ADD_ORDER_DETAILS(Convert.ToInt32(dgv_products_view.Rows[i].Cells[0].Value),
Convert.ToInt32(txt_order_id.Text),
Convert.ToInt32(dgv_products_view.Rows[i].Cells[3].Value),

One of those text fields is not a valid number. But putting the conversions inline like that is a very bad idea since it is difficult to find out which. Added to that you should never use Convert.ToInt, as it will potentially crash your application if it is given bad data (as you have seen). Use Int.TryParse so you can capture any bad data and ask the user to fix it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900