Hi SUJAYS,
Using the article i have created the example for multiple parameter.
Refer below code.
Namespaces
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.Http;
Model
public class CustomerModel
{
/// <summary>
/// Gets or sets Name.
/// </summary>
public string Name { get; set; }
/// <summary>
/// Gets or sets City.
/// </summary>
public string City { get; set; }
}
ApiController
public class CustomerAPIController : ApiController
{
[Route("api/CustomerAPI/GetCustomers")]
[HttpPost]
public List<Customer> GetCustomers(CustomerModel customer)
{
List<Customer> customers = new List<Customer>();
string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = "SELECT CustomerID,ContactName,City FROM Customers WHERE (ContactName LIKE @Name + '%' OR @Name IS NULL) AND (City = @City OR @City IS NULL)";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Name", !string.IsNullOrEmpty(customer.Name) ? customer.Name : (object)DBNull.Value);
cmd.Parameters.AddWithValue("@City", !string.IsNullOrEmpty(customer.City) ? customer.City : (object)DBNull.Value);
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
customers.Add(new Customer
{
CustomerID = sdr["CustomerID"].ToString(),
ContactName = sdr["ContactName"].ToString(),
City = sdr["City"].ToString()
});
}
}
con.Close();
}
}
return customers;
}
public class Customer
{
public string CustomerID { get; set; }
public string ContactName { get; set; }
public string City { get; set; }
}
}
HTML
Name:<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<br />
City:<asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="Search" />
<hr />
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField ItemStyle-Width="150px" DataField="CustomerID" HeaderText="CustomerID" />
<asp:BoundField ItemStyle-Width="150px" DataField="ContactName" HeaderText="Contact Name" />
<asp:BoundField ItemStyle-Width="150px" DataField="City" HeaderText="City" />
</Columns>
</asp:GridView>
Namespaces
using System.Net;
using System.Text;
using System.Web.Script.Serialization;
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.PopulateGridView();
}
}
protected void Search(object sender, EventArgs e)
{
this.PopulateGridView();
}
private void PopulateGridView()
{
string apiUrl = "http://localhost:26404/api/CustomerAPI";
object input = new
{
Name = txtName.Text.Trim(),
City= txtCity.Text.Trim()
};
string inputJson = (new JavaScriptSerializer()).Serialize(input);
WebClient client = new WebClient();
client.Headers["Content-type"] = "application/json";
client.Encoding = Encoding.UTF8;
string json = client.UploadString(apiUrl + "/GetCustomers", inputJson);
gvCustomers.DataSource = (new JavaScriptSerializer()).Deserialize<List<Customer>>(json);
gvCustomers.DataBind();
}
public class Customer
{
public string CustomerID { get; set; }
public string ContactName { get; set; }
public string City { get; set; }
}
Screenshot