0

无法将“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
}
4

1 回答 1

0

代码在尝试将员工对象转换为字节数组时失败,因为员工类未实现 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); 
  } 
于 2012-04-06T13:38:35.713 回答