从表中,它只从EmpID中读取int值,但当它是字符串时,它显示错误

本文关键字:字符串 显示 错误 int EmpID 读取 | 更新日期: 2023-09-27 18:03:09

这是使用ajax和jquery的webmethod。当EmpID为123,12,123456时,它打开弹出框并显示我想要的输出,但当EmpID为A001,ABC时,它显示错误,如"检索数据时错误:"如何解决…提前感谢查询数据:-

 $(document).on("click", ".editButton", function () {
                $("#myModal").focus();
                var id = $(this).attr("data-id");
                console.log(id);
                $("#btnUpdate").attr("edit-id", id);
                //alert(id);
                $.ajax({
                    type: "Post",
                    contentType: "application/json; charset=utf-8",
                    url: "Default3.aspx/EditData",
                    data: '{eid: ' + id + '}',
                    dataType: "json",
                    success: function (data) {
                        var empDetails = $.parseJSON(data.d);
                        $.each(empDetails, function (index, value) {
                            $("#FirstName1").val(value.EmpID);
                            $("#MiddleName1").val(value.EmpName);
                            $("#Surname1").val(value.EmpAddress);
                            //alert("hi");
                        });
                    },
                    error: function () {
                        alert("Error while retrieving data of :" + id);
                    }
                });
            });
        });

表格:-

<div class="row">
    <div class="col-lg-12">
        <br />
        <br />
        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    Admin Employee Details Tables
                </div>
                <!-- /.panel-heading -->
                <div class="panel-body">
                    <div class="dataTable_wrapper">
                        <div id="dataTables-example_wrapper" class="dataTables_wrapper form-inline dt-bootstrap no-footer">
                            <div class="row">
                                <div class="col-sm-6">
                                    <div class="dataTables_length" id="dataTables-example_length">
                                        <label>
                                            Show
                                        <select name="dataTables-example_length" aria-controls="dataTables-example" class="form-control input-sm" id="select">
                                            <option value="5">5</option>
                                            <option value="10">10</option>
                                            <option value="25">25</option>
                                            <option value="50">50</option>
                                            <option value="100">100</option>
                                        </select>
                                            entries</label>
                                    </div>
                                </div>
                                <div class="col-sm-6">
                                    <div id="dataTables-example_filter" class="dataTables_filter">
                                        <label>Search:<input type="search" class="form-control input-sm" placeholder="" aria-controls="dataTables-example" id="searchtxt" /></label>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="table-responsive">
                                    <table id="dataTables-example" class="table table-striped table-bordered table-hover dataTable no-footer " role="grid" aria-describedby="dataTables-example_info">
                                        <thead>  
                                            <tr role="row">  
                                                <th class="sorting_asc" tabindex="0" aria-controls="dataTables-example" rowspan="1" colspan="1" style="width: 175px;" aria-sort="ascending" aria-label="Rendering engine: activate to sort column descending">Emp ID</th>  
                                                <th class="sorting" tabindex="0" aria-controls="dataTables-example" rowspan="1" colspan="1" style="width: 203px;" aria-label="Browser: activate to sort column ascending">Emp Name</th>  
                                                <th class="sorting" tabindex="0" aria-controls="dataTables-example" rowspan="1" colspan="1" style="width: 184px;" aria-label="Platform(s): activate to sort column ascending">Emp Address</th>  
                                                </tr>  
                                        </thead>
                                                <tbody>  
                                                    <% for (var data = 0; data < TableData.Rows.Count; data++)    
                                                                                                   { %>  
                                                        <tr class="gradeA odd " role="row">  
                                                            <td class="sorting_1">  
                                                                <%=TableData.Rows[data]["EmpID"]%>  
                                                            </td>  
                                                            <td>  
                                                                <%=TableData.Rows[data]["EmpName"]%>  
                                                            </td>  
                                                            <td>  
                                                                <%=TableData.Rows[data]["EmpAddress"]%>  
                                                            </td>  
                                                            <td>  
                                                                <input type="button" class="btn btn-primary editButton" data-id="<%=TableData.Rows[data]["EmpID"] %>" data-toggle="modal" data-target="#myModal" name="submitButton" id="btnEdit" value="Edit" />
                                                            </td>  
                                                            <td>  
                                                                <input type="button" class="btn btn-primary deleteButton" data-id="<%=TableData.Rows[data]["EmpId"] %>" name="submitButton" id="btnDelete" value="Delete" />  
                                                            </td>  
                                                        </tr>  
                                                        <% } %>  
                                                </tbody>  
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                    <!-- /.table-responsive -->
                </div>
                <!-- /.panel-body -->
            </div>
            <!-- /.panel -->
        </div>
    </div>
</div>

c# Code-

[WebMethod]
    public static string EditData(string eid)
     {
         string jsondata;
         using(var con=new SqlConnection(ConnectString))
         {
             var query = "Select * from NewEmp where EmpID='" + eid + "' ";
             using(var cmd=new SqlCommand(query, con))
             {
                 using(var sda=new SqlDataAdapter())
                 {
                     cmd.Connection = con;
                     sda.SelectCommand = cmd;
                     TableData.Clear();
                     sda.Fill(TableData);
                     jsondata = JsonConvert.SerializeObject(TableData);
                 }
             }
         }
         return jsondata;
     }

从表中,它只从EmpID中读取int值,但当它是字符串时,它显示错误

听起来ID字段在某处被设置为整数值,这样数字可以工作,但字母会抛出错误。您的JSON看起来也很混乱,有奇怪的字段映射:

$("#FirstName1").val(value.EmpID);
                        $("#MiddleName1").val(value.EmpName);
                        $("#Surname1").val(value.EmpAddress);

FirstName可能不能很好地映射到EmpID。中间名到EmpName等

但总的来说,它听起来像是期待一个整数输入,而你给它字母。查找它并将输入期望更改为varchar/string或将输入更改为仅由数字组成。