Solved

Populate VB.net Checkbox list from SQL table

Posted on 2013-06-04
10
1,525 Views
Last Modified: 2013-06-04
I have this SQL query:

SELECT PAYRCORD, DSCRIPTN FROM UPR40600 ORDER BY PAYRCORD

On my user form I want a checkbox list that will show:

Checkbox      PAYRCORD       DSCRIPTN

What is the code necessary to populate the checkbox list like this from the SQL table in the query?
0
Comment
Question by:thoecherl
[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
  • 5
10 Comments
 
LVL 15

Expert Comment

by:ChloesDad
ID: 39220348
Is this windows forms or web as the approach is different as a checkboxlist is a web control, there is not a windows forms equivalent so we have to create our own user control.
0
 

Author Comment

by:thoecherl
ID: 39220380
Windows form
0
 
LVL 15

Expert Comment

by:ChloesDad
ID: 39220393
OK, well as I said the checkboxlist is only for Web,

You can either use a set of standard check boxes that are added dynamically to a frame with the text of the combo box being made up from your data, or you can create a new user control that has a check box and a text box to allow you to nicely format the data into two apparent columns.

I'm presuming that you know how to create the query and loop through the records.

Which way do you want to go?
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:thoecherl
ID: 39220409
I'm a bit confused.  I'm using Visual Studio 2010 and I created the project as a vb.net Windows Form project.  CheckedListBox is one of the available forms, which I have dragged onto my form.

My preference is your second scenario, though I need three columns, one for the check box and one for each of the fields in the SQL query - PAYRCORD and DSCRIPTN.  I can make the SQL connection and execute the query, but do I use a datareader to loop through the results of the query or table fill, or what?
0
 

Author Comment

by:thoecherl
ID: 39220536
I've requested that this question be closed as follows:

Accepted answer: 0 points for thoecherl's comment #a39220409

for the following reason:

I got it to work using this:

        Dim str As String = "Data Source = T-PC;Initial Catalog = TWO;Persist Security Info= True;User ID=sa;Password=123!"
        Dim con As New SqlConnection(str)
        Dim cmd As String = "SELECT RTRIM(PAYRCORD) AS PAYCODE, RTRIM(DSCRIPTN) AS DESCRIPTION FROM UPR40600 ORDER BY PAYRCORD"
        Dim adpt As New SqlDataAdapter(cmd, con)
        Dim myDataSet As New DataSet()
        adpt.Fill(myDataSet, "UPR40600")
        Dim myDataTable As DataTable = myDataSet.Tables(0)
        Dim tempRow As DataRow
        For Each tempRow In myDataTable.Rows
            'ListBox1.Items.Add((tempRow("ControlNo") & " (" & tempRow("EmpNo") & ")" & " (" & tempRow("CheckOutDate") & ")" & " (" & tempRow("CheckOutTime") & ")" & " (" & tempRow("TaxiNo") & ")" & " (" & tempRow("PlateNo") & ")" & " (" & tempRow("Model") & ")" & " (" & tempRow("Make") & ")"))
            'ListBox1.Items.Add((tempRow("ControlNo") & " (" & tempRow("EmpNo") & ")"))
            chkPayCodes.Items.Add(tempRow("PAYCODE") & "            " & tempRow("DESCRIPTION"))
        Next

Thanks
0
 
LVL 15

Expert Comment

by:ChloesDad
ID: 39220438
I was going to use the text part of the check box for one column, then a text box for the other.

I see the confusion now, There are two controls, checkedlistbox (WF) and CheckBoxList (Web)

I misread your original post and searched for the wrong one.....

Can you post your code for executing the query and then I can tell you what to do next
0
 

Author Comment

by:thoecherl
ID: 39220443
I accepted my comment as the solution, rather than the contributor's solution, by mistake.  I would like to fix that
0
 
LVL 15

Expert Comment

by:ChloesDad
ID: 39220444
Glad you got it working and again sorry for the confusion over the control names
0
 
LVL 15

Accepted Solution

by:
ChloesDad earned 500 total points
ID: 39220460
In this case, I see no problem in doing what you did. I didn't actually give you a solution, although I may have helped you in finding it yourself
0
 

Author Closing Comment

by:thoecherl
ID: 39220537
You sent me in the right direction.  Thanks
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

627 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