0

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

5
  • are you sure that txt_IdPayment.Text is a valid Int32 ? Commented Dec 28, 2021 at 12:54
  • the problem is with txt_OrderID.Text.Trim() Commented Dec 28, 2021 at 12:55
  • is this a Win Forms app?, do you call this function in TextChanged Event ? Commented Dec 28, 2021 at 13:05
  • yes it is a win form. No i call it in a method, which then i call in click of an button Commented Dec 28, 2021 at 13:17
  • the only thing I can think about is that txt_IdPayment.Text, try to debug your code and check the value of txt_IdPayment.Text Commented Dec 28, 2021 at 13:32

2 Answers 2

1

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().

Sign up to request clarification or add additional context in comments.

5 Comments

txt_IdPayment.Text.Trim() without Convert.ToInt32 shows error
Weird! Are you sure?
yeah i tried now again. If i delete Convert.ToInt32 it shows the error can not convert from string to int
No, this is another error. If you send a string to your method, you will have this exception. Can you try to get it in a parameter before calling your method ? For example var id = txt_IdPayment.Text.Trim();
thank you. When i did this var id, it worked.
0
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);

3 Comments

This is not a good solution Amit. In case of something nonconvertible to int you will get the same error. For example, if the input text is ABC, you will have the same error and if the length is 0, you will get a false result. You should check what you send to your method before.
If we restrict our input type to accept only numeric value then it will work.
Yes but, you can not restrict the textbox without a specific code. And we don't know the context and where the ID comes from into the textbox.

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.