1

Ok. I have several JQuery Ajax posts, which are calling individual WebMethods. I've come across an issue, and i now need to combine the webmethods into one, but then still be able to split out the return values according to the list. So for example, I need something that returns multiple lists, so that my Ajax call, can then render the respective Jquery Datatables like: table 1 = drList[0], table 2 =drList[1] etc.

Webmethod code is below:

[WebMethod]
[ScriptMethod]
public static List<GlobalClasses.ValueDateSummary>  GetValueDateSummary()
{

    string TSQL = "SELECT * FROM vw_view1 WHERE (SenderBIC ='" +    HttpContext.Current.User.Identity.Name + "') ORDER BY ValueDate DESC";
    DataTable dtValueDateSummary = null;
    GlobalClasses.ValueDateSummary objSummary;

    SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["MIReporting"].ConnectionString);
    using (conn)
    {
        conn.Open();
        using (SqlDataAdapter sqlAdapter = new SqlDataAdapter(TSQL, conn))
        {
            dtValueDateSummary = new DataTable();
            sqlAdapter.Fill(dtValueDateSummary);
        }
    }
    List<GlobalClasses.ValueDateSummary> drList = new List<GlobalClasses.ValueDateSummary>();
    foreach (DataRow row in dtValueDateSummary.Rows)
    {
        objSummary = new GlobalClasses.ValueDateSummary();
        objSummary.fld1= row["1"].ToString();
        objSummary.fld2 = row["2"].ToString();
        objSummary.fld3 = row["3"].ToString();
        objSummary.fld5 = row["4"].ToString();

        drList.Add(objSummary);
    }
    return drList;
}

the only difference between this and the other webmethod call is the view being used.

the Ajax call is :

           $.ajax({
               type: 'POST',
               url: 'Default.aspx/GetValueDateSummary',
               data: '{}',
               contentType: 'application/json;charset=utf-8',
               dataType: 'json',
               success: function (response) {

                 renderMsgSummary(response.d);
               },
               error: function (errMsg) {
                   $('#errorMessage').text(errMsg);
               }
           })

the renderMsgSummary is the Jquery datatable, so this need to be as follows:

renderMsgSummary(response.d[0]);
renderOtherTable(response.d[1]);

OK - almost there, trying to merge the client side script in now.

     <script type="text/javascript">
       $(document).ready(function () {
           function renderMsgVal(result) {
               var dtMsgValData = [];
               $.each(result, function () {
                   dtMsgValData.push([
                       this.SenderBIC,
                       this.ValueDate,
                       this.MessageType,
                       this.Reference
                   ]);
               });

               function renderMsgSummary(result) {
                   var dtMsgSumData = [];
                   $.each(result, function () {
                       dtMsgSumData.push([
                           this.SenderBIC,
                           this.MessageDate,
                           this.MessageType,
                           this.Count
                       ]);
                   });

                   $('#tblValueDateSummary').dataTable({
                       "aaData": dtMsgValData,

                       "aLengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100]]
                       'asStripClasses': null,
                       "iDisplayLength": 10,
                       //"aaSorting": [[0, "asc"]],
                       "bJQueryUI": true,
                       "bFilter": true,
                       "bAutoWidth": false,
                       "bProcessing": true,
                       // "sDom": 'RC<"clear">lfrtip',
                       "sDom": 'RC<"H"lfr>t<"F"ip>',

                       //Scrolling .......
                       "sScrollY": "250px",
                       "sScrollX": "100%",
                       "sScrollXInner": "100%",
                       "bScrollCollapse": true,

                       //Dynamic Language .......
                       "oLanguage": {
                           //"sZeroRecords": "There are no messages that match your,
                           "sLengthMenu": "Display _MENU_ records per,
                           "sInfo": "Displaying _START_ to _END_ of _TOTAL_ records",
                           "sInfoEmpty": "Displaying _START_ to _END_ of _TOTAL_m
                           "sInfoFiltered": "(filtered from _MAX_ total records)",
                           "sEmptyTable": 'No Rows to display.....!',
                           "sSearch": "Search all columns:",
                           "sLoadingRecords": "Please wait - loading..."
                       },
                       "oSearch": {
                           "sSearch": "",
                           "bRegex": false,
                           "bSmart": true
                       }
                   });

                   $('#tblMessageDateSummary').dataTable({
                       "aaData": dtMsgSumData,
                       "aLengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100]]
                       "asStripClasses": null,
                       "iDisplayLength": 10,
                       "aaSorting": [[0, "asc"]],
                       "bJQueryUI": true,
                       "bFilter": true,
                       "bAutoWidth": true,
                       "bProcessing": true,
                       "sDom": 'RC<"clear">lfrtip',
                       //"sDom": '<"F"ip>l',


                       //Scrolling .......
                       "sScrollY": "250px",
                       "sScrollX": "100%",
                       "sScrollXInner": "100%",
                       "bScrollCollapse": true,

                       //Dynamic Language .......
                       "oLanguage": {
                           // "sZeroRecords": "There are no messages that match your,
                           "sLengthMenu": "Display _MENU_ records per,
                           "sInfo": "Displaying _START_ to _END_ of _TOTAL_ records",
                           "sInfoEmpty": "Showing 0 to 0 of 0 records",
                           "sInfoFiltered": "(filtered from _MAX_ total records)",
                           "sEmptyTable": 'No Rows to display.....!',
                           "sSearch": "Search all columns:"
                       },
                       "oSearch": {
                           "sSearch": "",
                           "bRegex": false,
                           "bSmart": true
                       }
                   });
               }


               $.ajax({
                   type: 'POST',
                   url: 'Default.aspx/GetMessageDetails',
                   data: '{}',
                   contentType: 'application/json;charset=utf-8',
                   dataType: 'json',
                   success: function (response) {
                       //console.log(response);
                       //alert(response.d);
                       renderMsgVal(response.d[0]);
                       renderMsgSummary(response.d[1]);
                   },
                   error: function (errMsg) {
                       $('#errorMessage').text(errMsg);
                   }
               });


       </script>

not sure if this is possible?

2 Answers 2

1

If I understand correctly, you can return a List of Lists.

public static List<List<GlobalClasses.ValueDateSummary>> GetValueDateSummary()    
{
    var allLists = new List<List<GlobalClasses.ValueDateSummary>>();

    foreach (DataRow row in dtValueDateSummary.Rows)
    {
        objSummary = new GlobalClasses.ValueDateSummary();
        objSummary.fld1= row["1"].ToString();
        objSummary.fld2 = row["2"].ToString();
        objSummary.fld3 = row["3"].ToString();
        objSummary.fld5 = row["4"].ToString();

        drList.Add(objSummary);
    }   
    allLists.Add(drList);

    //add other lists to allLists
    //..

    return allLists;
}
Sign up to request clarification or add additional context in comments.

1 Comment

yeah, simply you can do this :)
0

wrap your various lists into a bigger class i.e.

make a class something like

public class Wrapper
{
    public List<GlobalClasses.ValueDateSummary> list1 {get;set;}
    public List<GlobalClasses.ValueDateSummary> list2 {get;set;}
    public List<SomeOtherClassCollectino> list3{get;set;}
}

and return this wrapper in your webmethod i.e.

[WebMethod]
[ScriptMethod]
public static List<GlobalClasses.ValueDateSummary>  GetValueDateSummary()
{
    // your db logic
    Wrapper wrapper = new Wrapper();

    List<GlobalClasses.ValueDateSummary> drList = new List<GlobalClasses.ValueDateSummary>();
    foreach (DataRow row in dtValueDateSummary.Rows)
    {
        objSummary = new GlobalClasses.ValueDateSummary();
        objSummary.fld1= row["1"].ToString();
        objSummary.fld2 = row["2"].ToString();
        objSummary.fld3 = row["3"].ToString();
        objSummary.fld5 = row["4"].ToString();

        drList.Add(objSummary);
    }
    wrapper.list1 =  drList;
    //similarly fetch other lists 
    //wrapper.list2 = drList2;
    return new JavaScriptSerializer().Serialize(wrapper);
}

and on the client side you can access it like:

 var jsonData = $.parseJSON(msg.d);
 renderMsgSummary(jsonData.list1);
 renderMsgSummary(jsonData.list2);

Comments

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.