ASP.NET with MySQL - use TINYINT with asp: Checkbox

For my ASP.NET web page, I am using a MySQL database with a TINYINT field to specify a boolean (0/1) value.

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

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

      

What is challenging me now is how to undo a transaction in an InsertItemTemplate or EditItemTemplate .

The listview textbox reads like:

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

      

How do I bind the asp: Checkbox value back to the database as an integer value?

+2


source to share


3 answers


This website link text has been extremely helpful in solving this problem.

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

So to insert, set the 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 set the ItemUpdating event to edit .

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;
        }        

      

+2


source


I know this is really old, but I just spent a couple of hours trying to figure it out.

in UPDATE or INSERT statement you can use this

IF (? = 'True', 1,0)



there is no need to change the bound checkbox to the element template or anything else.

I'm not sure if that matters, but I changed my parameter type to Boolean, and since I now have a job I don't want to change it.

+1


source


A bit late, but why not just bind it directly to the checkbox instead of the textbox? No processing code was like that. See below. MySQL tinyint (1) converts directly to .NET booleans and vice versa when using the MySQL Connector / .NET.

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

      

0


source







All Articles