locked
how to show exception message from ajax RRS feed

  • Question

  • User-1634604574 posted

    public JsonResult getColumns(String from_date, String Party_Type, String Voucher_Num, String to_date, String Account, String party, String company, String project)
    {
    List<String> returns = new List<string>();
    List<dummy> listreg = new List<dummy>();

    try
    {

    SqlCommand cmd = new SqlCommand("[dbo].[REPORT__General_Ledger]", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@From_Date", from_date);
    cmd.Parameters.AddWithValue("@To_Date", to_date);
    cmd.Parameters.AddWithValue("@Account", Account);
    cmd.Parameters.AddWithValue("@party", party);
    cmd.Parameters.AddWithValue("@company", company);
    cmd.Parameters.AddWithValue("@project", project);
    cmd.Parameters.AddWithValue("@Voucher_Num", Voucher_Num);
    cmd.Parameters.AddWithValue("@Party_Type", Party_Type);

    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    adapter.Fill(dt);
    foreach (DataColumn dr in dt.Columns)
    {
    listreg.Add(new dummy
    {
    variable = dr.ColumnName.ToString(),
    });
    }
    Dictionary<string, object> row;
    foreach (DataRow dr in dt.Rows)
    {
    row = new Dictionary<string, object>();
    foreach (DataColumn col in dt.Columns)
    {
    row.Add(col.ColumnName, dr[col].ToString());
    }
    rows.Add(row);
    }
    }

    catch (SqlException Ex)
    {
    returns.Add("Error:\n" + Ex.Message);
    return Json(returns, JsonRequestBehavior.AllowGet);


    }


    return Json(listreg, JsonRequestBehavior.AllowGet);
    }

    how can i show that highlight message from ajax

    Tuesday, April 2, 2019 9:33 PM

All replies

  • User475983607 posted

    You asked a similar question and I recommended returning an JSON object.  I also recommended a RESTful approach and provided links.

    {"message" : "Hello World!"}

    I recommended these approaches because they are easy to check for the existence.  I left it up to you to read the docs and come up with a design of your own.

    You came up with the following which I do not understand.

    returns.Add("Error:\n" + Ex.Message);
    return Json(returns, JsonRequestBehavior.AllowGet);

    You can make the approach work but the it is little clunky.  You'll need to loop over the results and look for the substring "Error:\n".  From there it is a mystery how you want to deal with the string.  Are you going to spit the string to get the message or use the entire string with the white space character.  There might be other messages with an Error:\n" substring.

    I would simply return an object that is easy to handle.  Maybe my initial object was too simple.

    {
    	"isError": true,
    	"errorMessage": "Oops! There was an error."
    }

    Tuesday, April 2, 2019 11:43 PM
  • User1520731567 posted

    Hi zhyanadil.it@gmail.com,

    returns.Add("Error:\n" + Ex.Message);
    return Json(returns, JsonRequestBehavior.AllowGet);

    how can i show that highlight message from ajax

    If you would like to show returns back to ajax,you could pay attention to success callback in ajax.

    For example:

      $.ajax({
                    url: '/ControllerName/getColumns',
                    type: 'GET',
    ... success: function (data) { alert(data[0]); } })

    More details about ajax,you could refer to:

    https://www.aspsnippets.com/Articles/ASPNet-MVC-Call-Controller-Method-from-View-using-jQuery-AJAX.aspx

    Best Regards.

    Yuki Tao

    Wednesday, April 3, 2019 8:46 AM
  • User-474980206 posted

    the problem with this approach (return error as ok status) is that it only works with caught errors. url, network, or binding errors are not caught.  so the ajax need to check ok static for errors, and also catch other errors. if you just return all errors as a 500 status, you can use common logic:

                $.ajax({
                    url: '/ControllerName/getColumns',
                    type: 'GET',
                    ...
                    success: function (data) {
      
                        alert(data);
                    },
                    fail: functions(x, s, e) {
                        alert("fail: " + s):
                    }
                })
    

    Wednesday, April 3, 2019 7:44 PM
  • User-1038772411 posted

    Hi, Zhyanadil.it@gmail.com

    Please Refer this document, you can see lots of techniques regarding this hope you can get as you want

    https://stackoverflow.com/questions/377644/jquery-ajax-error-handling-show-custom-exception-messages


    Thanks

    Thursday, April 4, 2019 5:09 AM