[英]Calling a Restful Web Service that has HttpRequestMessage as a parameter?
[英]Calling a web service that has list of a model as a parameter to a web form
我的服务看起来像这样...
[WebMethod]
public bool insertRecord(List<People> stu)
{
using (LearnershipDBEntities dn = new LearnershipDBEntities())
{
Person obj = new Person();
foreach (var item in stu)
{
obj.personID = item.personId;
obj.dateOfBirth = item.dob;
obj.idNumber = item.idNumber;
obj.name = item.name;
obj.title = item.title;
}
dn.People.Add(obj);
dn.SaveChanges();
}
return true;
}
我的表格有此代码
private void button2_Click(object sender, EventArgs e)
{
try
{
ToDbXml.Service2 nb = new ToDbXml.Service2();
Service2Client s = new Service2Client();
People dto = new People();
for (int i = 0; i <= 4; i++)
{
dto.name = string.IsNullOrEmpty(dataGridView1.Rows[i].Cells[0].Value.ToString()) ? string.Empty : dataGridView1.Rows[i].Cells[0].Value.ToString();
dto.dob = string.IsNullOrEmpty(dataGridView1.Rows[i].Cells[1].Value.ToString()) ? (DateTime)System.Data.SqlTypes.SqlDateTime.Null : DateTime.Parse(dataGridView1.Rows[i].Cells[1].Value.ToString());
dto.title = string.IsNullOrEmpty(dataGridView1.Rows[i].Cells[2].Value.ToString()) ? string.Empty : dataGridView1.Rows[i].Cells[2].Value.ToString();
dto.idNumber = string.IsNullOrEmpty(dataGridView1.Rows[i].Cells[3].Value.ToString()) ? string.Empty : dataGridView1.Rows[i].Cells[3].Value.ToString();
}
s.insertRecord(dto);
MessageBox.Show("Values Inserted Succesfully");
}
catch (SqlException ex)
{
MessageBox.Show( ex.Message);
}
}
当我在传递给我的服务的对象上悬停时,出现此错误。 不能在人与人之间转换[]
您需要构造一个人员列表并将其传递给webservice方法。 因此,我建议您像这样修改Web服务的源代码:
[WebMethod]
public bool insertRecord(List<People> stu)
{
using (LearnershipDBEntities dn = new LearnershipDBEntities())
{
Person obj = new Person();
foreach (var item in stu)
{
obj.personID = item.personId;
obj.dateOfBirth = item.dob;
obj.idNumber = item.idNumber;
obj.name = item.name;
obj.title = item.title;
dn.People.Add(obj);
}
dn.SaveChanges();
}
return true;
}
和事件按钮处理程序是这样的:
private void button2_Click(object sender, EventArgs e)
{
try
{
ToDbXml.Service2 nb = new ToDbXml.Service2();
Service2Client s = new Service2Client();
List<People> listOfPeople = new List<People>();
for (int i = 0; i <= 4; i++)
{
listOfPeople.add (
new People()
{
name = string.IsNullOrEmpty(dataGridView1.Rows[i].Cells[0].Value.ToString()) ? string.Empty : dataGridView1.Rows[i].Cells[0].Value.ToString(),
dob = string.IsNullOrEmpty(dataGridView1.Rows[i].Cells[1].Value.ToString()) ? (DateTime)System.Data.SqlTypes.SqlDateTime.Null : DateTime.Parse(dataGridView1.Rows[i].Cells[1].Value.ToString()),
title = string.IsNullOrEmpty(dataGridView1.Rows[i].Cells[2].Value.ToString()) ? string.Empty : dataGridView1.Rows[i].Cells[2].Value.ToString(),
idNumber = string.IsNullOrEmpty(dataGridView1.Rows[i].Cells[3].Value.ToString()) ? string.Empty : dataGridView1.Rows[i].Cells[3].Value.ToString()
});
}
s.insertRecord(listOfPeople);
MessageBox.Show("Values Inserted Succesfully");
}
catch (SqlException ex)
{
MessageBox.Show( ex.Message);
}
}
希望这能解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.