employes.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);
}
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');
}
console.log(parsedData)的结果;对于4行:
对象{id:39,prenom:“ayoub”,nom:“laazazi”,imei:“35blabla..227”,性别:“男性”,电话:“06blabla..4”,评论:“meme” }
对象{id:40,prenom:“admin”,nom:“admin”,imei:“744444”,性别:“男性”,电话:null,评论: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} //这里的问题sexe不应该是NULL作为SQL服务器形象说。
我在模态选择中得到的结果(PS:选择值是:男性和女性):
2 examples of the Modals image
SQL Server中的表[employe]:
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>
员工类:
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;
}
}
}
修改
感谢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"));
}
}
答案 0 :(得分:1)
从我看到你正试图获得一个单一的&#39;记录。试试这个......
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"));
}
}
另外,在您的AJAX中,您可能需要更改&#39; POST&#39;到&#39; GET&#39;
答案 1 :(得分:1)
我认为问题在于&#39; imei&#39;在&#39; sexe&#39;这导致读者失败。
替换
e.imei = reader.GetString(reader.GetOrdinal("phone_IMEI"));
与
int ordinal = reader.GetOrdinal('phone_IMEI');
if(!reader.IsDbNull(ordinal))
{
e.imei = reader.GetString(ordinal);
}
else
{
e.imei = string.Empty;
}
有更好的方法来写这个,但试着看看是否能解决它。您还必须对可能返回null的任何其他值执行此操作,尝试为reader对象编写扩展方法。