1

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; } } 
1
  • 2
    Checkboxes have a Property called Checked. this is already a boolean and returns the checked state of the checkbox Commented Oct 6, 2021 at 14:06

2 Answers 2

1

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) .

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

1 Comment

Thanks a lot, I understand it now. Great and simple explanation!
0

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.

1 Comment

Thanks, yes it is supposed to be a checkbox. But it still do not want to pass the FootPlate, WirstCord or Excluded value

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.