-1

I have a textbox and a button. What i am attempting to do is filter a gridview with the data that has been searched for. The two fields im searching for are customerID and CustomerName.

My code:

Dim GetAllCusts As New BusinessLayer.Customer
Dim dt As DataTable = GetAllCusts.GetCustomers 
Dim dv As New DataView(dt)

dv.RowFilter = String.Format("CustomerID='{0}' Or CustomerName='{0}'", SearchTextbox.Text) 

gridview1.DataSource = dv
gridview1.DataBind()

When searching for a customer id everything works. When searching for a customer name i get "Cannot perform '=' operation on System.Int32 and System.String.".

I tried to CONVERT(CustomerName, System.String) but that didnt resolve however i think this was leading me away from the initial problem as i started to guess around.

I would like to have one textbox to search for a customer id and customer name. What is wrong with the RowFilter and how could it be resolved?

Thanks

4

2 に答える 2

4

概念的な問題があります。または、数値を検索するか、文字列を検索します。
このようにコードを変更する必要があると思います

Dim custID as Integer
if Int32.TryParse(SearchTextbox.Text, custID) Then
   ' CustomerID is a numeric field - no need to use single quotes'
   dv.RowFilter = String.Format("CustomerID={0}", custID) 
else
   ' CustomerName is a string. Use single quotes and double the '
   ' possible single quotes inside the search box'
   dv.RowFilter = String.Format("CustomerName='{0}'", SearchTextbox.Text.Replace("'", "''") 
End If
gridview1.DataSource = dv
于 2013-04-04T11:19:23.210 に答える
0

CutomerId は整数フィールドだと思います。文字列フィールドである Name を検索しようとすると、文字列値で整数フィールドを検索しようとしています。

于 2013-04-04T11:21:37.390 に答える