Ayoub Laazazi Ayoub Laazazi - 3 months ago 24
ASP.NET (C#) Question

ASP.NET Getting wrong data from WebService

The getEditData method in employes.aspx.cs :

[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 :

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); for the 4 rows :

Object { id: 39, prenom: "ayoub", nom: "laazazi", imei: "35blabla..227", sexe: "male", tel: "06blabla..4", comment: "meme" }

Object { id: 40, prenom: "admin", nom: "admin", imei: "744444", sexe: "male", tel: null, comment: null }

Object { 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.

What I get in the modal Select (PS: the Select values are : male and female) :

2 examples of the Modals image

The table [employe] in SQL Server :

SQL Server table image

The openEdit(id) Button :

<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:

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"));
}
}

Answer

I think the problem is with the null value for 'imei' preceding 'sexe' and this is causing the reader to fallover.

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.

Comments