locked
Get All Data From API RRS feed

  • Question

  • User-807418713 posted

    Hello

    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)
            {
            }
        }
    }
    

    It working Fine

    Now I Have One More Which Return All Data And How To Add This Code To Fetch All Data

     public DataSet GetRecord()
            {
    
                SqlCommand com = new SqlCommand("myspalldata", con);
                com.CommandType = CommandType.StoredProcedure;           
                SqlDataAdapter da = new SqlDataAdapter(com);
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
    
            }

    I want to add this GetRecord How To Add In My Existing Code

    Need Help

    Thanks

    Monday, January 14, 2019 5:17 PM

Answers

  • User-2054057000 posted

    Hello Gopi.MCA, 

    You add the below Method to your Values controller: Notice the method returns IEnumerable<Student> instead of DataSet. 

    public IEnumerable<Student> GetRecord()
    {
        SqlCommand com = new SqlCommand("myspalldata", con);
        com.CommandType = CommandType.StoredProcedure;           
        SqlDataAdapter da = new SqlDataAdapter(com);
        DataSet ds = new DataSet();
        da.Fill(ds);
        List<Student> stu = new List<Student>();   
                             
        // loop through all the records in ds and add to stu object    
        foreach (DataRow dr in ds.table[0].Rows)
        {
            Student student = new Student();
            student.Name = dr["Name"].ToString();
            student.Age = Convert.ToInt32(dr["Age"]);
            student.Address = dr["Add"].ToString(); 
            //....
            stu.Add(student);   
        }
        return stu;
    }

    Kindly see this reference tutorial on API Creations in ASP.NET MVC 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 14, 2019 6:38 PM

All replies

  • User475983607 posted

    I want to add this GetRecord How To Add In My Existing Code

    Below is a basic example of the syntax.

        public class RecordController : ApiController
        {
            db dblayer;
            public RecordController()
            {
                dblayer = new db();
            }
            // GET: api/Record
            public DataSet Get()
            {
                DataSet ds = dblayer.GetRecord();
                return ds;
            }
    
            // GET: api/Record/5
            public DataSet Get(int id)
            {
                DataSet ds = dblayer.GetRecordById(id);
                return ds;
            }
    
            // POST: api/Record
            public void Post([FromBody]string value)
            {
            }
    
            // PUT: api/Record/5
            public void Put(int id, [FromBody]string value)
            {
            }
    
            // DELETE: api/Record/5
            public void Delete(int id)
            {
            }
        }
    

    I strongly recommend that you go through the Web API tutorials to learn the basics.  The tutorials explain REST and how to properly design a REST (Web API) service.  I also recommend returning a class and not a DataSet.  DataSets are ASP.NET types and only known to ASP.NET.

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

    Monday, January 14, 2019 6:18 PM
  • User-2054057000 posted

    Hello Gopi.MCA, 

    You add the below Method to your Values controller: Notice the method returns IEnumerable<Student> instead of DataSet. 

    public IEnumerable<Student> GetRecord()
    {
        SqlCommand com = new SqlCommand("myspalldata", con);
        com.CommandType = CommandType.StoredProcedure;           
        SqlDataAdapter da = new SqlDataAdapter(com);
        DataSet ds = new DataSet();
        da.Fill(ds);
        List<Student> stu = new List<Student>();   
                             
        // loop through all the records in ds and add to stu object    
        foreach (DataRow dr in ds.table[0].Rows)
        {
            Student student = new Student();
            student.Name = dr["Name"].ToString();
            student.Age = Convert.ToInt32(dr["Age"]);
            student.Address = dr["Add"].ToString(); 
            //....
            stu.Add(student);   
        }
        return stu;
    }

    Kindly see this reference tutorial on API Creations in ASP.NET MVC 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 14, 2019 6:38 PM