Home > Software engineering >  How do I convert Booleans correctly?
How do I convert Booleans correctly?

Time:10-07

I am working on a project for user administration in ASP.NET Web App (Framework 4.8) and I thought my gridview would be more effective if I turned some columns that before was TextBoxes to CheckBoxes.

Now that I am going to it gives me: "String was not recognized as a valid Boolean." when I have converted the items. So what am I doing wrong converting these Booleans?

gvTestUsers_RowCommand

protected void gvTestUsers_RowCommand(object sender, GridViewCommandEventArgs e)
{
    try
    {
        if (e.CommandName.Equals("AddNew"))
        {
            int tbl_users_key = 0;

            TextBox myTxt;
            CheckBox myCheck;

            myTxt = ((TextBox)gvTestUsers.FooterRow.FindControl("txtNamefooter"));
            string Name;
            Name = myTxt.Text;

            string CostCenter;
            myTxt = ((TextBox)gvTestUsers.FooterRow.FindControl("txtCostCenterfooter"));
            CostCenter = myTxt.Text;

            string Employee;
            myTxt = ((TextBox)gvTestUsers.FooterRow.FindControl("txtEmployeefooter"));
            Employee = myTxt.Text;

            Boolean FootPlate;
            myCheck = ((CheckBox)gvTestUsers.FooterRow.FindControl("CheckFootPlateFooter"));
            FootPlate = Convert.ToBoolean(myCheck.Text);

            Boolean WirstCord;
            myCheck = ((CheckBox)gvTestUsers.FooterRow.FindControl("CheckWirstCordFooter"));
            WirstCord = Convert.ToBoolean(myCheck.Text);

            Boolean Excluded;
            myCheck = ((CheckBox)gvTestUsers.FooterRow.FindControl("txtExcludedfooter"));
            Excluded = Convert.ToBoolean(myCheck.Text);

            string Comment;
            myTxt = ((TextBox)gvTestUsers.FooterRow.FindControl("txtCommentfooter"));
            Comment = myTxt.Text;


            string sMsg;

            sMsg = "";


            bool btest = fcEditTBL_USERS(tbl_users_key, Name, CostCenter, Employee, FootPlate, WirstCord, Excluded, Comment, ref sMsg, 0);


            if (btest == false)
            {
                lblErrorMessage.Text = sMsg;
                lblSuccessMessage.Text = " ";
                return;
            }

            PopulateGridView();

            lblSuccessMessage.Text = Name   " Was Added To Record.";
        }
    }
    catch (Exception ex)
    {
        lblSuccessMessage.Text = "";
        lblErrorMessage.Text = ex.Message;
    }
}

CodePudding user response:

As per your code following code is wrong.

Boolean Excluded;
                    myCheck = ((CheckBox)gvTestUsers.FooterRow.FindControl("txtExcludedfooter"));
                    Excluded = Convert.ToBoolean(myCheck.Text); 

As per your id txtExcludedfooter, is this textbox value or the checkbox?

Need to correct this.

CodePudding user response:

This:

Boolean FootPlate;
myCheck = ((CheckBox)gvTestUsers.FooterRow.FindControl("CheckFootPlateFooter"));
FootPlate = Convert.ToBoolean(myCheck.Text);

should be:

Boolean FootPlate;
myCheck = ((CheckBox)gvTestUsers.FooterRow.FindControl("CheckFootPlateFooter"));
FootPlate = myCheck.Checked;

You don't need to convert, (.Text is the text associated with the check box, not the true/false value if the box is checked or not) .

  • Related