0

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;

        }
}
Valentine
  • 3
  • 2
  • 1. Show the code from your WinForm where you're trying to populate. 2. In your last snippet, you're creating instance of FetchInformationBVNService , but you're not calling your method GetCustomerNameWithBVN anywhere – Caldazar Aug 20 '18 at 14:54
  • @Caldazar, Its Edited you can see how i did it here – Valentine Aug 20 '18 at 14:56
  • Did you try putting breakpoint and see if `a` actually has something in it? Second, you are returning Customer object, and try to put it in string. That doesn't work that way. You can use var, or declare a to be of type Customer (and I suggest you change naming of variables). And third, your bool conversion won't work. Can't convert Customer object to bool like that... You should check if a is not null instead – Caldazar Aug 20 '18 at 15:03
  • @Caldazar, Lost here, would be nice if you show how i'd go about it pls programmatically – Valentine Aug 20 '18 at 15:06

1 Answers1

0

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)

Caldazar
  • 2,801
  • 13
  • 21