How to display a table row data, when the user wants to fetch based on id using web api in asp.net? Here, I have done displaying all table rows of data based on nothing.
CustomerDetailsController.cs :
using WebApiDemo.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace WebApiDemo.Controllers
{
public class CustomerDetailsController : ApiController
{
public static IList<CustomerDetail> listCusDt = new List<CustomerDetail>()
{
};
[AcceptVerbs("GET")]
public CustomerDetail RPCStyleMethodFetchFirstCustomerDetails()
{
return listCusDt.FirstOrDefault();
}
[HttpGet]
[ActionName("GetAllCustomerDetails")]
public List<CustomerDetail> Get(int? id = null)
{
SqlDataReader reader = null;
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = ConfigurationManager.ConnectionStrings["ERPConnectionString"].ConnectionString;
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.CommandType = CommandType.StoredProcedure;
if (id != null)
sqlCmd.CommandText = "getBasketsForCustomer" + id + "";
else
sqlCmd.CommandText = "getBasketsForCustomer";
sqlCmd.Connection = myConnection;
myConnection.Open();
reader = sqlCmd.ExecuteReader();
List<CustomerDetail> customerDetails = new List<CustomerDetail>();
while (reader.Read())
{
CustomerDetail cusdt = new CustomerDetail();
cusdt.OrderId = Convert.ToInt32(reader.GetValue(0));
cusdt.CustomerId = reader.GetValue(1).ToString();
cusdt.OrderDate = reader.GetValue(2).ToString();
cusdt.ProductName = reader.GetValue(3).ToString();
cusdt.OrderId = Convert.ToInt32(reader.GetValue(4));
cusdt.OrderId = Convert.ToInt32(reader.GetValue(5));
cusdt.OrderId = Convert.ToInt32(reader.GetValue(6));
customerDetails.Add(cusdt);
}
return customerDetails;
myConnection.Close();
}
}
}
CustomerDetail.cs :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApiDemo.Models
{
public class CustomerDetail
{
public int OrderId { get; set; }
public string CustomerId { get; set; }
public string OrderDate { get; set; }
public string ProductName { get; set; }
public int Quantity { get; set; }
public int UnitPrice { get; set; }
public int Amount { get; set; }
}
}