1

hi guys am trying to insert a boolean value using a Textbox, now i get an error that says "String was not recognized as a valid Boolean." because i added 0 in Textbox.

Below is a method in my business layer to insert the values

public void ADD_CUSTOMER(string Customer_Code, string Customer_Nmae, string Address, string Country, string City, string Phone, string Fax, string Mobile,
        string Email, string Note, byte[] Cust_Imaige, decimal Credit, decimal Debit, decimal Balance, bool Statas, string criterion)
    {
        DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
        DAL.open();
        SqlParameter[] param = new SqlParameter[16];
        param[0] = new SqlParameter("@Customer_Code", SqlDbType.NVarChar, 25);
        param[0].Value = Customer_Code;

        param[1] = new SqlParameter("@Customer_Nmae", SqlDbType.NVarChar, 100);
        param[1].Value = Customer_Nmae;

        param[2] = new SqlParameter("@Address", SqlDbType.NVarChar, 100);
        param[2].Value = Address;

        param[3] = new SqlParameter("@Country", SqlDbType.NVarChar, 100);
        param[3].Value = Country;

        param[4] = new SqlParameter("@City", SqlDbType.NVarChar, 100);
        param[4].Value = City;

        param[5] = new SqlParameter("@Phone", SqlDbType.NVarChar, 100);
        param[5].Value = Phone;

        param[6] = new SqlParameter("@Fax", SqlDbType.NVarChar, 100);
        param[6].Value = Fax;

        param[7] = new SqlParameter("@Mobile", SqlDbType.NVarChar, 100);
        param[7].Value = Mobile;

        param[8] = new SqlParameter("@Email", SqlDbType.NVarChar, 100);
        param[8].Value = Email; 

        param[9] = new SqlParameter("@Note", SqlDbType.NVarChar, 100);
        param[9].Value = Note;

        param[10] = new SqlParameter("@Cust_Imaige", SqlDbType.Image);
        param[10].Value = Cust_Imaige;

        param[11] = new SqlParameter("@Credit", SqlDbType.Decimal);
        param[11].Value = Credit;

        param[12] = new SqlParameter("@Debit", SqlDbType.Decimal);
        param[12].Value = Debit;

        param[13] = new SqlParameter("@Balance", SqlDbType.Decimal);
        param[13].Value = Balance;

        param[14] = new SqlParameter("@Statas", SqlDbType.Bit);
        param[14].Value = Statas;

        param[15] = new SqlParameter("@criterion", SqlDbType.NVarChar, 50);
        param[15].Value = criterion;

        DAL.ExecuteCommand("ADD_CUSTOMER", param);
        DAL.close();

    }

Now below is my client Form

 private void btn_Add_Click(object sender, EventArgs e)
    {
       try
        {
            byte[] Cust_Imaige;
            if (pictureBox1.Image == null)
            {
                Cust_Imaige = new byte[0];

                Customer.ADD_CUSTOMER(txt_Customer_Code.Text, txt_Customer_Nmae.Text, txt_Address.Text,
                    txt_Country.Text, txt_City.Text,txt_Phone.Text,txt_Fax.Text,txt_Mobail.Text,
                   txt_Email.Text, txt_Note.Text, Cust_Imaige,Convert.ToDecimal(txt_Credit.Text),
                   Convert.ToDecimal(txt_Debit.Text),Convert.ToDecimal(txt_Balance.Text),Convert.ToBoolean(txt_Statas.Text), "without_Image");
                MessageBox.Show("added","add",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            else
            {
                MemoryStream ms = new MemoryStream();
                pictureBox2.Image.Save(ms, pictureBox2.Image.RawFormat);
                Cust_Imaige = ms.ToArray();
                Customer.ADD_CUSTOMER(txt_Customer_Code.Text, txt_Customer_Nmae.Text, txt_Address.Text,
                    txt_Country.Text, txt_City.Text, txt_Phone.Text, txt_Fax.Text, txt_Mobail.Text,
                   txt_Email.Text, txt_Note.Text, Cust_Imaige, Convert.ToDecimal(txt_Credit.Text),
                   Convert.ToDecimal(txt_Debit.Text), Convert.ToDecimal(txt_Balance.Text), Convert.ToBoolean(txt_Statas.Text), "with_Image");
                  MessageBox.Show("add","",MessageBoxButtons.OK,MessageBoxIcon.Information);

            }
        }


        catch
        {
            return;
        }
        finally
        {
            btn_Add.Enabled = false;
            btn_New.Enabled = true;
        }


    }
0

4 Answers 4

3

Convert.ToBoolean(txt_Statas.Text) won't work with 1 and 0

you should use a Checkbox and return Checkbox.Checked

or if you want to keep the TextBox then

replace Convert.ToBoolean(txt_Statas.Text) with txt_Statas.Text == "1" ? true : false

Sign up to request clarification or add additional context in comments.

Comments

0

I guess a checkbox would be a better fit for that because the value of the checkbox is a boolean.

Anyway, if you want to stay with your TextBox do the following.

txt_Statas.Text == "1"

Your changed code.

Customer.ADD_CUSTOMER(txt_Customer_Code.Text, txt_Customer_Nmae.Text, txt_Address.Text,
                    txt_Country.Text, txt_City.Text, txt_Phone.Text, txt_Fax.Text, txt_Mobail.Text,
                   txt_Email.Text, txt_Note.Text, Cust_Imaige, Convert.ToDecimal(txt_Credit.Text),
                   Convert.ToDecimal(txt_Debit.Text), Convert.ToDecimal(txt_Balance.Text), txt_Statas.Text == "1", "with_Image");

Comments

0

When converting to boolean via

Convert.ToBoolean(txt_Statas.Text) 

the routine expects the argument in "True" or "False" form, not in "0" or "1":

https://msdn.microsoft.com/en-us/library/86hw82a3(v=vs.110).aspx

in your case just put

txt_Statas.Text == "1"

instead of Convert.ToBoolean(txt_Statas.Text).

Comments

0

Create a common utility class with Extension Methods:

internal static class ConvertExtension {
    public static bool ToBoolean(this object value) {
        if (value == null or DBNull.Value.Equals(value)) return false;
        if (value is bool) return (bool)value;
        string obj =  value.ToString(CultureInfo.InvariantCulture);
        return !(string.IsNullOrEmpty(obj) 
            || string.Equals(obj, "0") 
            || string.Equals("false", obj, StringComparison.OrdinalIgnoreCase));
    }
}

usage

modify Convert.ToBoolean(txt_Statas.Text) to txt_Statas.Text.ToBoolean()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.