无法转换类型'ServiceReference1.EmployeeModel为键入'System.IConvertible'.In以避免错误的对象,有什么可以v请勿?

无法转换类型'ServiceReference1.EmployeeModel“对象键入'System.IConvertible”。 正是上述错误,为了避免西隧我们要做的是什么?

protected void btnCreate_Click(object sender, EventArgs e) { JavaScriptSerializer jsn = new JavaScriptSerializer(); WebRequest request = WebRequest.Create("localhost:50506/Service1.svc/CreateEmployee";); request.Method = "POST"; request.ContentLength = 10; EmployeeModel emp = new EmployeeModel(); //i added emp fields in to emp Byte[] data = { Convert.ToByte(emp) }; Stream requestStream = request.GetRequestStream(); requestStream.Write(data, 0, data.Length); //in Byte[] data line the error is coming }

--------------解决方案-------------

该代码失败时,它试图给雇员对象转换为字节数组,因为Employee类不实现IConvertible接口。

我不知道你想完成什么,而是因为你已经宣布的JavaScriptSerializer,它看起来像你可能要返回JSON。 你可能会寻找这一则:

void btnCreate_Click(object sender, EventArgs e) {
JavaScriptSerializer jsn = new JavaScriptSerializer();
WebRequest request = WebRequest.Create("localhost:50506/Service1.svc/CreateEmployee";);
request.Method = "POST";
request.ContentLength = 10;
EmployeeModel emp = new EmployeeModel();

string data = jsn.Serialize(emp);
Stream requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
}

分类:asp.net 时间:2015-03-14 人气:0
本文关键词: asp.net,Web服务,JSON
分享到:

相关文章

Copyright (C) 55228885.com, All Rights Reserved.

55228885 版权所有 京ICP备15002868号

processed in 0.992 (s). 10 q(s)