SidC
SidC

Reputation: 3203

How to Search a Database Table using One of Two Fields from ASP:TextBox and LINQ to Entities

My goal is to place a search box on partsearch.aspx page The search box will contain a textbox and a dropdownlist. The dropdownlist has two items, PartName or NSN, which need to be used in the where clause of my database query. If PartName is the selected value, then the query needs to be based on that field. If NSN is the selected value, then the query needs to search the NSN field for the contents of the textbox, txtSearch.

I have the following code in my page:

<asp:Label runat="server" ID="lblSearch" CssClass="black-normal-txt">Search by Part Name or NSN:</asp:Label>
<asp:TextBox runat="server" ID="txtSearch" CssClass="txtSearch swap_value" Text="Search for Parts"></asp:TextBox>
<asp:DropDownList runat="server" ID="ddlsearch" CssClass="black-normal-txt">
<asp:ListItem>Part Name</asp:ListItem>
<asp:ListItem>NSN</asp:ListItem></asp:DropDownList>
<asp:Button runat="server" ID="btnSubmit" Text="Search" />

The codebehind looks like:

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
    Using PartEntities As New diel_inventoryEntities()
        Dim Parts = PartEntities.PartList
        Dim query = From PartList In Parts

        If (ddlsearch.selecteditem.tostring() = "PartName") Then
            query = query.Where(Function(t) t.PARTNAME.Contains(txtSearch))
        ElseIf (ddlsearch.selecteditem.tostring() = "NSN") Then
            Query = Query.where(Function(t) t.NSN.Contains(txtSearch))
        End If
    End Using
End Sub

I receive the following error:

Error 2 Overload resolution failed because no accessible 'Where' can be called with these arguments: Extension method 'Public Function Where(predicate As System.Func(Of Diel_inventoryModel.PartList, Integer, Boolean)) As System.Collections.Generic.IEnumerable(Of Diel_inventoryModel.PartList)' defined in 'System.Linq.Enumerable': Nested function does not have the same signature as delegate 'System.Func(Of Diel_inventoryModel.PartList, Integer, Boolean)'. Extension method 'Public Function Where(predicate As System.Func(Of Diel_inventoryModel.PartList, Boolean)) As System.Collections.Generic.IEnumerable(Of Diel_inventoryModel.PartList)' defined in 'System.Linq.Enumerable': Overload resolution failed because no accessible 'Contains' can be called with these arguments: 'Public Function Contains(value As String) As Boolean': Value of type 'System.Web.UI.WebControls.TextBox' cannot be converted to 'String'. Extension method 'Public Function Contains(value As Char) As Boolean' defined in 'System.Linq.Enumerable': Value of type 'System.Web.UI.WebControls.TextBox' cannot be converted to 'Char'. Extension method 'Public Function Where(predicate As System.Linq.Expressions.Expression(Of System.Func(Of Diel_inventoryModel.PartList, Integer, Boolean))) As System.Linq.IQueryable(Of Diel_inventoryModel.PartList)' defined in 'System.Linq.Queryable': Nested function does not have the same signature as delegate 'System.Func(Of Diel_inventoryModel.PartList, Integer, Boolean)'. Extension method 'Public Function Where(predicate As System.Linq.Expressions.Expression(Of System.Func(Of Diel_inventoryModel.PartList, Boolean))) As System.Linq.IQueryable(Of Diel_inventoryModel.PartList)' defined in 'System.Linq.Queryable': Overload resolution failed because no accessible 'Contains' can be called with these arguments: 'Public Function Contains(value As String) As Boolean': Value of type 'System.Web.UI.WebControls.TextBox' cannot be converted to 'String'. Extension method 'Public Function Contains(value As Char) As Boolean' defined in 'System.Linq.Enumerable': Value of type 'System.Web.UI.WebControls.TextBox' cannot be converted to 'Char'. c:\inetpub\wwwroot\mydomain.com\PartSearch.aspx.vb 15 25 http://localhost/mydomain.com/

Is anyone familiar with this error? If so, how do I diagnose and fix it?

Thanks much for your help and guidance.

Upvotes: 0

Views: 1546

Answers (1)

Marek Karbarz
Marek Karbarz

Reputation: 29294

Try not relying on VB default properties (not even sure they still have those in VB.NET - haven't used it in a while); use the Text property on the TextBox control directly: Contains(txtSearch.Text)

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) 

Handles btnSubmit.Click
    Using PartEntities As New diel_inventoryEntities()
        Dim Parts = PartEntities.PartList
        Dim query = From PartList In Parts

        If (ddlsearch.selecteditem.tostring() = "PartName") Then
            query = query.Where(Function(t) t.PARTNAME.Contains(txtSearch.Text))
        ElseIf (ddlsearch.selecteditem.tostring() = "NSN") Then
            Query = Query.where(Function(t) t.NSN.Contains(txtSearch.Text))
        End If
    End Using
End Sub

Upvotes: 2

Related Questions