locked
Insert and retrive Checkbox List value in VB.NET RRS feed

  • Question

  • User-1578974752 posted

    How to insert 4 selected values of checkbox list in to database through insert command in vb.net.Type is the field in database and 5 values of checkbox1  is selected.checbox1.
    how to retrieve the saved values in to the chekbox lists upon clicking view

      cmd.CommandText = "Insert into rep_hdr(id,type) values('" + dissue.Text + "',' " + checkbox1.selectedvalue + "')"
                cmd.ExecuteNonQuery()

    Monday, November 5, 2018 7:48 AM

Answers

  • User61956409 posted

    Hi shsu,

    How to insert 4 selected values of checkbox list in to database through insert command in vb.net.Type is the field in database and 5 values of checkbox1  is selected.checbox1.

    As you mentioned, multi items could be selected by user, to insert all selected values in one filed "type" into database, you can try to add delimiter (such as ",") between selected values before you insert it into your database.

    Dim selectedtypes As String = ""
    For Each item As ListItem In CheckBoxList1.Items
        If item.Selected Then
            selectedtypes += item.Value + ","
        End If
    Next
    
    Dim query As String = "Insert into rep_hdr(id,type) values('" + dissue.Text + "',' " + selectedtypes.TrimEnd(",") + "')"
    
    'your code logic here

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 6, 2018 3:24 AM

All replies

  • User61956409 posted

    Hi shsu,

    How to insert 4 selected values of checkbox list in to database through insert command in vb.net.Type is the field in database and 5 values of checkbox1  is selected.checbox1.

    As you mentioned, multi items could be selected by user, to insert all selected values in one filed "type" into database, you can try to add delimiter (such as ",") between selected values before you insert it into your database.

    Dim selectedtypes As String = ""
    For Each item As ListItem In CheckBoxList1.Items
        If item.Selected Then
            selectedtypes += item.Value + ","
        End If
    Next
    
    Dim query As String = "Insert into rep_hdr(id,type) values('" + dissue.Text + "',' " + selectedtypes.TrimEnd(",") + "')"
    
    'your code logic here

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 6, 2018 3:24 AM
  • User-1578974752 posted

    Thanks Fei Han.

    The values are saving to Database. If I click retrieve button, is it possible to show these saved values in a checkbox list .

    Wednesday, November 7, 2018 2:53 AM
  • User1120430333 posted

    Thanks Fei Han.

    Is it possible as below :

    The saved values of checkbox list will show in checkbox list When I click retrieve Button.

    The database table column should be a Bit primitive column type where false = 0 and true = 1 or no = not checked = 0 and checked  yes = 1.

    https://docs.microsoft.com/en-us/sql/t-sql/data-types/bit-transact-sql?view=sql-server-2017

    Wednesday, November 7, 2018 5:25 AM