简体   繁体   English

ASP.NET从WebService获取错误的数据

[英]ASP.NET Getting wrong data from WebService

The getEditData method in employes.aspx.cs : serve.aspx.cs中的getEditData方法:

[System.Web.Services.WebMethod]
public static string getEditData(int id)
{
    Employe e = new Employe();
    try
    {
        using (SqlConnection openCon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyDBConnection"].ConnectionString))
        {
            string saveStaff = "SELECT * FROM employes WHERE id=@id";

            using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))
            {
                querySaveStaff.Connection = openCon;

                querySaveStaff.Parameters.Add("@id", SqlDbType.Int).Value = id;

                openCon.Open();

                using (SqlDataReader reader = querySaveStaff.ExecuteReader())
                {
                    // Check is the reader has any rows at all before starting to read.
                    if (reader.HasRows)
                    {
                        // Read advances to the next row.
                        while (reader.Read())
                        {
                            e.id = reader.GetInt32(reader.GetOrdinal("id"));
                            e.prenom = reader.GetString(reader.GetOrdinal("prenom"));
                            e.nom = reader.GetString(reader.GetOrdinal("nom"));
                            e.imei = reader.GetString(reader.GetOrdinal("phone_IMEI"));
                            e.sexe = reader.GetString(reader.GetOrdinal("sexe"));
                            e.tel = reader.GetString(reader.GetOrdinal("tel"));
                            e.comment = reader.GetString(reader.GetOrdinal("comment"));
                        }
                    }
                }
                openCon.Close();
            }
        }
    }
    catch (Exception ex) { }

    JavaScriptSerializer js = new JavaScriptSerializer();
    return js.Serialize(e);
}

The function JS / AJAX : 函数JS / AJAX:

function openEdit(id) {
    $.ajax({
        type: "POST",
        url: "employes.aspx/getEditData",
        data: '{ id: ' + id + ' }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            var parsedData = JSON.parse(data.d);
            console.log(parsedData);
            $("#idEdit").val(parsedData.id);
            $("#prenomEdit").val(parsedData.prenom);
            $("#nomEdit").val(parsedData.nom);
            $("#imeiEdit").val(parsedData.imei);
            $("#sexeEdit").val(parsedData.sexe);
            $("#telEdit").val(parsedData.tel);
            $("#commentEdit").val(parsedData.comment);
        },
        error: function (data) { console.log("error"); }
    });

    $("#myModalEdit").modal('toggle');
}

The result of console.log(parsedData); console.log(parsedData);的结果 for the 4 rows : 对于4行:

Object { id: 39, prenom: "ayoub", nom: "laazazi", imei: "35blabla..227", sexe: "male", tel: "06blabla..4", comment: "meme" } 对象{id:39,prenom:“ ayoub”,nom:“ laazazi”,imei:“ 35blabla..227”,sexe:“ male”,电话:“ 06blabla..4”,评论:“ meme”}

Object { id: 40, prenom: "admin", nom: "admin", imei: "744444", sexe: "male", tel: null, comment: null } 对象{id:40,前缀:“ admin”,nom:“ admin”,imei:“ 744444”,sexe:“ male”,电话:null,注释:null}

Object { id: 53, prenom: "lawl", nom: "k", imei: "555", sexe: "female", tel: null, comment: null } 对象{id:53,prenom:“ lawl”,nom:“ k”,imei:“ 555”,sexe:“ female”,tel:null,comment:null}

Object { id: 54, prenom: "gfd235", nom: "sdfgh2", imei: null, sexe: null, tel: null, comment: null } //Problem here sexe shouldn't be NULL as the SQL server image says. Object {id:54,prenom:“ gfd235”,nom:“ sdfgh2”,imei:null,sexe:null,tel:null,comment:null} //问题在这里sexe不应为NULL,因为SQL Server映像显示。

What I get in the modal Select (PS: the Select values are : male and female) : 我在模式选择中得到了什么(PS:选择值是:male和female):

2 examples of the Modals image 模态图像的2个示例

The table [employe] in SQL Server : SQL Server中的表[employe]:

SQL Server table image SQL Server表映像

The openEdit(id) Button : openEdit(id)按钮:

    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
    <asp:Timer ID="Timer1" runat="server" Interval="600000" ontick="Timer1_Tick"></asp:Timer>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:MyDBConnection %>" 
        SelectCommand="SELECT * FROM [employes]"></asp:SqlDataSource>

    <div id="info"></div>
    <!-- Data Display DIV -->
    <div id="viewdata" class="overflowTable">
        <asp:repeater id="Repeater1" datasourceid="SqlDataSource1" runat="server">
            <HeaderTemplate>
                <table class="table table-bordered table-hover table-colored results">
                    <thead>
                        <tr>
                        <th>Nom</th>
                        <th>Sexe</th>
                        <th>Code IMEI</th>
                        <th>Commentaire</th>
                        <th>Action</th>
                        </tr>
                    </thead>
                    <tbody id="myTbody">

            </HeaderTemplate>

            <ItemTemplate>
            <tr>
                <td onclick="selectRow(this)" class="restd"><%# generateName(Eval("prenom"), Eval("nom"))%></td>
                <td onclick="selectRow(this)" class="restd"><%# checkNull(Eval("sexe").ToString())%></td>
                <td onclick="selectRow(this)" class="restd"><%# checkNull(Eval("phone_IMEI").ToString()) %></td>
                <td onclick="selectRow(this)" class="restd"><%# checkNull(Eval("comment").ToString())%></td>
                <td>
                    <asp:LinkButton class="btn btn-success btn-sm" title="Modifier un client" onclick="openEdit(<%# Eval("id") %>)"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></asp:LinkButton>
                    <!-- this is the button that opens the modal -->
                </td>
            </tr>
            </ItemTemplate>

            <FooterTemplate>
                </tbody>
                </table>
            </FooterTemplate>
        </asp:repeater>
    </div>
</ContentTemplate>
</asp:UpdatePanel> 

Employe Class : 就业班:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace BlablaWebService
{
    public class Employe
    {
        public int id { get; set; }
        public string prenom { get; set; }
        public string nom { get; set; }
        public string imei { get; set; }
        public string sexe { get; set; }
        public string tel { get; set; }
        public string comment { get; set; }

        public Employe() { }

        public Employe(int id, string prenom, string nom, string imei, string sexe, string tel, string comment)
        {
            this.id = id;
            this.prenom = prenom;
            this.nom = nom;
            this.imei = imei;
            this.sexe = sexe;
            this.tel = tel;
            this.comment = comment;
        }
    }
}

EDIT 编辑

Thanks to Pól O' Muilleoir, this is my final working code: 感谢PólO'Muilleoir,这是我的最终工作代码:

using (SqlDataReader reader = querySaveStaff.ExecuteReader())
{
    if (reader.Read())
    {
        e.id = reader.GetInt32(reader.GetOrdinal("id"));
        e.prenom = reader.IsDBNull(reader.GetOrdinal("prenom")) ? string.Empty : reader.GetString(reader.GetOrdinal("prenom"));
        e.nom = reader.IsDBNull(reader.GetOrdinal("nom")) ? string.Empty : reader.GetString(reader.GetOrdinal("nom"));
        e.imei = reader.IsDBNull(reader.GetOrdinal("phone_IMEI")) ? string.Empty : reader.GetString(reader.GetOrdinal("phone_IMEI"));
        e.sexe = reader.IsDBNull(reader.GetOrdinal("sexe")) ? string.Empty : reader.GetString(reader.GetOrdinal("sexe"));
        e.tel = reader.IsDBNull(reader.GetOrdinal("tel")) ? string.Empty : reader.GetString(reader.GetOrdinal("tel"));
        e.comment = reader.IsDBNull(reader.GetOrdinal("comment")) ? string.Empty : reader.GetString(reader.GetOrdinal("comment"));
    }
}

From what I see you are trying to get a 'single' record. 从我的角度来看,您正在尝试获得“单一”记录。 try this instead ... 试试这个代替...

using (SqlDataReader reader = querySaveStaff.ExecuteReader())
            {
                if (reader.Read())
                    {
                        e.id = reader.GetInt32(reader.GetOrdinal("id"));
                        e.prenom = reader.GetString(reader.GetOrdinal("prenom"));
                        e.nom = reader.GetString(reader.GetOrdinal("nom"));
                        e.imei = reader.GetString(reader.GetOrdinal("phone_IMEI"));
                        e.sexe = reader.GetString(reader.GetOrdinal("sexe"));
                        e.tel = reader.GetString(reader.GetOrdinal("tel"));
                        e.comment = reader.GetString(reader.GetOrdinal("comment"));
                    }
                }

also, in your AJAX, you may need to change the type from 'POST' to 'GET' 另外,在您的AJAX中,您可能需要将类型从“ POST”更改为“ GET”

I think the problem is with the null value for 'imei' preceding 'sexe' and this is causing the reader to fallover. 我认为问题出在'sexe'之前'imei'的空值,这导致读者失败。

replace 更换

e.imei = reader.GetString(reader.GetOrdinal("phone_IMEI"));

with

int ordinal = reader.GetOrdinal('phone_IMEI');

    if(!reader.IsDbNull(ordinal))
    {
       e.imei = reader.GetString(ordinal);
    } 
    else
    {
       e.imei = string.Empty;
    }

There are better ways to write this but try it to see if that solves it. 有更好的方法可以编写此代码,但是请尝试看看是否可以解决。 You will also have to do this for any other values that may return null, try writing an extension method for reader object. 您还必须对可能返回null的任何其他值执行此操作,请尝试为reader对象编写扩展方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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