Reputation: 73
I am making a small database using sql server as the back and vb as the front end, I have nearly made it work however I have stumbled across this error:
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: The multi-part identifier "System.Data.DataRowView" could not be bound.
Here is my code:
Imports System.Data.SqlClient
Public Class searchDialog
Private Sub searchDialog_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'SearchDataSet.Books' table.
'You can move, or remove it, as needed.
Me.BooksTableAdapter.Fill(Me.SearchDataSet.Books)
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim ds As New DataSet
Dim query As String = "select * from Books where " + colNames.SelectedValue.ToString + " LIKE " + "'%" + colValues.Text + "%'"
BooksTableAdapter.Connection.Open()
Dim adp As New SqlDataAdapter(query, BooksTableAdapter.Connection.ConnectionString)
adp.Fill(ds, "Books")
BooksTableAdapter.Connection.Close()
filteredRecords.DataSource = ds
filteredRecords.DataMember = "Books"
End Sub
End Class
Upvotes: 1
Views: 3697
Reputation: 8647
Problem is here:
Dim query As String = "select * from Books where " + colNames.SelectedValue.ToString + " LIKE " + "'%" + colValues.Text + "%'"
I guess that colNames
is a control which is bound to a DataTable
or a Dataview
, and so selectedValue is a DataRowView
.
This is exactly the same issue in this post listbox selected item give me " System.Data.DataRowView" , C# winforms.
You can't set selectedValue.ToString
since it will allways return "System.Data.DataRowView".
You need to cast the selected item into DataRowView and then you can get a value from it.
Upvotes: 1