类不返回值

时间:2018-08-18 19:10:38

标签: c# sql-server web-services

美好的一天,

我有此代码,现在我希望它返回数据以返回全名,Id_type,Id_Number,出生日期,BVN

现在,我希望它从Web服务API中提取信息,然后显示为文本字段

我的课看起来像这样

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public class Customer
{
    public string fullname { get; set; }
    public string id_type { get; set; }
    public string id_number { get; set; }
    public string dob { get; set; }
    public string bvn { get; set; }
}

代码看起来像这样:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Windows.Forms;
using System.Web.Services;


[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]

public class FetchInformationBVNService : System.Web.Services.WebService
{
    string constring = @"Data Source=localhost\sqlexpress;Initial Catalog=*****;Integrated Security=True";
    public FetchInformationBVNService()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }

    [WebMethod]
    public Customer GetCustomerNameWithBVN(string bvn)
    {
        using (SqlConnection cn = new SqlConnection(constring))
        {
            cn.Open();
            string q = "select fullname,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);
                using (SqlDataReader rd = cmd.ExecuteReader())
                {
                    while (rd.Read())
                    {
                        return new Customer
                        {
                            fullname = rd["fullname"].ToString(),
                            id_type = rd["id_type"].ToString(),
                            id_number = rd["id_number"].ToString(),
                            dob = rd["date_ofbirth"].ToString(),
                            bvn = rd["bvn"].ToString()
                        };
                    }
                }
            }

        }

    }
}

现在我得到以下错误:

Not all Code paths return a value on line 34,喜欢它出了什么问题?

2 个答案:

答案 0 :(得分:1)

如果rd.Read()false,将不执行return语句。由于您只想返回一位客户,因此请使用if语句而不是循环

if (rd.Read())
{
    return new Customer
    {
        fullname = rd["fullname"].ToString(),
        id_type = rd["id_type"].ToString(),
        id_number = rd["id_number"].ToString(),
        dob = rd["date_ofbirth"].ToString(),
        bvn = rd["bvn"].ToString()
    };
}
return null; // In case no record is returned.

或返回空客户

return new Customer();

注意:即使您知道rd.Read()不会失败,C#也不会失败,并断定该方法将退出而不返回值。因此,请满足C#并添加此附加的return语句。

答案 1 :(得分:0)

如果rd.Read()不返回任何数据,则将跳过return行,因此在这种情况下,您可能要抛出异常或返回null。

顺便说一句,您是否要回头客?您在while循环中有return语句;如果您需要多个返回值,则需要将函数的返回类型更改为IEnumerable<Customer>,并将return替换为yield return