Home > Back-end >  Input string was not in a correct format. Textbox value
Input string was not in a correct format. Textbox value

Time:12-29

I have this method.

public static long? GetPayments(int PaymentId,string Reference)
        
{

          
            try
            {
                using (SqlConnection con = new SqlConnection(connstrig))
                {
                    con.Open();
                    DynamicParameters param = new DynamicParameters();
                    param.Add("@IDPayment", PaymentId);
                    param.Add("@Ref", Reference);
                    var result = con.Query<long?>("spc_PaymentStatusForCombo", param, commandType: CommandType.StoredProcedure).FirstOrDefault();
                    return result;
                }
            }
            catch (Exception exc)
            {
                ApplicationLog.WriteLog("spc_PaymentStatusForCombo", exc.Message, "Error");
                return null;
            }
        }

And when i call it this error is shown up:Input string was not in a correct format.

var result = 
GetPaymentsByPayId(Convert.ToInt32(txt_IdPayment.Text.Trim()), (txt_IdPayment.Text.Length > 0) ? (txt_OrderID.Text.Trim()) : string.Empty);

Can you help me please? Thank you

CodePudding user response:

The error is clear, the string input is not correct.

You can have this error for example if you try Convert.ToInt32("");

According to your method's structure, you should send an int value as the first parameter and the Convert.ToInt32(txt_IdPayment.Text.Trim()) must cause this exception.

Check what it returns txt_IdPayment.Text.Trim().

CodePudding user response:

var result = GetPaymentsByPayIdorMidasreference(Convert.ToInt32((txt_IdPayment.Text.Length > 0) ? txt_IdPayment.Text.Trim() : "0"), (txt_IdPayment.Text.Length > 0) ? (txt_OrderID.Text.Trim()) : string.Empty);
  •  Tags:  
  • c#
  • Related