DOUBLE POINTS (1000 Total) - Checked ListBox - Saving/loading to database

PeterErhardAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

platinumbayCommented:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Windows.Forms.ListBox

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents ListBox_EthnicGroup As System.Windows.Forms.ListBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.ListBox_EthnicGroup = New System.Windows.Forms.ListBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'ListBox_EthnicGroup
        '
        Me.ListBox_EthnicGroup.Location = New System.Drawing.Point(72, 48)
        Me.ListBox_EthnicGroup.Name = "ListBox_EthnicGroup"
        Me.ListBox_EthnicGroup.SelectionMode = System.Windows.Forms.SelectionMode.MultiSimple
        Me.ListBox_EthnicGroup.Size = New System.Drawing.Size(120, 95)
        Me.ListBox_EthnicGroup.TabIndex = 0
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(120, 176)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 1
        Me.Button1.Text = " submit "
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.ListBox_EthnicGroup)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Dim cnn As New SqlConnection("Server=(local);Database=EthnicGroups;Trusted_Connection=True;")

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadEthnicGroups()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim clientID As Integer = 1
        Dim selItem As Integer
        Dim cmd As SqlCommand = cnn.CreateCommand
        If cnn.State <> ConnectionState.Open Then cnn.Open()
        cmd.CommandType = CommandType.Text
        Dim result As Integer
        For Each lvi As DataRowView In Me.ListBox_EthnicGroup.SelectedItems
            selItem = CInt(lvi.Item(0))
            'selItem = CInt(soc.Item(0))
            cmd.CommandText = "INSERT INTO ClientNumber_EthnicGroupIDAuto (ClientNumber, EthnicGroupIDAuto) VALUES (" & clientID & "," & selItem & ")"
            result += CInt(cmd.ExecuteNonQuery)
        Next

        If cnn.State <> ConnectionState.Closed Then cnn.Close()
        MsgBox(result & " rows updated.")
    End Sub

    Private Function LoadEthnicGroups() As Boolean
        Dim cmd As SqlCommand = cnn.CreateCommand
        Dim da As SqlDataAdapter = New SqlDataAdapter
        Dim ds As DataSet = New DataSet

        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "procLoadEthnicGroupOptions"

        da.SelectCommand = cmd
        da.Fill(ds, "EthnicGroupSelection")

        With ListBox_EthnicGroup
            .DisplayMember = "EthnicGroup"
            .ValueMember = "LookUp_EthnicGroupIDAuto"
            .DataSource = ds.Tables(0)
        End With

        LoadEthnicGroups = True
    End Function
End Class
0
PeterErhardAuthor Commented:
This doesn't work as lvi is undefined. What is lvi and where does the DataRowView come in?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim clientID As Integer = 1
        Dim selItem As Integer
        Dim cmd As SqlCommand = cnn.CreateCommand
        If cnn.State <> ConnectionState.Open Then cnn.Open()
        cmd.CommandType = CommandType.Text
        Dim result As Integer
        For Each lvi As DataRowView In Me.ListBox_EthnicGroup.SelectedItems
            selItem = CInt(lvi.Item(0))
            'selItem = CInt(soc.Item(0))
            cmd.CommandText = "INSERT INTO ClientNumber_EthnicGroupIDAuto (ClientNumber, EthnicGroupIDAuto) VALUES (" & clientID & "," & selItem & ")"
            result += CInt(cmd.ExecuteNonQuery)
        Next

        If cnn.State <> ConnectionState.Closed Then cnn.Close()
        MsgBox(result & " rows updated.")
    End Sub
0
PeterErhardAuthor Commented:
1000 points are still up for grabs.
0
platinumbayCommented:
The statement:

For Each lvi As DataRowView In Me.ListBox_EthnicGroup.SelectedItems

defines lvi as a DataRowView.  I am reading through the selected options.

You can replace that line with:

Dim lvi as DataRowView
For Each lvi In Me.ListBox_EthnicGroup.SelectedItems
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PeterErhardAuthor Commented:
The answers to my question are in this thread - http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21219393.html but I'll give the 500 poitns to platinumbay for his help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.