WebService不会将信息填充到Winform的Textfield中

时间:2018-08-20 14:45:39

标签: c# sql-server web-services web-reference

我面临一个挑战,我最近编写了一个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,我想使用Same Web服务,以便它可以使用以下命名控件填充一些textField: fullname.Text,address.Text,city.Text,state.Text,id_type.Text, id_number.Text,bvn.Text。

它根本不填充。我已经从解决方案资源管理器->添加->服务参考->高级->添加Web参考添加了Web参考,然后将参考重命名为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;

        }
}

1 个答案:

答案 0 :(得分:0)

一些更改:

public class LevelLoader : MonoBehaviour {

public enum Level {
    Level1,
    Level2,
Level3
}

public Object[] levelPrefabs;

public void Load(Level level)
{
    int levelIndex = (int)level;
    if (levelIndex >= 0 && levelIndex < levelPrefabs.Length) {
        Instantiate(levelPrefabs[levelIndex]);
    } else {
        Debug.LogError("Invalid level index: " + levelIndex);
    }
}
}

其他提示,您应该考虑对正确的数据使用正确的类型。例如,{ 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