Error Handling in C# to handle records for HTML Table. RRS feed

  • Question

  • User-1549556126 posted

    I have a function in my controller file to get the list of records in the html table. The function is called by a method that returns the json output. The thing is when the function throws an error like giving blank records then the caption text of the html table is supposed to have the direct error message. I am able to fetch the message in result view but how to get it in the caption text?

    Here's the controller function with JSON method:

    //Fetching Members From PowerShell Script Output
            public List<string> PowerShellExecutorGrd(string scriptPath, string arg)
                var items = new List<string>();
                using (var shell = PowerShell.Create())
                    var results = shell.Invoke();
                    if (shell.HadErrors == false) {
                        if (results.Any()) {
                            foreach (var psObj in results)
                                items.Add(Server.HtmlEncode(psObj.ToString().Trim('{', '}')));
                    } else
                        items.Add("Error : " + shell.Streams.Error.ToString());
                return items;
            public JsonResult FillMembers(string GroupGUID)
                var SDC = PowerShellExecutorGrd(AppDomain.CurrentDomain.BaseDirectory + "Shell\\Get-ADGroupMembers.ps1", GroupGUID);
                return Json(new { message = SDC }, JsonRequestBehavior.AllowGet);

    if the script that is passed through the function gives no records then the message will be generated as shown here. I am generating divided by zero manually to test the text of the error. It could be any. When I am converting it into string I am getting it as response in my ajax call. However the response.message  that I am passing is type of error and not the exact text that I linked above. I am getting: {System.Management.Automation.PSDataCollection<System.Management.Automation.ErrorRecord>} as my text. How do I fetch the text obtained in Results view found under shell.Streams.Error.ToString() ? My .ajax view code is as follows:

                            type: "POST",
                            url: "/Group/FillMembers",
                            data: { GroupGUID: $("#ddlGroup option:selected").val() },
                            success: function (response) {
                                $.each(response.message, function (key, value) {
                                    var tr = $("<tr></tr>");
                                    var tbody = $("#tbody");
                                    var arr = value.split(";");
                                    $.each(arr, function (i, obj) {
                                        var temp = arr[i].trim().split("=")[1];
                                        td = $("<td></td>");
                                    //DropDownlist for required adjustments
                                    tr.append("<td><select id='ddlReqdAdjustment' class='form-control myddl'> \
                                                <option>Keep</option> \
                                                <option>Remove</option> \
                                                <option>Remove After</option> \
                                            </select> \
                                    //Textbox with Validation to allow only integers
                                    tr.append("<td><input type='number' class='form-control myText' disabled max='366' oninput='maxLimit(this)'\
                                                    onkeypress = 'return (event.charCode != 8 && event.charCode == 0 \
                                                                            || (event.charCode >= 48 && event.charCode <= 57))' /> \
                                        </td >");
                                    tr.append("<td><input type='radio' class='fakeRadio form-control' id='myChk' value='0'/> \
                                                    <label for='chk'/>\
                                    tr.append("<td><input type='date' class='form-control myDate'/></td > ");
                                    // Keep Count of HTML Rows

    This is my HTML Table Code where I am declaring caption:

    <div id="ResultGrid">
        <div id="tbl">
            <table id="data" class="table table-striped table-hover">
                    <tr id="thead" class="reviewsubhead">
                        <th>Member Name</th>
                        <th>Provisioning ID</th>
                        <th>Member Type</th>
                        <th hidden>GUID</th>
                        <th hidden>GroupGUID</th>
                        <th>Required Adjustment</th>
                        <th>No. of Day(s)</th>
                            <div class="form-control" onclick="clearRadio()" style="cursor:pointer;">
                                <a id="btnClear" class="arrow-link" style="width:auto; cursor:pointer;" onclick="clearRadio()">
                                    Clear : <label id="lblCountRecords"> 0 </label> / <label id="lblTotalCount"> 0 </label>
                <tbody id="tbody"></tbody>
            <p id="myCaption" style="text-align:center; color:red;" class="show-grid-footer" hidden>Sorry! Members Not Found</p>

    Also if there is a possible way to do this without adding an item it would be great.

    Wednesday, July 10, 2019 12:16 AM

All replies

  • User-1174608757 posted

    Hi vyasnikul,

    According to your description,since you have got string list , you could directly send it to front end..It  will be automatically deserialized in ajax, you could see as below:

     public JsonResult FillMembers()
                List<string> test = new List<string>();
                return Json(test, JsonRequestBehavior.AllowGet);

    front end:

        <meta name="viewport" content="width=device-width" />
        <script src="~/Scripts/jquery-3.3.1.js"></script>
                type: 'POST',  
                url: '/Hellow/FillMembers',  
                dataType: 'json',  
                data: {},  
                success: function(data)   
                    var list = data;
                    $.each(list, function (index, item) {
                error: function(ex)  
                    var r = jQuery.parseJSON(response.responseText);  
                    alert("Message: " + r.Message);  
            return false;  

    Best Regards


    Wednesday, July 10, 2019 10:15 AM
  • User-1174608757 posted

    HI vyasnikul,

    I notice that you have posted other case that you couldn't put the json data in html table,so I suggest that you could debug javacript code in chrome browser and see whether it has got correct format in ajax in front end.

    Here is the link I hope it could help you.


    Best Regards


    Wednesday, July 10, 2019 10:29 AM