Search code examples
c#sql-serverweb-servicesweb-reference

WebService won't populate information into Textfield on Winform


I have a challenge, I recently wrote a Webservice which is capable of Getting data from MSSQL Server and show in xml.

Looks like this

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; }
}

And using it from the Web service to retrieve data is given as thus :

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;
            }
        }

    }
}

All Works Fine, testing on IIS Express, No worries. here now I created a Winform i want to use the Same Web service so that it can populate some textField with the following named controls : fullname.Text,address.Text,city.Text,state.Text,id_type.Text,id_number.Text,bvn.Text.

It just doesnt populate at all. I have added the Web Reference from Solutions Explorer -> Add -> Service Reference -> Advanced -> Add Web Reference , then i renamed the reference there to newAccountSearchByBVN Which brings us to this point of having something like this :

References

**newAccountSearchByBVN.FetchInformationBVNService** where newAccountSearchByBVN is the namespace and FetchInformationBVNService is the service.

Now i in turn did something like this to retrieve the following information :

    newAccountSearchByBVN.Customer cs = new newAccountSearchByBVN.Customer();
    newAccountSearchByBVN.FetchInformationBVNService nacc = new newAccountSearchByBVN.FetchInformationBVNService();

Still wont work..

Question is , How do i get to retrieve the information and populate the form fields to show the information from the Database.

Trying to Populate Data From DB

Edit: Now i Try to populate the Data from the Web service Like this :

    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;

        }
}

Solution

  • Some changes:

     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;
    
            }
    }
    

    Additional tip, you should consider using right types for right data. For example (id_type and id_number shouldn't be string, but int)