?
Solved

String was not recognized as a valid Boolean.

Posted on 2010-04-08
16
Medium Priority
?
1,269 Views
Last Modified: 2012-05-09
Hi Guys,

I am getting the error String was not recognized as a valid Boolean. Currently what I am trying to do is save the user's preference on a profile.

This will allow me to run a query against the database and match the results to that of the person's preferences on the profile? I have attached my Web.Config file, aspx and aspx.cs files. Please help!!!!!
Web Config File

 <customErrors mode="Off"></customErrors>
    <profile enabled="true" defaultProvider="AspNetSqlProfileProvider">
      <properties>
        <add name="SmokeP" type="boolean"/>
        <add name="GenderP" type="boolean"/>
        <add name="JourneyP" type="boolean"/>
      </properties>
    </profile>

ASPX File

<asp:Label ID="lblSmokeP" runat="server" Text="Select Smoking Preference" Font-Bold="true" Font-Size=8></asp:Label>
    <asp:RadioButtonList ID="rbSmokeP" runat="server" Font-size="8">
    <asp:ListItem>Smoker</asp:ListItem>
    <asp:ListItem>Non-Smoker</asp:ListItem>
    <asp:ListItem>Both</asp:ListItem>
    </asp:RadioButtonList>
    <br />
    <br />
    <asp:Label ID="lblGenderP" runat="server" Text="Select Gender Preference" Font-Bold="true" Font-Size=8></asp:Label>
    <asp:RadioButtonList ID="rbGenderP" runat="server" Font-size="8">
    <asp:ListItem>Male</asp:ListItem>
    <asp:ListItem>Female</asp:ListItem>
    <asp:ListItem>Both</asp:ListItem>
    </asp:RadioButtonList>
    <br />
    <br />
    <asp:Label ID="lblJourneyP" runat="server" Text="Select Journey Preference (Seeking a Lift/Offering a Lift/Both)" Font-Bold="true" Font-Size=8></asp:Label>
    <asp:RadioButtonList ID="rbJourneyP" runat="server" Font-size="8">
    <asp:ListItem>Seeking</asp:ListItem>
    <asp:ListItem>Offering</asp:ListItem>
    <asp:ListItem>Both</asp:ListItem>
    </asp:RadioButtonList>

ASPX.CS File

 protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection myConn = new SqlConnection();
        myConn.ConnectionString = "Data Source=XG7;Initial Catalog=Car_Share_XG;Integrated Security=True";
        myConn.Open();
        SqlCommand command = new SqlCommand();
        command.CommandText = "SELECT * FROM tbl_Member";
        command.CommandType = CommandType.Text;
        command.Connection = myConn;
        SqlDataAdapter da = new SqlDataAdapter(command);
        DataSet ds = new DataSet();
        da.Fill(ds, "tbl_Member");

        // Get a FileStream object
        StreamWriter xmlDoc = new StreamWriter(Server.MapPath("./Data2.xml"), false);
        // WriteXml method to write an XML document
        ds.WriteXml(xmlDoc);
        xmlDoc.Close();
        myConn.Close();

        try
        {
            bool SmokeP = Profile.SmokeP;
            if (SmokeP)
                lblSmokeP.Text += "  : Currently a Smoker";
            else
                lblSmokeP.Text += " : Currently a Non-Smoker";
        }
        catch
        {
            lblSmokeP.Text += " : Currently Both";
        }

        try
        {
            bool GenderP = Profile.GenderP;
            
        }
        catch
        {
            lblGenderP.Text += " Both";
        }

        try
        {
            bool JourneyP = Profile.JourneyP;
           
        }
        catch
        {
            lblJourneyP.Text += " Both";
        }
    }

protected void btnSearch_Click(object sender, EventArgs e)
    {
        SqlConnection myConn = new SqlConnection();
        myConn.ConnectionString = "Data Source=XG7;Initial Catalog=Car_Share_XG;Integrated Security=True";
        myConn.Open();
        SqlCommand command = new SqlCommand();
        command.CommandText = "SELECT Member_Address FROM tbl_Member";
        command.CommandType = CommandType.Text;
        command.Connection = myConn;
        SqlDataAdapter da = new SqlDataAdapter(command);
        DataSet ds = new DataSet();
        da.Fill(ds, "tbl_Member");

        // Get a FileStream object
        StreamWriter xmlDoc = new StreamWriter(Server.MapPath("./out1.xml"), false);
        // WriteXml method to write an XML document
        ds.WriteXml(xmlDoc);
        xmlDoc.Close();
        myConn.Close();

        XmlDocument sourceDocument = new XmlDocument();
        sourceDocument.Load(Server.MapPath("./out1.xml"));

        XmlDocument outputDocument = new XmlDocument();
        XmlElement markers = outputDocument.CreateElement("markers", "");
        outputDocument.AppendChild(markers);
        // use ChildNodes[1] if a <?xml version='1.0' encoding='utf-8'?> is in data2.xml, otherwise ChildNodes[0]
        foreach (XmlNode node in sourceDocument.ChildNodes[0].ChildNodes)
        {
            XmlElement child = outputDocument.CreateElement("marker", "");
            XmlAttribute att = outputDocument.CreateAttribute("address", "");
            att.Value = node.ChildNodes[0].InnerText;
            child.Attributes.Append(att);
            markers.AppendChild(child);
        }
        outputDocument.Save(Server.MapPath("./out2.xml"));
        Profile.SmokeP = Boolean.Parse(rbSmokeP.SelectedValue);
        Profile.GenderP = Boolean.Parse(rbGenderP.SelectedValue);
        Profile.JourneyP = Boolean.Parse(rbJourneyP.SelectedValue);
        Profile.Save();
        Response.Redirect("SearchPage2.aspx");
    }

Open in new window

0
Comment
Question by:xmalcolm
  • 7
  • 6
  • 2
  • +1
16 Comments
 
LVL 12

Expert Comment

by:lazyberezovsky
ID: 30108015
Try System.Boolean as property type
0
 

Author Comment

by:xmalcolm
ID: 30108278
where do I put that? on the top of the aspx page along with the other 'Using' commands?
0
 
LVL 12

Expert Comment

by:lazyberezovsky
ID: 30108411
Sorry, man. That won't help :(
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 

Author Comment

by:xmalcolm
ID: 30108453
:(( any other ideas?
0
 
LVL 12

Expert Comment

by:lazyberezovsky
ID: 30108546
Check
rbSmokeP.SelectedValue
rbGenderP.SelectedValue
rbJourneyP.SelectedValue

What type of this controls? What exactly you are trying to parse?
0
 

Author Comment

by:xmalcolm
ID: 30108929
Well they are radio buttons. For example - for SmokeP - user can only select Smoker or Non-Smoker or Both

These values will then be used so that the query returns results matching the preference selected by the user.

0
 
LVL 12

Expert Comment

by:lazyberezovsky
ID: 30109278
If they are asp:Radiobatton, then just use
Profile.SmokeP = RadioButton1.Checked;
0
 
LVL 12

Expert Comment

by:lazyberezovsky
ID: 30109403
In your case
Profile.SmokeP = rbSmokeP.Checked;
0
 

Author Comment

by:xmalcolm
ID: 30109700
I Tried that earlier but I get a red underline. Could my web config be wrong?

<add name="SmokeP" type="boolean"/>

Profile.SmokeP = rbSmokeP.Checked;

When I hover over 'Checked' it says System.Web.UI.WebControls.RadioButtonList does not contain a definition for 'Checked' .....
0
 
LVL 4

Expert Comment

by:dshrivallabh
ID: 30110004
Profile.SmokeP = rbSmokeP.Selected;
0
 
LVL 4

Expert Comment

by:dshrivallabh
ID: 30110281
Profile.SmokeP = (rbSmokeP.SelectedIndex = 1?True:False);
0
 
LVL 12

Accepted Solution

by:
lazyberezovsky earned 2000 total points
ID: 30110283
Ah, man so you have radiobutton list, not simple radiobuttons.
Don't know your page code, but here is how to use rb list:

Profile.SmokeP = rbSmokeP.SelectedValue == "listItemName";
or
Profile.SmokeP = rbSmokeP.SelectedIndex == 0;
0
 

Author Comment

by:xmalcolm
ID: 30110470
Nope doesnt seem to work. It doesnt even come up on the list as soon as you put the dot after rbSmokeP. There is SelectedValue but then it shows this error: error CS0029: Cannot implicitly convert type 'string' to 'bool'. I then do it this way: Profile.SmokeP = Boolean.Parse(rbSmokeP.SelectedValue);

But get the error String was not recognized as a valid Boolean.

Please note: My radio buttons are 'RadioButtonList' instead of individual radio buttons with ID for each radio button.
0
 
LVL 12

Assisted Solution

by:lazyberezovsky
lazyberezovsky earned 2000 total points
ID: 30110706
SelectedValue is the NAME of radio button list item. Not it's value
See my code above
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 30111011
I believe that what you are looking for is this:

Profile.SmokeP = (rbSmokeP.SelectedItem.Text = "Smoker");
0
 

Author Comment

by:xmalcolm
ID: 30112018
Thanks the LearnedOne but Lazyberezovsky's solution worked or atleast i think its working :)). I am not getting an error message and my radio button stays selected on the preference I selected.

So thanks Lazyberezovsky. But now that I have done that, could you give me any ideas how I can use the stored preference to return results through a query. Currently I have:

SELECT Member_Address FROM tbl_Member

But what I need is SELECT Member_Address FROM tbl_Member WHERE (Preference selected is same as member preferences in tbl_member)

Any ideas?

0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…
In this video I will demonstrate how to set up Nine, which I now consider the best alternative email app to Touchdown.

599 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question