sunny kumar

sunny kumar

  • NA
  • 333
  • 8.7k

Showing sql query results in json format

Aug 7 2019 1:00 AM
  1. public class ValuesController : ApiController  
  2.     {  
  3.         Database_Access_layer.service db = new Database_Access_layer.service();  
  4.   
  5.   
  6.         public DataSet GetRecord()  
  7.         {  
  8.             DataSet ds = db.GetRecords();  
  9.             return ds;  
  10.         }  
  1. public class service  
  2.    {  
  3.        string st = ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString;  
  4.        public DataSet GetRecords()  
  5.        {  
  6.            SqlConnection con = new SqlConnection(st);  
  7.            string adp = "";  
  8.            adp = @"select   s.State_Id, RTRIM(s.State_Name) as state_name,count(1) as totalNo  
  9.   
  10.   
  11.   
  12.   
  13.   
  14.        
  15.   
  16.    from  BUyer_details                    
  17.   
  18.                   
  19.   
  20.   inner join Vehicles_Details vd  on BUyer_details.Vehicles_Id=vd.Vehicles_Id                    
  21.   
  22.   inner join Model_Registration mr on vd.Model_Code=mr.xEV_Model_Code                     
  23.   
  24.   left outer join OEM_Registration om on BUyer_details.OEM_Id = om.OEM_Id       
  25.   
  26.       
  27.   
  28.   inner join Dealer_Registration dr  on BUyer_details.Dealer_Id= dr.Dealer_Id  
  29.   
  30.    inner join Mst_Vehicle_Category vc on mr.Vihecle_cat_id=vc.Vihecle_cat_id   
  31.   
  32. inner join tbl_MasterState s on dr.State_Id=s.State_Id   
  33.   
  34.   where 1=1  group by s.State_Name,s.State_Id order by State_Id asc";  
  35.            SqlDataAdapter aa = new SqlDataAdapter(adp, con);  
  36.            DataSet ds = new DataSet();  
  37.            aa.Fill(ds);  
  38.            return ds;  
  39.        }  
  40.    }  
I tried creating the query and then returning in json format, but it is showing collections, i want a single array

Answers (2)