无法在控制台应用程序中将值''解析为类型'布尔值

问题描述 投票:0回答:2

我正在使用控制台应用程序中的wcf服务。我想根据帐号检索帐户信息。当我输入帐号时,它能够方法没有任何错误,但我在控制台应用程序中遇到以下错误。

:'格式化程序在尝试反序列化消息时抛出异常:尝试反序列化参数http://tempuri.org/:AccountBalanceCheekResult时出错。 InnerException消息是'反序列化System.Boolean类型的对象时出错。值''无法解析为'Boolean'类型。'。有关更多详细信息,请参阅InnerException。

这是班级。

[DataContract]
    public class AccountBalanceRequest : Current_Account_Details
    {
        string account_number;

        [DataMember]
        public string Account_Number
        {
            get { return account_number; }
            set { account_number = value; }
        }
    }
}

这是实现,我没有在ADO.NET代码中发现任何错误。

   public AccountBalanceRequest AccountBalanceCheek(AccountBalanceRequest accountNumber)
        {
            using (SqlConnection conn = new SqlConnection(ConnectionString))
            {
                conn.Open();

                var cmd = new SqlCommand("SELECT * FROM Current_Account_Details WHERE Account_Number = '" + accountNumber.Account_Number + "'", conn);


                cmd.CommandType = CommandType.Text;

                var reader = cmd.ExecuteReader();
                //read the result of the execute command.
                while (reader.Read())
                {
                    //assuming that your property is the same as your table schema. refer to your table schema Current_Account_Details

                    accountNumber.Account_Number = reader["Account_Number"].ToString();
                    accountNumber.Account_Creation_Date = reader["Account_Creation_Date"].ToString();

                    accountNumber.Account_Type = reader["Account_Type"].ToString();
                    accountNumber.Branch_Sort_Code = reader["Branch_Sort_Code"].ToString();
                    accountNumber.Account_Fees = reader["Account_Fees"].ToString();
                    accountNumber.Account_Balance = reader["Account_Balance"].ToString();
                    accountNumber.Over_Draft_Limit = reader["Over_Draft_Limit"].ToString();
                }
                return accountNumber;
            }
        }

这是控制台窗口代码。

  public static void Balance()
        {
            MyService.HalifaxCurrentAccountServiceClient currentAccount = new MyService.HalifaxCurrentAccountServiceClient("NetTcpBinding_IHalifaxCurrentAccountService");
            MyService.AccountBalanceRequest cs = new MyService.AccountBalanceRequest();


            string AccountNumber;


            Console.WriteLine("\nEnter your Account Number--------:");
            AccountNumber = Console.ReadLine();
            cs.Account_Number = AccountNumber;
            // MyService.AccountBalanceRequest cs1 = currentAccount.AccountBalanceCheek(AccountNumber);



           if (currentAccount.AccountBalanceCheek(cs))**//Error on this line**
            {

                    Console.WriteLine("Your Account Number is :" + cs.Account_Number);
                    Console.WriteLine("Your Account Type :" + cs.Account_Balance);
                    Console.WriteLine("Your Account Account Fee :" + cs.Account_Fee);
                    Console.WriteLine("Your Account Balance:" + cs.Account_Balance);
                    Console.WriteLine("Your Account Over Draft Limit :" + cs.Over_Draft_Limit);

                    Console.Write("--------------------------");
                    Console.ReadLine();
                    //Console.Clear();


           }

        }

这是我运行应用程序时的屏幕截图。

click here to see the error

c# wcf ado.net console-application
2个回答
3
投票

在您的代码中,您可以进行测试

 if (currentAccount.AccountBalanceCheek(cs))

这假设AccountBalanceCheek()函数返回bool,但函数的签名表明它返回AccountBalanceRequest类型而不是布尔值。

public AccountBalanceRequest AccountBalanceCheek(AccountBalanceRequest accountNumber)

你有几个选项,要么调整qazxsw poi来返回一个布尔值,要么调整if函数进行比较,即

AccountBalanceCheek

0
投票

您还可以使用另外一种方法。

由于您正在从控制台读取帐号,并且如果读取器对象能够找出至少一个记录,这意味着存在具有此类号码/ ID的帐户,则其他帐号不存在。

if(currentAccount.AccountBalanceCheek(cs) == someExpectedType)

还要更改方法的签名

    bool IsExist=false;

    while (reader.Read())
    {
    //into the loop means at least one record found so
    IsExist=true;
    accountNumber.Account_Number = reader["Account_Number"].ToString();
    }
return IsExist;

public AccountBalanceRequest AccountBalanceCheek(AccountBalanceRequest accountNumber)
© www.soinside.com 2019 - 2024. All rights reserved.