简体   繁体   中英

vb.net insert checkbox value into database

i am new vb.net beginner. i have some question about vb.net insert checkbox value into database. I finished the frontend and backend coding and i want to know any easy way to deploy the checkbox value insert into database. thanks!

 Frontend <td> <asp:CheckBox runat="server" ID="checkbox" AutoPostBack="true" CssClass="uamCheckBox" Width="10px" /> </td>

 BackEnd Dim ckbox As String = If(checkbox.Checked, "Y", "N") Using checkbox As New SqlCommand("INSERT INTO table (checkbox) VALUES(@checkbox)") checkbox.Parameters.AddWithValue("@checkbox", ckbox) End Using

I assume you table is not named table. You will substitute the name of your table for the word table in you sql string. Also I assume the name of the column is checkbox.

I would suggest more descriptive names for your controls.

I renamed the command because the code was confusing the control and the command name.

If this really is stored as a String type then

Private Sub InsertDatabase()
    Using cn As New SqlConnection(ConStr),
        chbox As New SqlCommand("INSERT INTO table (checkbox) VALUES(@checkbox)", cn)
        chbox.Parameters.Add("@checkbox", SqlDbType.VarChar).Value = If(checkbox.Checked, "Y", "N")
        cn.Open()
        chbox.ExecuteNonQuery()
    End Using
End Sub

If it is a bit, as it should be then

        chbox.Parameters.Add("@checkbox", SqlDbType.Bit).Value = checkbox.Checked

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM