Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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 ..?

C#
<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;
        }
Posted
Updated 18-May-15 18:47pm
v4
Comments
aarif moh shaikh 19-May-15 0:26am    
where you executing your query..
nebiam 19-May-15 0:32am    
executing elsewhere.. but stepping through this code only returns false for the 'terms' no matter what that columns value is..
StM0n 19-May-15 0:27am    
Could <<ds.Tables[0].Rows[0]["Terms"]>> be false? Have you checked the datasets in your DB?
nebiam 19-May-15 0:33am    
yeah i have checked and used multiple entries for testing both false and true..in saying that im using db2.. so have used character 'T' and 'F' ... but this has never been an issue before.. i was more wondering if its specifically due to the out bool parameter as i have never used it like this before.
StM0n 19-May-15 0:36am    
Does your method throws an exception? Especially near the <<terms=...>>? You catch an possible exception w/o rethrow it again.

1 solution

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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900