Click here to Skip to main content
15,894,539 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public JsonResult Refunded(string activity, string DCID, string DCN, string from, string to, string remark, string rid, DateTime dor, string sts, string pid)
     {
         string merchantCode = string.Empty;
         string status = string.Empty;
         string code = string.Empty;
         var DOR = "";
         if (!string.IsNullOrEmpty(sts) && sts == "F")
         {
             DOR = dor.ToString("yyyyMMdd");
         }
         pid = pid.TrimEnd(',');
         rid = rid.TrimEnd(',');
         if (rid == "")
         {
             rid = "0";
         } Exception ex;
         var rowsF = CDbManager.ExecuteNonQuery("APConnection",
             @"UPDATE [Refund] SET [RefundedDate] = @dor, [RefundStatus] = @sts WHERE [Id] in (SELECT items FROM dbo.Split(@rid,','))",
             CommandType.Text,
             new Dictionary<string, object>
             {
                 {"@dor", DOR},
                 {"@sts", sts},
                 {"@rid", rid}
             },
             out ex);
         if (rowsF > 0){
             var sendMail = true;
             if (!string.IsNullOrEmpty(sts) && sts == "F")
             {
                 var listPID = pid.TrimEnd(',').Split(',');
                 var listRID = rid.TrimEnd(',').Split(',');

                 for (var j = 0; j < listPID.Length; j++)
                 {
                     sendMail = sendMail && SendMail(Convert.ToInt32(listPID[j]), Convert.ToInt32(listRID[j]));
                     if (((_merchantCode == "M17648") && (_merchantCode == "M27399_S0001")))
                     {
                         ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

                         ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
                         string backendpostPayid = getRefundPayId(Convert.ToInt32(listRID[j]), Convert.ToInt32(listPID[j]));
                         string MailStatus = string.Empty;
                         try
                         {
                             string QString = "getRefundPayId=" + backendpostPayid.ToString();

                             WebRequest request = WebRequest.Create(System.Configuration.ConfigurationManager.AppSettings["refund_backendpost_url"].ToString());
                             request.Method = "POST";

                             byte[] byteArray = Encoding.UTF8.GetBytes(QString);
                             request.ContentType = "application/x-www-form-urlencoded";
                             request.ContentLength = byteArray.Length;

                             Stream dataStream = request.GetRequestStream();
                             dataStream.Write(byteArray, 0, byteArray.Length);
                             dataStream.Close();
                             WebResponse response = request.GetResponse();
                             dataStream = response.GetResponseStream();
                             StreamReader reader = new StreamReader(dataStream);
                             string BackendStatus = reader.ReadToEnd();
                             reader.Close();
                             dataStream.Close();
                             response.Close();
                             if (string.IsNullOrEmpty(BackendStatus)) {
                                 this.DicParam.Clear();
                                 this.DicParam.Add("@Detail", "MHWebservice-BackendPostRefundNotSent:PayId:" + backendpostPayid.ToString() + "");
                                 CDbManager.ExecuteSP("APConnection", "sp_epay_InsertM2UTemp", this.DicParam);
                             } else if (BackendStatus == "BackendPostRefundSent")
                             {
                                 this.DicParam.Clear();
                                 this.DicParam.Add("@Detail", "MHWebservice-BackendPostRefundSent:PayId:" + backendpostPayid.ToString() + "|Status:" + BackendStatus.ToString());
                                 CDbManager.ExecuteSP("APConnection", "sp_epay_InsertM2UTemp", this.DicParam);
                             }

                         } catch (Exception ex1){
                             this.DicParam.Clear();
                             this.DicParam.Add("@Detail", "MHWebservice-BackendPostRefundNotSent:PayId:" + backendpostPayid.ToString() + "|Status:" + ex1.ToString());
                             CDbManager.ExecuteSP("APConnection", "sp_epay_InsertM2UTemp", this.DicParam);
                         }
                     }
                 }
             } else {
                 code = "0";
                 status = ex != null ? ex.Message : "Failed to connect to database.";
             } AuditTrail(activity, DCID, DCN, from, to, remark);
             return Json(new { code, status });
         } }

//I being facing this error whereby it didnt didn't return value.

What I have tried:

<pre><pre><pre>   public JsonResult Refunded(string activity, string DCID, string DCN, string from, string to, string remark, string rid, DateTime dor, string sts, string pid)
        {
            string merchantCode = string.Empty;
            string status = string.Empty;
            string code = string.Empty;
            var DOR = "";
            if (!string.IsNullOrEmpty(sts) && sts == "F")
            {
                DOR = dor.ToString("yyyyMMdd");
            }
            pid = pid.TrimEnd(',');
            rid = rid.TrimEnd(',');
            if (rid == "")
            {
                rid = "0";
            } Exception ex;
            var rowsF = CDbManager.ExecuteNonQuery("APConnection",
                @"UPDATE [Refund] SET [RefundedDate] = @dor, [RefundStatus] = @sts WHERE [Id] in (SELECT items FROM dbo.Split(@rid,','))",
                CommandType.Text,
                new Dictionary<string, object>
                {
                    {"@dor", DOR},
                    {"@sts", sts},
                    {"@rid", rid}
                },
                out ex);
            if (rowsF > 0){
                var sendMail = true;
                if (!string.IsNullOrEmpty(sts) && sts == "F")
                {
                    var listPID = pid.TrimEnd(',').Split(',');
                    var listRID = rid.TrimEnd(',').Split(',');

                    for (var j = 0; j < listPID.Length; j++)
                    {
                        sendMail = sendMail && SendMail(Convert.ToInt32(listPID[j]), Convert.ToInt32(listRID[j]));
                        if (((_merchantCode == "M17648") && (_merchantCode == "M27399_S0001")))
                        {
                            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

                            ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
                            string backendpostPayid = getRefundPayId(Convert.ToInt32(listRID[j]), Convert.ToInt32(listPID[j]));
                            string MailStatus = string.Empty;
                            try
                            {
                                string QString = "getRefundPayId=" + backendpostPayid.ToString();

                                WebRequest request = WebRequest.Create(System.Configuration.ConfigurationManager.AppSettings["refund_backendpost_url"].ToString());
                                request.Method = "POST";

                                byte[] byteArray = Encoding.UTF8.GetBytes(QString);
                                request.ContentType = "application/x-www-form-urlencoded";
                                request.ContentLength = byteArray.Length;

                                Stream dataStream = request.GetRequestStream();
                                dataStream.Write(byteArray, 0, byteArray.Length);
                                dataStream.Close();
                                WebResponse response = request.GetResponse();
                                dataStream = response.GetResponseStream();
                                StreamReader reader = new StreamReader(dataStream);
                                string BackendStatus = reader.ReadToEnd();
                                reader.Close();
                                dataStream.Close();
                                response.Close();
                                if (string.IsNullOrEmpty(BackendStatus)) {
                                    this.DicParam.Clear();
                                    this.DicParam.Add("@Detail", "MHWebservice-BackendPostRefundNotSent:PayId:" + backendpostPayid.ToString() + "");
                                    CDbManager.ExecuteSP("APConnection", "sp_epay_InsertM2UTemp", this.DicParam);
                                } else if (BackendStatus == "BackendPostRefundSent")
                                {
                                    this.DicParam.Clear();
                                    this.DicParam.Add("@Detail", "MHWebservice-BackendPostRefundSent:PayId:" + backendpostPayid.ToString() + "|Status:" + BackendStatus.ToString());
                                    CDbManager.ExecuteSP("APConnection", "sp_epay_InsertM2UTemp", this.DicParam);
                                }
                      
                            } catch (Exception ex1){
                                this.DicParam.Clear();
                                this.DicParam.Add("@Detail", "MHWebservice-BackendPostRefundNotSent:PayId:" + backendpostPayid.ToString() + "|Status:" + ex1.ToString());
                                CDbManager.ExecuteSP("APConnection", "sp_epay_InsertM2UTemp", this.DicParam);
                            }
                        }
                    }
                } else {
                    code = "0";
                    status = ex != null ? ex.Message : "Failed to connect to database.";
                } AuditTrail(activity, DCID, DCN, from, to, remark);
                return Json(new { code, status });
            } }
Posted
Updated 1-Jun-20 4:35am
v2

Quote:
Not a code paths return a value

Rather simple
C#
public JsonResult Refunded(string activity, string DCID, string DCN, string from, string to, string remark, string rid, DateTime dor, string sts, string pid)
     {
...
         if (rowsF > 0){ // (1) if this test fail,
             var sendMail = true;
             if (!string.IsNullOrEmpty(sts) && sts == "F")
             {
...
             } else {
                 code = "0";
                 status = ex != null ? ex.Message : "Failed to connect to database.";
             } AuditTrail(activity, DCID, DCN, from, to, remark);
             return Json(new { code, status }); // (2) this return is skipped
         }
    // (3) and there is no return value here
     }

Advice: Never save lines in source code, it saves 1 key stroke at the expense code readability.
C#
} else { // never do this, it only makes your code more difficult to read.
    code = "0";
    status = ex != null ? ex.Message : "Failed to connect to database.";
} AuditTrail(activity, DCID, DCN, from, to, remark); // never do this, it only makes your code more difficult to read.

Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
Indentation style - Wikipedia[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
 
Share this answer
 
v2
Comments
[no name] 1-Jun-20 10:18am    
+5
Patrice T 1-Jun-20 10:20am    
Thank you
The problem is caused by your code being so badly formatted, you cannot see what the code is doing. Clean up the formatting and it becomes very obvious what the problem is. In the reformatted code below, look at the first "if" statement, then comments I added at the bottom of the code.

Truthfully, this code is a disaster with many problems. The code in this method has to be broken out into multiple methods, each doing a much smaller "thing". Your existing method code is doing way too much, like doing database work, posting to a web method, among other things, to be understood easily. Simplify what this method does and break out pieces of code into other methods. A method should do ONE THING and ONE THING ONLY. If that means a method that has to call other methods to have them do something, like post a message to a URL, then that's what you have to do.

You also have "magic numbers" that have no meaning defined for them, like "M17648". Replace these with constants with meaningful names.

This is your entire "Refunded" method:
C#
public JsonResult Refunded(string activity, string DCID, string DCN, string from, string to, string remark, string rid, DateTime dor, string sts, string pid)
{
    string merchantCode = string.Empty;
    string status = string.Empty;
    string code = string.Empty;
    var DOR = "";

    if (!string.IsNullOrEmpty(sts) && sts == "F")
    {
        DOR = dor.ToString("yyyyMMdd");
    }

    pid = pid.TrimEnd(',');
    rid = rid.TrimEnd(',');

    if (rid == "")
    {
        rid = "0";
    }

    Exception ex;

    var rowsF = CDbManager.ExecuteNonQuery("APConnection",
         @"UPDATE [Refund] SET [RefundedDate] = @dor, [RefundStatus] = @sts WHERE [Id] in (SELECT items FROM dbo.Split(@rid,','))",
         CommandType.Text,
         new Dictionary<string, object>
         {
         {"@dor", DOR},
         {"@sts", sts},
         {"@rid", rid}
         },
         out ex
    );

    if (rowsF > 0)
    {
        var sendMail = true;

        if (!string.IsNullOrEmpty(sts) && sts == "F")
        {
            var listPID = pid.TrimEnd(',').Split(',');
            var listRID = rid.TrimEnd(',').Split(',');

            for (var j = 0; j < listPID.Length; j++)
            {
                sendMail = sendMail && SendMail(Convert.ToInt32(listPID[j]), Convert.ToInt32(listRID[j]));

                if (((_merchantCode == "M17648") && (_merchantCode == "M27399_S0001")))
                {
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

                    ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

                    string backendpostPayid = getRefundPayId(Convert.ToInt32(listRID[j]), Convert.ToInt32(listPID[j]));

                    string MailStatus = string.Empty;

                    try
                    {
                        string QString = "getRefundPayId=" + backendpostPayid.ToString();

                        WebRequest request = WebRequest.Create(System.Configuration.ConfigurationManager.AppSettings["refund_backendpost_url"].ToString());
                        request.Method = "POST";

                        byte[] byteArray = Encoding.UTF8.GetBytes(QString);
                        request.ContentType = "application/x-www-form-urlencoded";
                        request.ContentLength = byteArray.Length;

                        Stream dataStream = request.GetRequestStream();
                        dataStream.Write(byteArray, 0, byteArray.Length);
                        dataStream.Close();
                        WebResponse response = request.GetResponse();
                        dataStream = response.GetResponseStream();
                        StreamReader reader = new StreamReader(dataStream);
                        string BackendStatus = reader.ReadToEnd();
                        reader.Close();
                        dataStream.Close();
                        response.Close();
                        if (string.IsNullOrEmpty(BackendStatus))
                        {
                            this.DicParam.Clear();
                            this.DicParam.Add("@Detail", "MHWebservice-BackendPostRefundNotSent:PayId:" + backendpostPayid.ToString() + "");
                            CDbManager.ExecuteSP("APConnection", "sp_epay_InsertM2UTemp", this.DicParam);
                        }
                        else if (BackendStatus == "BackendPostRefundSent")
                        {
                            this.DicParam.Clear();
                            this.DicParam.Add("@Detail", "MHWebservice-BackendPostRefundSent:PayId:" + backendpostPayid.ToString() + "|Status:" + BackendStatus.ToString());
                            CDbManager.ExecuteSP("APConnection", "sp_epay_InsertM2UTemp", this.DicParam);
                        }

                    }
                    catch (Exception ex1)
                    {
                        this.DicParam.Clear();
                        this.DicParam.Add("@Detail", "MHWebservice-BackendPostRefundNotSent:PayId:" + backendpostPayid.ToString() + "|Status:" + ex1.ToString());
                        CDbManager.ExecuteSP("APConnection", "sp_epay_InsertM2UTemp", this.DicParam);
                    }
                }
            }
        }
        else
        {
            code = "0";
            status = ex != null ? ex.Message : "Failed to connect to database.";
        }
        AuditTrail(activity, DCID, DCN, from, to, remark);
        return Json(new { code, status });
    }

    // If the "(rowsF > 0)" if statement fails, you
    // end up here with no return statement to return
    // anything to the caller.
}
 
Share this answer
 
v2
If you mean the error is "Not all code paths return a value" then you are probably missing a return statement.
See: c# returning error "not all code paths return a value" - Stack Overflow[^]
 
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