Answered by:
Amount display on lable showing - Input string was not in a correct format

Question
-
User-2074858223 posted
Am trying to update a record that has amount paid by customers but on submittion i get this error
code
int inserted = 0; float amountavailable = Convert.ToInt32(lblAmountUpdate.Text.Trim()); // int amountavailable = lblAmountUpdate); string paymentstatus = ddlpaymentstatus.SelectedItem.Text.Trim(); int amountpaid = Convert.ToInt32(txtamount.Text.Trim()); using (SqlConnection con = new SqlConnection()) { con.ConnectionString = str; using (SqlCommand cmd = new SqlCommand()) { cmd.CommandType = CommandType.Text; cmd.Connection = con; //Change the query like below //Make sure the column name matches the with the name provided in code as per your table // cmd.CommandText = "INSERT INTO Payments (UserName,SellId,PaymentStatus) VALUES(@UserName,@SellId,@PaymentStatus)"; // cmd.Parameters.AddWithValue("@UserName", Session["userName"]); // cmd.Parameters.AddWithValue("@CustomerName", ddlcustomername.SelectedItem.Text.Trim()); // cmd.Parameters.AddWithValue("@SellId", ddltableid.SelectedItem.Text.Trim()); // cmd.Parameters.AddWithValue("@PaymentStatus", paymentstatus); // cmd.Parameters.AddWithValue("@AmountPaid", txtamount.Text.Trim()); // cmd.Parameters.AddWithValue("@QuantitySupplied", txtQuantity.Text.Trim()); // cmd.Parameters.AddWithValue("@SuppliedStatus", ddlsupplystatus.SelectedItem.Text.Trim()); // cmd.Parameters.AddWithValue("@TotalAmount", grandtotal); // cmd.Parameters.AddWithValue("@OrderDate", DateTime.Today); // con.Open(); // inserted = cmd.ExecuteNonQuery(); // con.Close(); } } // else { // ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('No of Quantity entered is not available in Stock')", true); } if (inserted > 0) { float updatepayment = amountavailable + amountpaid; using (SqlConnection con = new SqlConnection()) { con.ConnectionString = str; using (SqlCommand cmd = new SqlCommand()) { cmd.CommandType = CommandType.Text; cmd.Connection = con; cmd.CommandText = "UPDATE Sells SET PaymentStatus = @PaymentStatus,AmountPaid=@AmountPaid WHERE SellId = @SellId"; cmd.Parameters.AddWithValue("@PaymentStatus", ddlpaymentstatus.SelectedItem.Text); cmd.Parameters.AddWithValue("@AmountPaid", updatepayment); cmd.Parameters.AddWithValue("@SellId", ddltableid.SelectedItem.Text); con.Open(); inserted = cmd.ExecuteNonQuery(); con.Close(); } } } BindProductsSells(); } } }
Server Error in '/' Application. Input string was not in a correct format. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.FormatException: Input string was not in a correct format. Source Error: Line 119: Line 120: int inserted = 0; Line 121: float amountavailable = Convert.ToInt32(lblAmountUpdate.Text.Trim()); Line 122: // int amountavailable = lblAmountUpdate); Line 123: string paymentstatus = ddlpaymentstatus.SelectedItem.Text.Trim();
Wednesday, November 22, 2017 2:25 PM
Answers
-
User2103319870 posted
float amountavailable = Convert.ToInt32(lblAmountUpdate.Text.Trim());
I guess you are trying to convert a value with decimals like 12.67, if so then convert.ToInt32 will throw exception. Try using ToSingle Method instead
float amountavailable = Convert.ToSingle(lblAmountUpdate.Text.Trim());
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, November 22, 2017 2:40 PM -
User2103319870 posted
with your solution still the record is not updatingint inserted = 0;
if (inserted > 0) {}In your code you have set the variable "inserted" value to 0 and have a check condition on update statement to see if value in variable 'inserted" is greater than zero, hence the code wont get executed.
You need to change the logic to make sure the inserted variable is having value greater than zero so that update code block will get executed
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, November 22, 2017 5:49 PM
All replies
-
User2103319870 posted
float amountavailable = Convert.ToInt32(lblAmountUpdate.Text.Trim());
I guess you are trying to convert a value with decimals like 12.67, if so then convert.ToInt32 will throw exception. Try using ToSingle Method instead
float amountavailable = Convert.ToSingle(lblAmountUpdate.Text.Trim());
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, November 22, 2017 2:40 PM -
User-2074858223 posted
with your solution still the record is not updating
int inserted = 0; float amountavailable = Convert.ToSingle(lblAmountUpdate.Text.Trim()); // int amountavailable = lblAmountUpdate); string paymentstatus = ddlpaymentstatus.SelectedItem.Text.Trim(); int amountpaid = Convert.ToInt32(txtamount.Text.Trim()); using (SqlConnection con = new SqlConnection()) { con.ConnectionString = str; using (SqlCommand cmd = new SqlCommand()) { cmd.CommandType = CommandType.Text; cmd.Connection = con; //Change the query like below //Make sure the column name matches the with the name provided in code as per your table // cmd.CommandText = "INSERT INTO Payments (UserName,SellId,PaymentStatus) VALUES(@UserName,@SellId,@PaymentStatus)"; // cmd.Parameters.AddWithValue("@UserName", Session["userName"]); // cmd.Parameters.AddWithValue("@CustomerName", ddlcustomername.SelectedItem.Text.Trim()); // cmd.Parameters.AddWithValue("@SellId", ddltableid.SelectedItem.Text.Trim()); // cmd.Parameters.AddWithValue("@PaymentStatus", paymentstatus); // cmd.Parameters.AddWithValue("@AmountPaid", txtamount.Text.Trim()); // cmd.Parameters.AddWithValue("@QuantitySupplied", txtQuantity.Text.Trim()); // cmd.Parameters.AddWithValue("@SuppliedStatus", ddlsupplystatus.SelectedItem.Text.Trim()); // cmd.Parameters.AddWithValue("@TotalAmount", grandtotal); // cmd.Parameters.AddWithValue("@OrderDate", DateTime.Today); // con.Open(); // inserted = cmd.ExecuteNonQuery(); // con.Close(); } } // else { // ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('No of Quantity entered is not available in Stock')", true); } if (inserted > 0) { float updatepayment = amountavailable + amountpaid; using (SqlConnection con = new SqlConnection()) { con.ConnectionString = str; using (SqlCommand cmd = new SqlCommand()) { cmd.CommandType = CommandType.Text; cmd.Connection = con; cmd.CommandText = "UPDATE Sells SET PaymentStatus = @PaymentStatus,AmountPaid=@AmountPaid WHERE SellId = @SellId"; cmd.Parameters.AddWithValue("@PaymentStatus", ddlpaymentstatus.SelectedItem.Text); cmd.Parameters.AddWithValue("@AmountPaid", updatepayment); cmd.Parameters.AddWithValue("@SellId", ddltableid.SelectedItem.Text); con.Open(); inserted = cmd.ExecuteNonQuery(); con.Close(); } } } BindProductsSells(); } } }
Wednesday, November 22, 2017 5:44 PM -
User2103319870 posted
with your solution still the record is not updatingint inserted = 0;
if (inserted > 0) {}In your code you have set the variable "inserted" value to 0 and have a check condition on update statement to see if value in variable 'inserted" is greater than zero, hence the code wont get executed.
You need to change the logic to make sure the inserted variable is having value greater than zero so that update code block will get executed
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, November 22, 2017 5:49 PM