Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

VB.Net listbox items to an Access Database

$
0
0

I have the current code, and it works as far as putting something into the database but only the last operation and not all of the selected list box items. I need the entire list box list put into the database with each operation on a separate row.

Dim Affected As Integer = 0

        Dim Builder As New OleDb.OleDbConnectionStringBuilder With
            {
                .Provider = "Microsoft.ACE.OLEDB.12.0",
                .DataSource = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CalcDB.accdb"),
                .PersistSecurityInfo = False
            }

       
        Using cn As New OleDb.OleDbConnection With {.ConnectionString = Builder.ConnectionString}
            Using cmd As New OleDb.OleDbCommand With {.Connection = cn}

                cmd.CommandText =
                    <SQL>
                    INSERT INTO CalcOps 
                    (
                        NumOne, 
                        Operation, 
                        NumTwo,
                        Equal,
                        Result
                    ) 
                    VALUES 
                    (
                        @NumOne, 
                        @Operation, 
                        @NumTwo, 
                        @Equal,
                        @Result
                    )</SQL>.Value

                With cmd.Parameters

                    .AddWithValue("@NumOne", NumOne.Text)
                    .AddWithValue("@Operation", ListBox1.Tag)
                    .AddWithValue("@NumTwo", NumTwo.Text)
                    .AddWithValue("@Equal", "=")
                    .AddWithValue("@Result", Result.Text)
                End With

                Dim x As Integer
                For x = 0 To ListBox1.Items.Count - 1
                    If ListBox1.GetSelected(x) = False Then
                        ListBox1.SetSelected(x, True)
                    End If
                Next


                Try
                    cn.Open()
                    Affected = cmd.ExecuteNonQuery

                Catch ex As Exception
                    MessageBox.Show("Problem:" & Environment.NewLine & ex.Message)
                End Try

            End Using
        End Using


        If Affected = 1 Then
           
            MessageBox.Show("Insert done")
        Else
            MessageBox.Show("Insert failed")
        End If


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>