Hi makumbi,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
Name: <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<br />
Country: <asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="Search" />
<hr />
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName" />
<asp:BoundField DataField="City" HeaderText="City" />
</Columns>
</asp:GridView>
Model
Public Class CustomerModel
Public Property Name As String
Public Property Country As String
End Class
Namespaces
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Web.Http
Imports System.Net
Imports System.Web.Script.Serialization
Controller
Public Class CustomerAPIController
Inherits ApiController
<Route("api/CustomerAPI/GetCustomers")>
<HttpPost>
Public Function GetCustomers(customer As CustomerModel) As List(Of Customer)
Dim customers As New List(Of Customer)()
Dim constr As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString
Using con As New SqlConnection(constr)
Dim query As String = "SELECT CustomerID,ContactName,City,Country FROM Customers WHERE (ContactName LIKE @Name + '%' OR @Name IS NULL) AND (Country = @Country OR @Country IS NULL)"
Using cmd As New SqlCommand(query)
cmd.Connection = con
cmd.Parameters.AddWithValue("@Name", customer.Name)
cmd.Parameters.AddWithValue("@Country", customer.Country)
con.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader()
While sdr.Read()
customers.Add(New Customer() With {
.CustomerID = sdr("CustomerID").ToString(),
.ContactName = sdr("ContactName").ToString(),
.City = sdr("City").ToString(),
.Country = sdr("Country").ToString()
})
End While
End Using
con.Close()
End Using
End Using
Return customers
End Function
End Class
Code
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.PopulateGridView()
End If
End Sub
Protected Sub Search(ByVal sender As Object, ByVal e As EventArgs)
Me.PopulateGridView()
End Sub
Private Sub PopulateGridView()
Dim apiUrl As String = "http://localhost:61977/api/CustomerAPI"
Dim input As Object = New With {
.Name = txtName.Text.Trim(),
.Country = txtCountry.Text.Trim()
}
Dim inputJson As String = New JavaScriptSerializer().Serialize(input)
Dim client As WebClient = New WebClient()
client.Headers("Content-type") = "application/json"
client.Encoding = Encoding.UTF8
Dim json As String = client.UploadString(apiUrl & "/GetCustomers", inputJson)
gvCustomers.DataSource = (New JavaScriptSerializer()).Deserialize(Of List(Of Customer))(json)
gvCustomers.DataBind()
End Sub
Public Class Customer
Public Property CustomerID As String
Public Property ContactName As String
Public Property City As String
Public Property Country As String
End Class
Screenshot