none
WEB Api: Problem in API (Get) RRS feed

  • Question

  • 1. From Postman

    {
        "Status": 200,
        "Message": "Success.",
        "Data": {
            "Table": [
                {
                    "Slider_ID": 7,
                    "Slider_Name": "Slider 4",
                    "Slider_Img_URL": "Slider/636169083787285383_15055723_10154554079010569_2983182283704857698_n.jpg",
                },
                {
                    "Slider_ID": 9,
                    "Slider_Name": "Slider3",
                    "Slider_Img_URL": "Slider/636023619016433974_3.jpg",
                }
            ]
        }
    }

    2. Controller:

    [ActionName("Slider")]
            public WebapiOutput GetHomeSlider()
            {
                WebapiOutput result = new WebapiOutput();
                

                try
                {
                    SliderDAC sliderDAC = new SliderDAC();
                    result.Status = 200;
                    result.Message = "Success.";
                    result.Data =sliderDAC.GetHomeSlider();
                }
                catch (Exception)
                {
                    result.Status = 204;
                    result.Message = "Internal server error.";
                    result.Data = new JObject();
                }
                return result;
            }

    3. Method:

    public DataSet GetHomeSlider()
            {
                try
                {
                    Database objDB = new SqlDatabase(this._sqlConnectionString);
                    DbCommand cmdDB = objDB.GetStoredProcCommand("[dbo].[USP_MMI_GETSLIDER]");

                    var ds = objDB.ExecuteDataSet(cmdDB);
                    return ds;
                }
                catch (Exception)
                {
                    throw;
                }

                

            }

    Module: 

    public class Slider
        {
            public int Slider_ID { get; set; }

            [Required(ErrorMessage = "Please enter slider name.")]
            public string Slider_Name { get; set; }
            public string Slider_Img_URL { get; set; }
          
        }

    The Problem is that "Table" is getting printed.

    • Moved by Sabah ShariqMVP Monday, September 4, 2017 1:03 PM Moved From Visual C#
    Monday, September 4, 2017 5:53 AM

All replies

  • Hi there ! 

    It's because you return the dataset , try something like this 

    DataTable firstTable = dataSet.Tables[0];

    loop over the datarows

    firstTable.Rows

    Add them to an array of DTO classes (data tranfer object) classes and return the array

    private void PrintRows(DataSet dataSet)
    {
        // For each table in the DataSet, print the row values.
        foreach(DataTable table in dataSet.Tables)
        {
            foreach(DataRow row in table.Rows)
            {
                foreach (DataColumn column in table.Columns)
                {
                    Console.WriteLine(row[column]);
    //add to an array
                }
            }
        }
    }

    friendly regards

    Laurens




    • Edited by laurens vdb Monday, September 4, 2017 12:36 PM
    Monday, September 4, 2017 6:46 AM
  • A WebAPI is a Web service, and you don't return a dataset or a datatable out of a Web service.

    https://www.hanselman.com/blog/ReturningDataSetsFromWebServicesIsTheSpawnOfSatanAndRepresentsAllThatIsTrulyEvilInTheWorld.aspx

    You should learn how to use a DTO or DTO(s) in a List<T>.

    https://en.wikipedia.org/wiki/Data_transfer_object

    https://www.codeproject.com/Articles/1050468/Data-Transfer-Object-Design-Pattern-in-Csharp

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-5

    You should learn how to use a Repository that deals with the CRUD operation with the db and not the Controller.

    http://www.c-sharpcorner.com/article/creating-web-api-with-repository-pattern-and-dependency-injection/

    WebAPi issues can be dicussed at the forum.

    http://forums.asp.net/

    Monday, September 4, 2017 11:38 AM
  • Hi,

    This is Visual C# forum. As your issue is related to Web, please ask your question into ASP.Net forum for getting quick response. I am moving your thread to off-topic.

    https://forums.asp.net/

    Your understanding and cooperation will be grateful.


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Monday, September 4, 2017 1:03 PM