out bool参数总是假的 [英] out bool parameter always false

查看:203
本文介绍了out bool参数总是假的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论terms属性是什么值(true或false),out bool参数总是false ...我错了..我的代码在这里..?



No matter what value the terms attribute is (true or false), the out bool parameter is always false... Where i am wrong.. My Code in this ..?

<pre>
        public bool CheckUser(string firstName, string surname, string email, DateTime dob, out int PersonID, out int CategoryID, out bool terms, out string categoryDesc)
        {
            DataSet ds = new DataSet();
            
            bool userExists = false;            
            PersonID = 0;
            CategoryID = 0;
            categoryDesc = String.Empty;
            terms = false;

            OpenConnection();

            try
            {
                using (ISqlCommand command = CreateCommand())
                {
                    command.SetCommandText(this.CheckUserCommandText);
                    command.SetStringParameter("FirstName", firstName);
                    command.SetStringParameter("Surname", surname);
                    command.SetStringParameter("Email", email);
                    command.SetDateTimeParameter("DOB", dob);

                    command.OpenCommand(ds, "User");                                      
                    
                    try
                    {
                        PersonID = Converter.ToInteger(ds.Tables[0].Rows[0]["PersonID"]);
                        CategoryID = Converter.ToInteger(ds.Tables[0].Rows[0]["CategoryID"]);
                        terms = Convert.ToBoolean(ds.Tables[0].Rows[0]["Terms"]);
                        categoryDesc = Converter.ToString(ds.Tables[0].Rows[0]["Category"]);                         

                        userExists = true;
                    }
                    catch
                    {
                        userExists = false;
                    }                    
                }
            }
            finally
            {
                CloseConnection();
            }
            return userExists;
        }

推荐答案

你好,



似乎 terms = Convert.ToBoolean(ds.Tables [0] .Rows [0] [Terms]); 抛出异常。



您将char值转换为boolean。只是抛出InvalidCastException,这就是你每次都得到条款为假的原因。


你可以检查从数据库中检索的值,然后设置条款的值。



希望它有所帮助。



问候。
Hi there,

it seems that terms = Convert.ToBoolean(ds.Tables[0].Rows[0]["Terms"]); throwing an Exception.

you are converting a char value into boolean. that simply throws InvalidCastException and that is the reason why you are getting terms as false every time.

you can check the value retrieved from database and then set the value for terms.

Hope it helps.

Regards.


这篇关于out bool参数总是假的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆