WebService 不会将信息填充到 Winform 上的文本字段中
WebService won't populate information into Textfield on Winform
我有一个挑战,我最近写了一个 Web 服务,它能够从 MSSQL Server 获取数据并显示在 xml 中。
看起来像这样
Customer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Customer
/// </summary>
public class Customer
{
public string fullname { get; set; }
public string address { get; set; }
public string city { get; set; }
public string state { get; set; }
public string id_type { get; set; }
public string id_number { get; set; }
public string dob { get; set; }
public string bvn { get; set; }
}
并使用它从 Web 服务检索数据如下:
FetchInformationBVNService.cs
public Customer GetCustomerNameWithBVN(string bvn_search)
{
using (SqlConnection cn = new SqlConnection(constring))
{
cn.Open();
string q = "select fullname,address,city,state,id_type,id_number,date_ofbirth,bvn from account_info where bvn =@bvn";
using (SqlCommand cmd = new SqlCommand(q, cn))
{
cmd.Parameters.AddWithValue("@bvn", bvn_search);
using (SqlDataReader rd = cmd.ExecuteReader())
{
if (rd.Read())
{
return new Customer
{
fullname = rd["fullname"].ToString(),
address = rd["address"].ToString(),
city = rd["city"].ToString(),
state = rd["state"].ToString(),
id_type = rd["id_type"].ToString(),
id_number = rd["id_number"].ToString(),
dob = rd["date_ofbirth"].ToString(),
bvn = rd["bvn"].ToString()
};
}return null;
}
}
}
}
一切正常,在 IIS Express 上测试,不用担心。现在我创建了一个 Winform 我想使用相同的 Web 服务,以便它可以使用以下命名控件填充一些文本字段:fullname.Text、address.Text、city.Text state.Text,id_type.Text,id_number.Text,bvn.Text.
它根本就没有填充。我已经从 Solutions Explorer -> Add -> Service Reference -> Advanced -> Add Web Reference 添加了 Web Reference, 然后我将那里的引用重命名为 newAccountSearchByBVN
这给我们带来了到现在为止有这样的事情:
参考资料
**newAccountSearchByBVN.FetchInformationBVNService**
其中 newAccountSearchByBVN
是命名空间,FetchInformationBVNService
是服务。
现在我又做了类似的事情来检索以下信息:
newAccountSearchByBVN.Customer cs = new newAccountSearchByBVN.Customer();
newAccountSearchByBVN.FetchInformationBVNService nacc = new newAccountSearchByBVN.FetchInformationBVNService();
Still wont work..
问题是,如何检索信息并填充表单字段以显示数据库中的信息。
正在尝试从数据库填充数据
编辑:
现在我尝试像这样从 Web 服务填充数据:
private void button5_Click(object sender, EventArgs e)
{
newAccountSearchByBVN.Customer cs = new newAccountSearchByBVN.Customer();
newAccountSearchByBVN.FetchInformationBVNService nacc = new newAccountSearchByBVN.FetchInformationBVNService();
string a = nacc.GetCustomerNameWithBVN(bvn_search.Text).ToString();
bool check = bool.Parse(a);
if (check)
{
cs.fullname = fullname.Text;
cs.address = address.Text;
cs.city = city.Text;
cs.state = state.Text;
cs.id_type = id_type.Text;
cs.id_number = id_number.Text;
}
}
一些变化:
private void button5_Click(object sender, EventArgs e)
{
newAccountSearchByBVN.Customer cs; //no need to create new object, you'll be receiving it from server
newAccountSearchByBVN.FetchInformationBVNService nacc = new newAccountSearchByBVN.FetchInformationBVNService();
cs = nacc.GetCustomerNameWithBVN(bvn_search.Text); //instead of saving your Customer to string, save it to cs variable you already
if (cs != null) //check if you've received object, it's not null
{
//order was wrong. You want to populate text boxes, and you were taking data from text boxes here...
fullname.Text = cs.fullname;
address.Text = cs.address;
city.Text = cs.city;
state.Text = cs.state;
id_type.Text = cs.id_type;
id_number.Text = cs.id_number;
}
}
额外提示,您应该考虑为正确的数据使用正确的类型。例如(id_type
和 id_number
不应该是 string
,而是 int
)
我有一个挑战,我最近写了一个 Web 服务,它能够从 MSSQL Server 获取数据并显示在 xml 中。
看起来像这样
Customer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Customer
/// </summary>
public class Customer
{
public string fullname { get; set; }
public string address { get; set; }
public string city { get; set; }
public string state { get; set; }
public string id_type { get; set; }
public string id_number { get; set; }
public string dob { get; set; }
public string bvn { get; set; }
}
并使用它从 Web 服务检索数据如下:
FetchInformationBVNService.cs
public Customer GetCustomerNameWithBVN(string bvn_search)
{
using (SqlConnection cn = new SqlConnection(constring))
{
cn.Open();
string q = "select fullname,address,city,state,id_type,id_number,date_ofbirth,bvn from account_info where bvn =@bvn";
using (SqlCommand cmd = new SqlCommand(q, cn))
{
cmd.Parameters.AddWithValue("@bvn", bvn_search);
using (SqlDataReader rd = cmd.ExecuteReader())
{
if (rd.Read())
{
return new Customer
{
fullname = rd["fullname"].ToString(),
address = rd["address"].ToString(),
city = rd["city"].ToString(),
state = rd["state"].ToString(),
id_type = rd["id_type"].ToString(),
id_number = rd["id_number"].ToString(),
dob = rd["date_ofbirth"].ToString(),
bvn = rd["bvn"].ToString()
};
}return null;
}
}
}
}
一切正常,在 IIS Express 上测试,不用担心。现在我创建了一个 Winform 我想使用相同的 Web 服务,以便它可以使用以下命名控件填充一些文本字段:fullname.Text、address.Text、city.Text state.Text,id_type.Text,id_number.Text,bvn.Text.
它根本就没有填充。我已经从 Solutions Explorer -> Add -> Service Reference -> Advanced -> Add Web Reference 添加了 Web Reference, 然后我将那里的引用重命名为 newAccountSearchByBVN
这给我们带来了到现在为止有这样的事情:
参考资料
**newAccountSearchByBVN.FetchInformationBVNService**
其中 newAccountSearchByBVN
是命名空间,FetchInformationBVNService
是服务。
现在我又做了类似的事情来检索以下信息:
newAccountSearchByBVN.Customer cs = new newAccountSearchByBVN.Customer();
newAccountSearchByBVN.FetchInformationBVNService nacc = new newAccountSearchByBVN.FetchInformationBVNService();
Still wont work..
问题是,如何检索信息并填充表单字段以显示数据库中的信息。
正在尝试从数据库填充数据
编辑: 现在我尝试像这样从 Web 服务填充数据:
private void button5_Click(object sender, EventArgs e)
{
newAccountSearchByBVN.Customer cs = new newAccountSearchByBVN.Customer();
newAccountSearchByBVN.FetchInformationBVNService nacc = new newAccountSearchByBVN.FetchInformationBVNService();
string a = nacc.GetCustomerNameWithBVN(bvn_search.Text).ToString();
bool check = bool.Parse(a);
if (check)
{
cs.fullname = fullname.Text;
cs.address = address.Text;
cs.city = city.Text;
cs.state = state.Text;
cs.id_type = id_type.Text;
cs.id_number = id_number.Text;
}
}
一些变化:
private void button5_Click(object sender, EventArgs e)
{
newAccountSearchByBVN.Customer cs; //no need to create new object, you'll be receiving it from server
newAccountSearchByBVN.FetchInformationBVNService nacc = new newAccountSearchByBVN.FetchInformationBVNService();
cs = nacc.GetCustomerNameWithBVN(bvn_search.Text); //instead of saving your Customer to string, save it to cs variable you already
if (cs != null) //check if you've received object, it's not null
{
//order was wrong. You want to populate text boxes, and you were taking data from text boxes here...
fullname.Text = cs.fullname;
address.Text = cs.address;
city.Text = cs.city;
state.Text = cs.state;
id_type.Text = cs.id_type;
id_number.Text = cs.id_number;
}
}
额外提示,您应该考虑为正确的数据使用正确的类型。例如(id_type
和 id_number
不应该是 string
,而是 int
)