1

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
Chris
  • 8,527
  • 10
  • 34
  • 51
user2520014
  • 73
  • 2
  • 11
  • What is colNames and colValues? – Chris Jun 28 '13 at 16:55
  • If you are using Visual Studio, set a break point on the line where you set your query. Then debug step next (f8). What is in the variable "query"? It looks like either your combobox (colNames) does not have the value property set or it is set to a datarow. – tgolisch Jun 28 '13 at 17:02
  • In your load method, you might want to put your Me.BooksTableAdapter.Fill inside of a !IsPostBack... – Nick DeMayo Jun 28 '13 at 17:13

1 Answers1

1

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.

Community
  • 1
  • 1
Chris
  • 8,527
  • 10
  • 34
  • 51
  • hi colnames is a search by combo box and colvalues is a textbox for my keywords – user2520014 Jun 28 '13 at 17:37
  • @user2520014 So I confirm that the problem is that you can't set colnames.selectedValue.toString (ok i made a mistake in my answer and I have writed ColValues instead of ColNames). See the link above to convert it and get the value. – Chris Jun 28 '13 at 17:51
  • Chris is right. You are invoking ToString() on the object your combobox is bound to, and it's giving you the string representation of the object's type. – Cortright Jun 28 '13 at 19:29