mercredi 6 mai 2015

How to use a checkbox to change values in an SQL database in asp.net

I currently have system which has a web front end and a back office system. User can book properties online or call our office to book a property. In the admin system on the web front, I have two check boxes to determine if the property is available on the front end or the back office system. This is controlled using a Check box. The code for the check box is as follows;

<asp:CheckBox ID="CheckBoxAvailableToWeb" runat="server" TextAlign="Left" Text="Available for web bookings"
                                    Checked="true" />

I have an field in an SQL Database called "isAvailableToWeb" which has a Boolean result. What I want to achieve is if the check box is checked, the value of the "isAvailableToWeb" field is set to "True" or set to "False" if un-checked.

I have tried to complete this function using the following code;

 Protected Sub CheckBoxAvailableToWeb_CheckedChanged(sender As Object, e As EventArgs, ByVal beachhutid As Long)
    Using dbContext = New bbhasDBEntities
        Dim item
        item = (From i In dbContext.tblBeachHuts Where i.beachHutId = beachhutid Select i).First()
        If CheckBoxAvailableToWeb.Checked = True Then

            item.AvailableToWeb = True
        Else
            item.AvailableToWeb = False
        End If

        dbContext.tblBeachHuts.Attach(item)
        Call dbContext.SaveChanges()
    End Using
End Sub

This code doesn't throw up any errors but doesn't also make the change that I would like to see.

I have a button on this page that saves the information so I would also like to know if it would be better, once the code is working, to put it in that Sub.

Aucun commentaire:

Enregistrer un commentaire