Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Databinding a Checkbox List

Posted on 2010-08-28
9
Medium Priority
?
651 Views
Last Modified: 2012-05-10
Hi all.

I have an edit form that contains a checkbox list, then has an "other" box below it so users can type in an answer if none of the checkboxes apply. I render the list, and then I need to check the boxes that were in the database. Then I need to fill the "other" box if needed. I can't seem to figure out 2 things. First, I can't change the textbox value in the checkboxlist databound sub. That's the hard part. The other part that baffles me is that my code to check the boxes feels very sloppy. If I try to use something like:

cboCategories.Items.FindByValue(dbReader("category")).Selected = True

I get a nasty object error. Any ideas?

Here is my code for the databound sub:

 
Protected Sub cboCategories_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboCategories.DataBound

        If IsNumeric(hfCommunicationID.Value) Then
            Dim cboCategoryListItem As ListItem
            Dim foundAMatch As Boolean = False

            '--- load categories
            dbConnection.SPName("sp_Communications")
            dbConnection.SPParameter("Action", "SELECTCATEGORIES")
            dbConnection.SPParameter("communicationID", hfCommunicationID.Value)

            Dim dbReader As SqlDataReader
            dbReader = dbConnection.SPExecuteDataReader
            While dbReader.Read
                For Each cboCategoryListItem In cboCategories.Items
                    If cboCategoryListItem.Value = dbReader("category") Then
                        cboCategoryListItem.Selected = True
                        foundAMatch = True
                    End If
                Next
                If Not foundAMatch Then
                    txtOtherCategory.Text = dbReader("category").ToString
                End If
                foundAMatch = False
            End While
            dbConnection.SPCloseDataReader()
        End If

    End Sub

Open in new window

0
Comment
Question by:DEWebWorks
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 31

Expert Comment

by:MlandaT
ID: 33552442
1. The problemn with the check is what happens if the value is not found? You may need to implement this as two lines:

cboCategories.Items.FindByValue(dbReader("category")).Selected = True

should perhaps be:

If not isdbnumm(dbReader("category")) then

      dim chk as checkbox = cboCategories.Items.FindByValue(dbReader("category"))
.Selected = True
0
 
LVL 31

Expert Comment

by:MlandaT
ID: 33552463
1. The problemn with the check is what happens if the value is not found? You may need to implement this as two lines:

cboCategories.Items.FindByValue(dbReader("category")).Selected = True

should perhaps be:

If not isdbnumm(dbReader("category")) then

      dim chk as ListItem = cboCategories.Items.FindByValue(dbReader("category"))
      if chk isnot nothing then chk.Selected = True

End if

2. To implement the code for the "Other" and changing the textbox value in the databound... The problem is probably that you are looping through the datareader adn the value that get's set in the textbox is largely dependant on the dbReader("category") for the very last row in your datareader. I'm not sure about your use of loops there. Doesnt look right.
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 33552600
shouldn't this:

If not isdbnumm(dbReader("category")) then

be

If not isdbnull(dbReader("category")) then

?

AW
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 31

Expert Comment

by:MlandaT
ID: 33552621
Arthur_Wood:

Typo...

But yes indeed you are right. Thanks. It is just meant to be a check to make sure that we dont try to access the value if it is null.
0
 

Author Comment

by:DEWebWorks
ID: 33552938
Sorry, I didn't outline the structure very well. The categories come from a lookup table - no chance of a null.

MlandaT, your solution to problem 1 worked out perfectly! Now I'm just down to why the "other" box won't fill. The basic structure is this. When someone saves the parent record (tblCommunications), first all records are deleted in the tblCommunications_Categories for that ID (it's a wipe and replace scenario). Then I loop through all checkboxes to save the checked ones. Then if something is in the "Other Category" textbox, I save that. So, upon editing, the dbReader can only have the checked boxes and the value of the extra text box.

This "should" not have anything to do with the order that things are rendered, right?

My code now looks like this:

While dbReader.Read
                cboCategoryListItem = cboCategories.Items.FindByValue(dbReader("category"))
                If cboCategoryListItem IsNot Nothing Then
                    cboCategoryListItem.Selected = True
                    cboCategoryListItem = Nothing
                Else
                    txtOtherCategory.Text = dbReader("category").ToString
                End If
            End While

Open in new window

0
 
LVL 31

Expert Comment

by:MlandaT
ID: 33556238
Let's see the code for saving
0
 

Author Comment

by:DEWebWorks
ID: 33558135
I've confirmed the data exists in the database.


'--- save categories
        Dim cboCategoryListItem As ListItem
        For Each cboCategoryListItem In cboCategories.Items
            If cboCategoryListItem.Selected Then
                dbConnection.SPName("sp_Communications")
                dbConnection.SPParameter("Action", "INSERTCATEGORIES")
                dbConnection.SPParameter("communicationID", newCommunicationID)
                dbConnection.SPParameter("category", cboCategoryListItem.Text)
                dbConnection.SPExecuteNonQuery()

                dbConnection.SPParameterClear()
            End If

        Next
        '--- pick up "other" category
        If txtOtherCategory.Text <> "" Then
            dbConnection.SPName("sp_Communications")
            dbConnection.SPParameter("Action", "INSERTCATEGORIES")
            dbConnection.SPParameter("communicationID", newCommunicationID)
            dbConnection.SPParameter("category", txtOtherCategory.Text)
            dbConnection.SPExecuteNonQuery()

            dbConnection.SPParameterClear()

        End If

Open in new window

0
 
LVL 31

Accepted Solution

by:
MlandaT earned 2000 total points
ID: 33558314
Looks to me like all your code should work. However, since we are inserting the 'Other' value into the same table as the rest of the categories... when the combobox binds again the next time... there is nothin there to distinguish the 'Other' value from the rest of them.
0
 

Author Closing Comment

by:DEWebWorks
ID: 33558565
Ok, so my partner dug through my 600 lines of code on the page and found where I was resetting the textbox value to "".

Thank you SOOOO much for your help :)
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Large Outlook files lead to various unwanted errors and corruption issues. Furthermore, large outlook files can also make Outlook take longer to start-up, search, navigate, and shut-down. So, In this article, i will discuss a method to make your Out…
This article will help to fix the below errors for MS Exchange Server 2013 I. Certificate error "name on the security certificate is invalid or does not match the name of the site" II. Out of Office not working III. Make Internal URLs and Externa…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question