locked
Write Name In Api RRS feed

  • Question

  • User-807418713 posted

    Hello

    If i call my api its showing like this

    {"Table":[]}

    but i want

    {"ABSalesData":[]}
    Wednesday, January 16, 2019 4:58 PM

Answers

  • User36583972 posted

    How To Add  the below in my above code

    {"ABSalesData":[]}

    Change the GetRecord method as below: Find the Table(you will return to client) in dataset and change TableName, then return it.

          //api/values/1
            public DataSet GetRecord(int id)
            {
                // get a dataset
                DataSet ds = new DataSet();
                DataTable table = new DataTable();
                table.Columns.Add("Dosage", typeof(int));
                table.Columns.Add("Drug", typeof(string));
                table.Columns.Add("Patient", typeof(string));
                table.Columns.Add("Date", typeof(DateTime));
    
                table.Rows.Add(25, "Indocin", "David", DateTime.Now);
                table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
                table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
                table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
                table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
                ds.Tables.Add(table);
                //-------------end dataset-----------
    
    
                // get a Table in dataset and change TableName
                ds.Tables[0].TableName = "ABSalesData";
                return ds;
            }

    Best Regards,

    Yong Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 17, 2019 8:08 AM

All replies

  • User36583972 posted

    Hi Gopi.MCA,

    Hello

    If i call my api its showing like this

    {"Table":[]}

    but i want

    {"ABSalesData":[]}

    You can define a class contains the ABSalesData field.

    For example:

      public class ParentObject
        {
            public List<Vehicaltest> ABSalesData { get; set; }
        }
    
        public class Vehicaltest
        {
            public int Vehical_ID { get; set; }
            public string Va_Cmp { get; set; }
            public string V_Colr { get; set; }
        }

    Web API:

         public IHttpActionResult Get()
            {
                ParentObject pob = new ParentObject();
                pob.ABSalesData = new List<Vehicaltest>()
            {
               new Vehicaltest()
               {
                   Vehical_ID = 101,
                   Va_Cmp = "Tvera",
                   V_Colr = "White",
               },
               new Vehicaltest()
               {
                   Vehical_ID = 102,
                   Va_Cmp = "Toyota",
                   V_Colr = "Gray",
               },
    
               new Vehicaltest()
               {
                   Vehical_ID = 1002,
                   Va_Cmp = "BMW",
                   V_Colr = "Red",
               },
    
               new Vehicaltest()
               {
                   Vehical_ID = 1002,
                   Va_Cmp = "Hyundai",
                   V_Colr = "Black",
               }
            };
    
                return Ok(pob);
            }
    

    Besides, It would be appreciated if you could close the thread by marking helpful posts as an answer. If you have a new question you can start a new thread with all necessary code snippets for anyone else to be able to reproduce your issue from scratch along with a detailed description about the results including any exception messages.


    Best Regards,

    Yong Lu

    Thursday, January 17, 2019 6:41 AM
  • User-807418713 posted

    Hello Yohan Lu

    Thanks For Reply

    This Is My API Code

    This Is My database access layer

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    
    namespace MVCAPI.database_access_layer
    {
        public class db
        {
    
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
            public DataSet GetRecordById(int id)
            {
    
                SqlCommand com = new SqlCommand("mysp", con);
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.AddWithValue("@SN", id);
                SqlDataAdapter da = new SqlDataAdapter(com);
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
    
            }
    
    
        }
    }

    This value controler

    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 MVCAPI.Controllers
    {
    
    
    
        public class ValuesController : ApiController
        {
    
            database_access_layer.db dblayer = new database_access_layer.db();
    
            // GET api/values
            public IEnumerable<string> Get()
            {
                return new string[] { "value1", "value2" };
            }
    
            public DataSet GetRecord(int id)
            {
    
                DataSet ds = dblayer.GetRecordById(id);
    
                return ds;
    
            }
    
            // GET api/values/5
            public string Get(int id)
            {
                return "value";
            }
    
            // POST api/values
            public void Post([FromBody]string value)
            {
            }
    
            // PUT api/values/5
            public void Put(int id, [FromBody]string value)
            {
            }
    
            // DELETE api/values/5
            public void Delete(int id)
            {
            }
        }
    }

    How To Add  the below in my above code

    {"ABSalesData":[]}
    Thursday, January 17, 2019 7:35 AM
  • User36583972 posted

    How To Add  the below in my above code

    {"ABSalesData":[]}

    Change the GetRecord method as below: Find the Table(you will return to client) in dataset and change TableName, then return it.

          //api/values/1
            public DataSet GetRecord(int id)
            {
                // get a dataset
                DataSet ds = new DataSet();
                DataTable table = new DataTable();
                table.Columns.Add("Dosage", typeof(int));
                table.Columns.Add("Drug", typeof(string));
                table.Columns.Add("Patient", typeof(string));
                table.Columns.Add("Date", typeof(DateTime));
    
                table.Rows.Add(25, "Indocin", "David", DateTime.Now);
                table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
                table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
                table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
                table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
                ds.Tables.Add(table);
                //-------------end dataset-----------
    
    
                // get a Table in dataset and change TableName
                ds.Tables[0].TableName = "ABSalesData";
                return ds;
            }

    Best Regards,

    Yong Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 17, 2019 8:08 AM