Alnebras Murtada
Alnebras Murtada

Reputation: 135

Constructing a string with a value from a ListBox

I want to fill a datagridview when an item is selected from a listbox.

My code gives me this error:

Operator '&' is not defined for string "SELECT DeptName FROM Department" and type 'DataRowView'.

This my code:

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    cmd = New OleDbCommand("SELECT DeptName FROM  Departments inner  JOIN Faculties ON Faculties.ID = Departments.FacID where FacID='" & ListBox1.SelectedValue & "'", conn)
    Dim daa As New OleDbDataAdapter(cmd)
    Dim dtt As New DataTable
    daa.Fill(dtt)
    dgv.DataSource = dtt
End Sub

Upvotes: 1

Views: 100

Answers (2)

djv
djv

Reputation: 15774

There are a couple ways to look at this.

You could set the ValueMember on ListBox1 beforehand. Here is an example where the ListBox has values from 1 to 10, and their squares. The ValueMember is FacID, which is just the number. The DisplayMember is the square of FacID (for demonstration purposes!), demonstrating that you don't need to display the value member.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim i As Integer
    Dim table As New DataTable()
    Dim column1 As New DataColumn("FacID")
    Dim column2 As New DataColumn("Square")
    table.Columns.Add(column1)
    table.Columns.Add(column2)
    For i = 0 To 9
        Dim row As DataRow = table.NewRow()
        row("FacID") = i
        row("Square") = i ^ 2
        table.Rows.Add(row)
    Next i
    Dim view As New DataView(table)
    ListBox1.DataSource = view
    ' set the DisplayMember and ValueMember
    ListBox1.DisplayMember = "Square"
    ListBox1.ValueMember = "FacID"
End Sub

Private Sub ListBox1_SelectedValueChanged(sender As Object, e As EventArgs) Handles ListBox1.Click
    Dim message = "Selected value: " & ListBox1.SelectedValue.ToString()
    MessageBox.Show(message)
End Sub

This is what the ListBox would look like at run-time

enter image description here

And when it is clicked, the message showing the selected value

If this is done before running the code you provided, then you could almost use your code as is, because now ListBox1.ValueMember is an integer. Note, you should be more strict and convert the integer to a string in preparation for the query.

Dim query =
    "SELECT DeptName FROM Departments " &
    "inner JOIN Faculties ON Faculties.ID = Departments.FacID " &
    "where FacID = '" & ListBox1.SelectedValue.ToString() & "'"

You could also not set the ValueMember. When ValueMember is not specified, your SelectedValue defaults to the type in the DataSource, which is a DataView, and, for all intents and purposes, an IList of DataRowView.

With no DisplayMember, the ListBox looks like this

enter image description here

I assume it's not like this. So just set the ValueMember beforehand and you should be all set.

Upvotes: 1

Pikoh
Pikoh

Reputation: 7703

ListBox1.SelectedValue is a DataRowView, so you can't add it to a string. you'll probably want something like:

"SELECT DeptName FROM  Departments inner  JOIN Faculties ON Faculties.ID = Departments.FacID where FacID='" _
     & ListBox1.SelectedValue["yourColumn"].ToString() & "'"

Or

"SELECT DeptName FROM  Departments inner  JOIN Faculties ON Faculties.ID = Departments.FacID where FacID='" _
     & ListBox1.SelectedValue[columnIndex].ToString() & "'"

Upvotes: 0

Related Questions