简体   繁体   中英

loop through DataTable while Changing the values in parameters Asp.net

in my application I want to create webservice which generate the JSON DATA but my problem is that is it Generating the JSON Data First time but when I change the value of stored Procedure in Parameters then it don't loop through from DataRow.

here is my code

<WebMethod(EnableSession:=True)> _
    Public Shared Function Select_Search(ByVal MAKE As String, ByVal COLOR As String, ByVal CATAGORY As String, ByVal MODEL As String, ByVal TRANSMISSION As String, ByVal FUAL_TYPE As String, ByVal DRIVE As String, ByVal CHASSIS_NO As String) As SearchParameters()
        Dim ListbyClass As New List(Of SearchParameters)()
        Dim datatable As New DataTable()
        Dim myStock As New dsStockTableAdapters.newSTOCK_LISTTableAdapter()

        datatable = myStock.GetData(MAKE, "0", "0", "0", "0", DisplayType, "", "", "", "Any", "", "", "", "", "", 0, "", "", 0, "")

        Try
            'here when i change the above parameters value then looping is not working
            For Each rdr As DataRow In datatable.Rows

                Dim SRCH As New SearchParameters()
                SRCH.CHASSIS_NO = rdr("CHASSIS_NO").ToString()
                SRCH.MODEL = rdr("MODEL").ToString()
                SRCH.color = rdr("color").ToString()
                SRCH.TRANSMISSION = rdr("TRANSMISSION").ToString()
                SRCH.DOOR = rdr("DOOR").ToString()
                SRCH.MAKE = rdr("MAKE").ToString()
                SRCH.Image1 = rdr("Image1").ToString()
                SRCH.MODEL_DESCRIPTION = rdr("MODEL_DESCRIPTION").ToString()
                ListbyClass.Add(SRCH)

            Next
        Catch
        End Try

        Return ListbyClass.ToArray()

    End Function

Now for example when i pass 0 to MAKE parameter then is is generating the JSON because foreach loop will work in datatable.Rows but as i change the MAKE value to somthing like 23 it will not loop through the DataTalbe Rows and JSON will not be Generated

Here is my Ajax method script i am passing values to parameters by using jQuery.

<script type="text/javascript">

        function StockList() {
            $("#update").empty();
            var make = $('#<%= ddlMake.ClientID %>').val();
            var color = $('#<%= ddlColor.ClientID %>').val();
            var catagory = $('#<%= ddlCat.ClientID %>').val();
            var model = $('#<%= ddlMakeModel.ClientID %>').val();
            var transmission = $('#<%= ddltransmission.ClientID%>').val();
            var fual_type = $('#<%= rdfuelType.ClientID%>').val();
            var drive = $('#<%= ddldrive.ClientID %>').val();    
            var chassis_no = $('#<%= txtChassis_No.ClientID %>').val();

            var data = { MAKE: make, COLOR: color, CATAGORY: catagory, MODEL: model, TRANSMISSION: transmission, FUAL_TYPE: fual_type, DRIVE: drive, CHASSIS_NO: chassis_no }
            var jsonData = JSON.stringify(data);

            $.ajax({
                type: "POST",
                url: "stocklist.aspx/Select_Search",
                data: jsonData,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(data) {
                    console.log(data);

                    var j = 1;
                    var count = 9;

                    for (var i = 0; i < data.d.length; i++) {
                        var output = '<ul class="selection page-' + (i == count || (i && !(i % 9)) ? j++ : j) + '">';
                        output += '<li >';
                        output += '<table><tr>';
                        output += '<td><img src="http://localhost:37245/NewPeaceAuto - Steer_Well/WebStock_Images/' + data.d[i].Image1 + '" alt=""/></td>';
                        output += '<td>CHASSIS NO:</td><td>' + data.d[i].CHASSIS_NO + '</td>';
                        output += '<td>MODEL:</td><td>' + data.d[i].MODEL + '</td>';
                        output += '<td>COLOR:</td><td>' + data.d[i].color + '</td>';
                        output += '<td>TRANS:</td><td>' + data.d[i].TRANSMISSION + '</td>';
                        output += '<td>DOOR:</td><td>' + data.d[i].DOOR + '</td>';
                        output += '<td>MAKE:</td><td>' + data.d[i].MAKE + '</td>';
                        output += '</tr></table></li></ul>';
                        $("#update").append(output);
                        count = count + 9;
                    }

                }
            });
        }
</script>

How to loop throwgh the DataTable no matter which values will be passed in parameters like below

                             22     5        3       11       2
datatable = myStock.GetData(MAKE, MODEL, CATAGORY, COLOR, FUAL_TYPE .....)

I notice that you have an empty Catch statement. If you know for a fact that this line:

datatable = myStock.GetData(MAKE, "0", "0", "0", "0", DisplayType, "", "", "", "Any", "", "", "", "", "", 0, "", "", 0, "")

... returns data when MAKE is "23" - that datatable will contain records - then what I suspect may be happening is that something in the Try block is causing an error. Since you have an empty Catch statement, you don't know what that error is, and the code continues on.

Try either removing the Try .. Catch .. End Try code from around the code, or put something in the Catch block to tell you when you have an error.

ETA: If no errors are showing up, then the most likely possibility is that your table datatable has 0 rows. A quick way to find out: add the following line before your For Each :

 System.Diagnostics.Debug.Assert(datatable.Rows.Count > 0);

Run your code (in debug mode, or nothing will happen) and see if you get a message box indicating that the assertion is NOT true.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM