WebService 不会将信息填充到 Winform 上的文本字段中

我有一个挑战,我最近编写了一个 Web 服务,它能够从 MSSQL Server 获取数据并以 xml 显示。


看起来像这样


客户.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 服务,以便它可以使用以下命名控件填充一些 textField:fullname.Text,address.Text,city.Text,state.Text,id_type.Text,id_number.Text ,bvn.Text。


茅侃侃
浏览 195回答 1
1回答

弑天下

一些变化:&nbsp;private void button5_Click(object sender, EventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; newAccountSearchByBVN.Customer cs; //no need to create new object, you'll be receiving it from server&nbsp; &nbsp; &nbsp; &nbsp; newAccountSearchByBVN.FetchInformationBVNService nacc = new newAccountSearchByBVN.FetchInformationBVNService();&nbsp; &nbsp; &nbsp; &nbsp; cs = nacc.GetCustomerNameWithBVN(bvn_search.Text); //instead of saving your Customer to string, save it to cs variable you already&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if (cs != null) //check if you've received object, it's not null&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //order was wrong. You want to populate text boxes, and you were taking data from text boxes here...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fullname.Text = cs.fullname;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; address.Text = cs.address;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; city.Text = cs.city;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state.Text = cs.state;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id_type.Text = cs.id_type;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id_number.Text = cs.id_number;&nbsp; &nbsp; &nbsp; &nbsp; }}附加提示,您应该考虑为正确的数据使用正确的类型。例如(id_type并且id_number不应该是string,但是int)
打开App,查看更多内容
随时随地看视频慕课网APP