Link to home
Start Free TrialLog in
Avatar of CochiseCounty
CochiseCountyFlag for United States of America

asked on

set checked = true for checkboxlist

I have this checkboxlist from a database table with 3 fields ID, Assignee, Status
cmdSQL = New SqlCommand("sp_RetrieveAssigneeBySuspense", cnnST)
        cmdSQL.CommandType = CommandType.StoredProcedure
        cmdSQL.Parameters.Add("@SuspenseID", Session("ID"))
        Reader = cmdSQL.ExecuteReader()
        chkStatus.DataSource = Reader
        chkStatus.DataTextField = Reader("Assignee")
        chkStatus.DataValueField = Reader("ID")
        chkStatus.DataBind()

If I want to do something like if the field 'status' in my database is 'completed' then that checkbox should be checked as default. How do I do that? Thanks for help
Avatar of Raju Srivatsavaye
Raju Srivatsavaye
Flag of United States of America image

does your query return the status field..if it does do something like this..

chkStatus.DataTextField = Reader("Assignee")
        chkStatus.DataValueField = Reader("ID")

if Reader("Status")="completed" then
 chkStatus.checked=true
end if
        chkStatus.DataBind()
Avatar of CochiseCounty

ASKER

I did that but the chkStatus listbox doesn't show up in my page
ASKER CERTIFIED SOLUTION
Avatar of RJeyaPrakash
RJeyaPrakash

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vinodhsomasekharan
vinodhsomasekharan

Hai,

Instead of DataReader just try dataset

Dim myAdapter As New SqlClient.SqlDataAdapter
Dim myDataSet As New DataSet

       myAdapter.Fill(myDataSet, "myTable")
        chkStatus.DataSource = myDataSet.Tables(0)
        chkStatus.DataTextField = "Assignee"
        chkStatus.DataValueField = "id"
        chkStatus.DataBind()

'Loop through the dataset and check whether staus is completed if so just set  selected property of the the corresponding ' checkbox in the list to True
        For itemCounter As Integer = 0 To myDataSet.Tables(0).Rows.Count - 1
            With myDataSet.Tables(0).Rows(itemCounter)
                If .Item("Status").ToString.ToLower = "completed" Then chkStatus.Items(itemCounter).Selected = True
            End With
        Next

It works fine for me..

hope this will help u

Regards
Vinu