tags:

views:

288

answers:

2

For my ASP .NET webpage I am using a MySQL database with a TINYINT field to indicate a boolean (0/1) value.

Thanks to stackoverflow I was able to read the field value into a Listview as a asp:Checkbox.

 <asp:CheckBox ID="freight_foundCheckbox" runat="server"
     Checked='<%# Convert.ToBoolean(Eval("freight_found")) %>'  />

What is challenging me now is how to reverse the transaction in the InsertItemTemplate or EditItemTemplate.

The listview textbox reads as:

    <asp:TextBox ID="freight_foundTextBox" runat="server" 
       Text='<%# Bind("freight_found") %>' />

How do I bind a asp:Checkbox value back into the database as a integer value?

+1  A: 

This web site link text was tremendously helpful in resolving this issue.

The key is to capture the value and convert it into a MySQL friendly format before writing to the database.

So for a insert set a ItemInserting event with this code:

CheckBox freightFound = (CheckBox)ListView2.InsertItem.FindControl("freight_foundCheckbox");
        if (freightFound.Checked == true)
        {               
            //true
            e.Values["freight_found"] = 1;
        }
        else
        {
            //false
            e.Values["freight_found"] = 0;
        }

And then for a edit set a ItemUpdating event.

CheckBox freightFound = (CheckBox)ListView2.EditItem.FindControl("freight_foundCheckbox");

        if (freightFound.Checked == true)
        {
            //true               
            e.NewValues["freight_found"] = 1;
        }
        else
        {
            //false
            e.NewValues["freight_found"] = 0;
        }
John M
A: 

A little late here, but why not just bind it directly to the checkbox instead of a text field? No code behind handling needed that way. See below. MySQL tinyint(1) converts directly to .NET boolean and back when using the MySQL Connector/.NET.

<asp:CheckBox ID="freight_foundCheckbox" runat="server"
    Checked='<%# Bind("freight_found") %>'  />
Kasey Speakman