How to get selected items in the checklistbox - vb.net

I made a program to populate checklistbox2 when checklistbox items is checked. Here I got the result but the process is taken in a different way.
The problem is that checklistbox2 is populating when 3 times clicks on a listbox in checkedlistbox1
Dim connection As New OleDbConnection("Data Source=xxx;Provider=xxxx; User Id=xxxx; Password=xxx")
Dim dt2 As New DataTable
Dim i As Integer
For i = 0 To CheckedListBox1.Items.Count - 1 Step i + 1
If CheckedListBox1.GetItemCheckState(i) = CheckState.Checked Then
Dim xx As String = (CType(CheckedListBox1.Items(i), DataRowView))("name")
Dim sqlstr2 As String = "xxxxxxxxxxxxxx"
Dim command2 As New OleDbCommand(sqlstr2, connection)
Dim adpt2 As New OleDbDataAdapter(command2)
adpt2.SelectCommand = command2
adpt2.Fill(dt2)
CheckedListBox2.DataSource = dt2
CheckedListBox2.DisplayMember = "name"
CheckedListBox2.ValueMember = "name"
End If
Next

Related

Moving rows up and down in a datagridview and updating the database

I have a datagridview and I am getting all the data from a sql database I want to be able to move rows from one position to the other either up or down and it should also reflect the same in the database.
Currently this is what I have done and it does move the items as desired ( am adding 100 to the value of the column that am using to move up and subtracting 100 when moving down) but the problem with this is that if it find that the value of the next item in the row is lower, it doesnt move or if you add 100 then its value suddenly becomes more that 3 items above it, it doesnt move to the next position instead it moves 3 positions up, how can I simplify it to make it move just 1 row above or below respectively.
Private Sub BntMoveUp_Click(sender As Object, e As EventArgs) Handles BntMoveUp.Click
For i As Integer = 0 To DataGridView2.Rows.Count() - 1
Dim c As Boolean
c = DataGridView2.Rows(i).Cells(0).Value
' if the checkbox cell is checked
If c = True Then
' MessageBox.Show("Checked")
Dim cid As Integer = DataGridView2.Rows(i).Cells(1).Value
Dim cid2 As Integer = DataGridView2.Rows(i - 1).Cells(1).Value
Str = "update Production..CMBGSeat_Staging set [Priority] =[Priority] > 1 where cid=" & cid
updaterecords(Str)
Dim cmd As Odbc.OdbcCommand
Dim LDataadapter As Odbc.OdbcDataAdapter
Dim MyDataset As DataSet
Dim insert As String
con()
insert = "select * from Production..CMBGSeat_Staging where DateCompleted is null order by Priority desc"
cmd = New Odbc.OdbcCommand(insert, sqlcon)
LDataadapter = New Odbc.OdbcDataAdapter
LDataadapter.SelectCommand = cmd
MyDataset = New DataSet
' MyDataset = New DataSet
LDataadapter.Fill(MyDataset)
DataGridView2.DataSource = MyDataset.Tables(0)
' if not
Else
' MessageBox.Show("Not Checked")
End If
Next
End Sub
Private Sub BtnMoveDown_Click(sender As Object, e As EventArgs) Handles BtnMoveDown.Click
For i As Integer = 0 To DataGridView2.Rows.Count() - 1
Dim c As Boolean
c = DataGridView2.Rows(i).Cells(0).Value
'If the Then checkbox cell Is checked
If c = True Then
' MessageBox.Show("Checked")
Dim cid As Integer = DataGridView2.Rows(i).Cells(1).Value
Dim cid2 As Integer = DataGridView2.Rows(i + 1).Cells(1).Value
Str = "update Production..CMBGSeat_Staging set [Priority] =[Priority] < 1 where cid=" & cid
updaterecords(Str)
Dim cmd As Odbc.OdbcCommand
Dim LDataadapter As Odbc.OdbcDataAdapter
Dim MyDataset As DataSet
Dim insert As String
con()
insert = "select * from Production..CMBGSeat_Staging where DateCompleted is null order by Priority desc"
cmd = New Odbc.OdbcCommand(insert, sqlcon)
LDataadapter = New Odbc.OdbcDataAdapter
LDataadapter.SelectCommand = cmd
MyDataset = New DataSet
MyDataset = New DataSet
LDataadapter.Fill(MyDataset)
DataGridView2.DataSource = MyDataset.Tables(0)
' if not
Else
'MessageBox.Show("Not Checked")
End If
Next
End Sub

Problem with my usercontrol listview fill data from msaccess

I created listview ms activex by vb.net to my access database
i add that code in vb.net to fill my listview
Public Sub FillListview(ByVal CurrntDb As String)
Dim conn As OleDbConnection
Dim cmd As OleDbCommand
Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim itemcoll(100) As String
ListView1.View = View.Details
ListView1.GridLines = True
ListView1.Items.Clear()
conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source =" & CurrntDb)
Dim strQ As String = String.Empty
strQ = "SELECT Accounts.ID AS [م], Customers.Customer AS [العميل], Accounts.Debit AS [مدين], Accounts.Credit AS [دائن], Accounts.Dates AS [التاريخ], Accounts.Notes AS [البيان] FROM Accounts INNER JOIN Customers ON Accounts.Customer_ID = Customers.Customer_ID;"
cmd = New OleDbCommand(strQ, conn)
da = New OleDbDataAdapter(cmd)
ds = New DataSet
conn.Open()
da.Fill(ds)
Dim i As Integer = 0
Dim j As Integer = 0
' adding the columns in ListView
For i = 0 To ds.Tables(0).Columns.Count - 1
ListView1.Columns.Add(ds.Tables(0).Columns(i).ColumnName.ToString())
Next
'Now adding the Items in Listview
For i = 0 To ds.Tables(0).Rows.Count - 1
For j = 0 To ds.Tables(0).Columns.Count - 1
itemcoll(j) = ds.Tables(0).Rows(i)(j).ToString()
Next
Dim lvi As New ListViewItem(itemcoll)
ListView1.Items.Add(lvi)
ListView1.Refresh()
Next
conn.Close()
End Sub
and regestir it in my access database
and call it in access code by that code
Dim xlist As New AForTestListviewBySedo.UserControl1
Call xlist.FillListview("C:\Users\Elsayed\Desktop\New folder\mydb.accdb")
but they give me nothing not error but nothing
I tested that code in usercontrol by add button in it
button works good
What can I do ?
the solution is with my code in access to call record
i need to set object at first
the correct code is
Option Explicit
Option Compare Database
Public listv As MsAccessListviewACX1_00.UCBySedo
Public Sub LoadListview()
Set listv = Me.UCBySedo9.Object
listv.FillListview ("C:\Users\Elsayed\Desktop\New folder\mydb.accdb")
End Sub
Private Sub Command115_Click()
Call LoadListview
End Sub

Trying to add a list of suppliers in an array and then i am adding it to listbox and combobox using `.addrange` but i am receiving error

I retrieved list of suppliers from database and saved it in an array TOTAL_SUPPLIERS_ARRAY NOW i am trying it to add in the listbox or combobox but it shows an error on runtime saying "VALUE CAN NOT BE NULL" but if i try to add it with an loop it works properly why is it not working with .addrange ?
Sub GET_SUPPLIERS_DETAILS()
Dim CON As New MySqlConnection("server=localhost; username=root; password=Masoom1; database=airtech_db;")
Dim cmd As New MySqlCommand("Select * from `Suppliers`;", CON)
Dim da As New MySqlDataAdapter("Select * from `Suppliers`;", CON)
Dim ds As New DataSet
Dim dr As MySqlDataReader
Dim TOTAL_SUPPLIERS As Integer
CON.Open()
da.Fill(ds)
dr = cmd.ExecuteReader
TOTAL_SUPPLIERS = ds.Tables(0).Rows.Count
Dim TOTAL_SUPPLIERS_ARRAY(TOTAL_SUPPLIERS) As String, ARRAYINDEX As Integer
Do While dr.Read() = True
TOTAL_SUPPLIERS_ARRAY(ARRAYINDEX) = dr("Supplier_Name").ToString()
ARRAYINDEX += 1
Loop
CON.Close()
Dim cbCell As New DataGridViewComboBoxCell
For k = 0 To DataGridView1.Rows.Count - 1
cbCell = DataGridView1.Rows(k).Cells("Supplier_Name")
For iIndex = 0 To UBound(TOTAL_SUPPLIERS_ARRAY) - 1
cbCell.Items.Add(TOTAL_SUPPLIERS_ARRAY(iIndex))
Next
Next
ListBox1.Items.AddRange(TOTAL_SUPPLIERS_ARRAY)
ComboBox1.Items.AddRange(TOTAL_SUPPLIERS_ARRAY)
For I As Integer = 0 To UBound(TOTAL_SUPPLIERS_ARRAY) - 1
TextBox1.Text += TOTAL_SUPPLIERS_ARRAY(I) & " - "
Next I
End Sub
This part works only as i added it through a loop
For I As Integer = 0 To UBound(TOTAL_SUPPLIERS_ARRAY) - 1
TextBox1.Text += TOTAL_SUPPLIERS_ARRAY(I) & " - "
Next I
listbox.addrange command and combobox.addrange command not working
Here you have an issue TOTAL_SUPPLIERS_ARRAY(TOTAL_SUPPLIERS). Array declared in VB - array(upper-bound). Upper bound is 1 less than count. So, you should do TOTAL_SUPPLIERS_ARRAY(TOTAL_SUPPLIERS - 1).
But why struggle? Just use list
Dim myList as New List(of String)
While dr.Read()
myList.Add(dr("Supplier_Name").ToString())
Loop
And then, if you still need array, you can use LINQ - myList.ToArray
ListBox1.DataSource = myList.ToArray()

how to add list in `DATAGRIDVIEW` with combobox on runtime

i tried the following code but it adds the list every time when we start new row so if we have two rows its add it two times if we move to three rows it adds the list for three time
Please provide me simple and easy way to add the list into the combobox of datagridview
Dim CON As New MySqlConnection("server=localhost; username=root; password=Masoom1; database=airtech_db;")
Dim cmd As New MySqlCommand("Select * from `Suppliers`;", CON)
Dim da As New MySqlDataAdapter("Select * from `Suppliers`;", CON)
Dim ds As New DataSet
Dim dr As MySqlDataReader
Dim TOTAL_SUPPLIERS As Integer
CON.Open()
da.Fill(ds)
dr = cmd.ExecuteReader
TOTAL_SUPPLIERS = ds.Tables(0).Rows.Count
Dim TOTAL_SUPPLIERS_ARRAY(TOTAL_SUPPLIERS) As String, ARRAYINDEX As Integer
ARRAYINDEX = 0
Do While dr.Read() = True
TOTAL_SUPPLIERS_ARRAY(ARRAYINDEX) = dr("Supplier_Name").ToString()
ARRAYINDEX += 1
Loop
CON.Close()
Dim cbCell As New DataGridViewComboBoxCell
For k = 0 To DataGridView1.Rows.Count - 1
cbCell = DataGridView1.Rows(k).Cells("Supplier_Name")
For iIndex = 0 To UBound(TOTAL_SUPPLIERS_ARRAY) - 1
cbCell.Items.Add(TOTAL_SUPPLIERS_ARRAY(iIndex))
Next
Next
You can set it only once for a column.
For example you can populate combobox when loading a form.
Private Sub Form_Load(sender As Object, e As EventArgs) Handles MyForm.Load
With Me.DataGridView.Columns("Supplier_Name")
.DataSource = GetSupplierNames()
End With
End Sub
Private Function GetSupplierNames() As List(Of String)
Dim query As String = "SELECT Supplier_Name FROM Suppliers"
Using connection As New MySqlConnection(connectionString)
Using command As New MySqlCommand(query, connection)
connection.Open()
Dim supplierNames = new List(Of String)()
Using reader AS MySqlDataReader = command.ExecuteReader()
While reader.Read()
Dim name As String = reader.GetString(0)
supplierNames.Add(name)
End While
End Using
Return supplierNames
End Using
End Using
End Function
well I found a simple solution to existing code to fix the problem as every time it runs clear the existing list and let the list add again so add cbCell.Items.Clear()to code helped
Dim cbCell As New DataGridViewComboBoxCell
For k = 0 To DataGridView1.Rows.Count - 1
cbCell = DataGridView1.Rows(k).Cells("Supplier_Name")
cbCell.Items.Clear()
For iIndex = 0 To UBound(TOTAL_SUPPLIERS_ARRAY)
cbCell.Items.Add(TOTAL_SUPPLIERS_ARRAY(iIndex))
Next
Next

add listbox values to access database windows form application

I have a calculator with two textboxes where the user puts a number in each one. They then click a plus, minus, divide, or multiply button and it does that function to the numbers. These numbers are saved to a listbox called listbox1. When the user clicks to display the results, the listbox is populated with all their saved values, and the application is SUPPOSED to save the listbox items to an access database. it is not working. Below is my code, where numFirst is the name of a category in the database table:
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles
btnDisplay.Click
ListBox1.Items.Clear()
For arrayindex As Integer = 0 To intarrayCount - 1
ListBox1.Items.Add(Input(arrayindex))
ListBox1.Text = Convert.ToString(ListBox1.Items.Item(arrayindex))
Next arrayindex
Dim query As String = "SELECT * FROM wk6"
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\helse_000\Desktop\APU\VB Advanced\week4\ENTD461_wk4_Andrew_Helsel\ENTD461_wk2_Andrew_Helsel\calculator.mdb"
Dim command As OleDbCommand = New OleDbCommand
Dim var1 As String = Convert.ToString(ListBox1.Items.Item(0))
command.CommandText = "INSERT into wk6 (numFirst) VALUES (" + var1 + ")"
Figured it out, removed the select all query string and made my textbox fields into parameters after modifying their results a bit to fit the format I needed for the table.
For i As Integer = 0 To ListBox1.Items.Count - 1
Dim firstString As String = Convert.ToString(ListBox1.Items.Item(i))
Dim leftPart As String = firstString.Split(Convert.ToChar
(strButtonSelected))(0)
Dim rightPart As String = firstString.Split(Convert.ToChar("="))(1)
Dim a As Integer = firstString.IndexOf(strButtonSelected)
Dim b As Integer = firstString.IndexOf("=")
Dim secNum = firstString.Substring(a + 1, b - 4)
secNum = secNum.Trim
Dim conn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=C:\Users\helse_000\Desktop\APU\VB Advanced\week4\ENTD461_wk4_Andrew_Helsel\ENTD461_wk2_Andrew_Helsel\calculator.mdb")
Dim command As OleDbCommand = New OleDbCommand
Dim cmdstring As String = "INSERT INTO wk6 (numFirst, numSecond, Operator, equals, Answer)" + " VALUES (#firstName,#lastName,#Operator,#equals,#answer)"
command = New OleDbCommand(cmdstring, conn)
command.Parameters.AddWithValue("#firstName", leftPart)
command.Parameters.AddWithValue("#lastName", secNum)
command.Parameters.AddWithValue("#Operator", strButtonSelected)
command.Parameters.AddWithValue("#equals", "=")
command.Parameters.AddWithValue("#answer", rightPart)
conn.Open()
command.ExecuteNonQuery()
conn.Close()