Results 1 to 5 of 5

Thread: How to load state of checkboxlist in database asp.net?

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2013
    Posts
    3

    How to load state of checkboxlist in database asp.net?

    I got 2 checkboxlists , each checkbox has 6 checkboxes inside it (below is my database)

    checkboxlist1 : id , rowa

    checkboxlist2:id , rowb

    id : int
    rowa : bit
    rowb : bit
    When I click one checkbox, the value is saved to my database as TRUE ,But somehow, when I load the page, it does not show the checked state (checked/unchecked). Here is my code . i think my problem is , how to load checked value (bit) of checkboxlist ?

    is there anyone else can help me out or give me another solution ?

    . Here is my code:



    <asp:Panel ID="Panel1" runat="server">
    <asp:CheckBoxList ID="CheckBoxList1" runat="server" style="margin-left: 258px">
    <asp:ListItem Text="1"></asp:ListItem>
    <asp:ListItem Text="2"></asp:ListItem>
    <asp:ListItem Text="3"></asp:ListItem>
    <asp:ListItem Text="4"></asp:ListItem>
    </asp:CheckBoxList>
    <br />
    <br />

    <asp:CheckBoxList ID="CheckBoxList2" runat="server"
    style="margin-left: 255px">
    <asp:ListItem Text="5"></asp:ListItem>
    <asp:ListItem Text="6"></asp:ListItem>
    <asp:ListItem Text="7"></asp:ListItem>
    <asp:ListItem Text="8"></asp:ListItem>
    </asp:CheckBoxList>
    </br>
    <asp:Button ID="Button1" runat="server" Text="Button" />
    <br />
    <br />
    <br />
    <br />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
    SelectCommand="SELECT [rowa],[rowb] FROM [tbtest]"></asp:SqlDataSource>
    </asp:Panel>

    </form>


    Button1_click

    Dim connectionString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
    Dim insertSql As String = "INSERT INTO tbtest(rowa,rowb) VALUES(@rowa,@rowb)"

    Using myConnection As New SqlConnection(connectionString)
    myConnection.Open()
    Dim myCommand As New SqlCommand(insertSql, myConnection)
    myCommand.Parameters.AddWithValue("@rowa", SqlDbType.Bit).Value = 1
    myCommand.Parameters.AddWithValue("@rowb", SqlDbType.Bit).Value = 1
    myCommand.ExecuteNonQuery() myConnection.Close()
    End Using


    Page_load

    Dim connectionString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
    Using myConnection As New SqlConnection(connectionString)
    Dim objCmd_team As SqlCommand = New SqlCommand("SELECT [rowa],[rowb] FROM [tbtest]", myConnection)
    myConnection.Open()
    Dim objReader As SqlDataReader = objCmd_team.ExecuteReader()

    While (objReader.Read())
    Dim currentCheckBox As ListItem = checkboxlist1.Items.FindByText(objReader("rowa"))

    If currentCheckBox IsNot Nothing Then
    currentCheckBox.Selected = True
    End If
    Dim currentCheckBox1 As ListItem = checkboxlist2.Items.FindByText(objReader("rowb"))
    If currentCheckBox1 IsNot Nothing Then
    currentCheckBox1.Selected = True
    End If
    End While
    End Using

  2. #2
    Fanatic Member karthikeyan's Avatar
    Join Date
    Oct 2005
    Location
    inside .net
    Posts
    919

    Re: How to load state of checkboxlist in database asp.net?

    Here is the way to achieve

    Code:
     CheckBoxList1.Items.FindByValue(yourvalue).Selected = true;
    Hope this helps.
    Loving dotnet

  3. #3
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: How to load state of checkboxlist in database asp.net?

    So, rowa and rowb are of bit type and you are trying to find an item by text or value or whatever?
    Is that the case?
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2013
    Posts
    3

    Re: How to load state of checkboxlist in database asp.net?

    yes , rowa and rowb are bit type , all i want to load the state of checkboxlist

  5. #5
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: How to load state of checkboxlist in database asp.net?

    What value will this return?
    "(objReader("rowa")"
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width