在更改参数Asp.net中的值时循环访问DataTable

时间:2013-11-26 14:18:45

标签: asp.net ajax vb.net c#-4.0

在我的应用程序中我想创建生成JSON数据的webservice但我的问题是它是第一次生成JSON数据但是当我更改参数中存储过程的值时,它不会从DataRow循环。

这是我的代码

<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

现在例如当我将0传递给MAKE参数然后是生成JSON,因为foreach循环将在datatable.Rows中工作,但是当我将MAKE值更改为类似23的东西时,它将不会循环通过DataTalbe行和JSON将不能生成

这是我的Ajax方法脚本,我使用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>

如果将在下面的参数中传递哪些值,如何循环抛出DataTable

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

1 个答案:

答案 0 :(得分:1)

我注意到你有一个空Catch语句。如果您知道这一行的事实:

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

...当MAKE为“23”时返回数据 - datatable将包含记录 - 然后我怀疑可能发生的事情是Try块中的某些内容导致了错误。由于您有一个空Catch语句,因此您不知道该错误是什么,并且代码仍在继续。

尝试从代码周围删除Try .. Catch .. End Try代码,或在Catch块中添加一些内容,以便在出现错误时告诉您。

ETA:如果没有出现错误,那么最有可能的是您的表datatable有0行。快速查找方法:在For Each之前添加以下行:

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

运行你的代码(在调试模式下,或什么都不会发生),看看你是否收到一个消息框,指出断言是不正确的。