Answered by:
regarding API creation

Question
-
User364480375 posted
Dear Masters,
I have to create API for following table :
CREATE TABLE [dbo].[praptra54]( [pid] [int] IDENTITY(1,1) NOT NULL, [r_date] [datetime] NULL, [r_month] [nvarchar](50) NULL, [DistrictId] [int] NULL, [cell_02] [numeric](18, 2) NULL, [cell_03] [numeric](18, 2) NULL, [cell_04] [numeric](18, 2) NULL, [cell_05] [numeric](18, 2) NULL, [cell_06] [numeric](18, 2) NULL, [cell_07] [numeric](18, 2) NULL, [cell_08] [numeric](18, 2) NULL, [cell_09] [numeric](18, 2) NULL, [cell_10] [numeric](18, 2) NULL, [cell_11] [numeric](18, 2) NULL, [cell_12] [nvarchar](max) NULL, [createddate] [datetime] NULL, [status] [int] NULL ) ON [PRIMARY]
I am using webform . I am new to API creation .I have to create API so that data can be consumed by other project team .Kindly suggest
when user select month then data should be displayed based on that selection.Sunday, July 29, 2018 3:38 AM
Answers
-
User-369506445 posted
I create a sample for you, please follow it step by step
<g class="gr_ gr_34 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="34" data-gr-id="34">First</g> create a Web API project
then create a Model <g class="gr_ gr_107 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="107" data-gr-id="107">basedon</g> your table
public class PraptraEntity { public int pid { get; set; } public DateTime r_date { get; set; } public string r_month { get; set; } public int DistrictId { get; set; } public DateTime createddate { get; set; } public int status { get; set; } public decimal cell_02 { get; set; } public decimal cell_03 { get; set; } public decimal cell_04 { get; set; } public decimal cell_05 { get; set; } public decimal cell_06 { get; set; } public decimal cell_07 { get; set; } public decimal cell_08 { get; set; } public decimal cell_09 { get; set; } public decimal cell_10 { get; set; } public decimal cell_11 { get; set; } public string cell_12 { get; set; } }
then create <g class="gr_ gr_40 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Grammar multiReplace" id="40" data-gr-id="40">a <g class="gr_ gr_39 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling ins-del multiReplace" id="39" data-gr-id="39">api</g></g> controller called PraptraController
public class PraptraController : ApiController { //suppose your model is below List<PraptraEntity> Model = new List<PraptraEntity>() { new PraptraEntity() { pid = 1, r_date = DateTime.Now, r_month ="05", createddate=DateTime.Now , DistrictId = 1 , status = 0 } }; // GET: api/Praptra public IEnumerable<PraptraEntity> Get(string month) { var result = Model.Where(x => x.r_month == month).ToList(); return result; } } public class PraptraEntity { public int pid { get; set; } public DateTime r_date { get; set; } public string r_month { get; set; } public int DistrictId { get; set; } public DateTime createddate { get; set; } public int status { get; set; } public decimal cell_02 { get; set; } public decimal cell_03 { get; set; } public decimal cell_04 { get; set; } public decimal cell_05 { get; set; } public decimal cell_06 { get; set; } public decimal cell_07 { get; set; } public decimal cell_08 { get; set; } public decimal cell_09 { get; set; } public decimal cell_10 { get; set; } public decimal cell_11 { get; set; } public string cell_12 { get; set; } }
now run your project and enter the below <g class="gr_ gr_300 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="300" data-gr-id="300">addres</g> on <g class="gr_ gr_315 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="315" data-gr-id="315">browser</g>
http://localhost:51984/api/Praptra?month=05
just change localhost:51984 to your address
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, July 29, 2018 4:31 AM -
User753101303 posted
Hi,
It should be easier to help if you tell us what is the first problem you have when you try to use the existing documentation.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, July 30, 2018 7:55 AM
All replies
-
User-369506445 posted
hi
you can use Web API or WCF or web service .now what's your problem ?
Sunday, July 29, 2018 3:52 AM -
User-369506445 posted
here are good samples that create a web <g class="gr_ gr_44 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="44" data-gr-id="44">api</g> step by step please follow below links:
http://www.tutorialsteacher.com/webapi/create-web-api-project
Sunday, July 29, 2018 4:02 AM -
User364480375 posted
i need to use webAPI. I am new Web API.
Sunday, July 29, 2018 4:02 AM -
User-369506445 posted
I create a sample for you, please follow it step by step
<g class="gr_ gr_34 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="34" data-gr-id="34">First</g> create a Web API project
then create a Model <g class="gr_ gr_107 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="107" data-gr-id="107">basedon</g> your table
public class PraptraEntity { public int pid { get; set; } public DateTime r_date { get; set; } public string r_month { get; set; } public int DistrictId { get; set; } public DateTime createddate { get; set; } public int status { get; set; } public decimal cell_02 { get; set; } public decimal cell_03 { get; set; } public decimal cell_04 { get; set; } public decimal cell_05 { get; set; } public decimal cell_06 { get; set; } public decimal cell_07 { get; set; } public decimal cell_08 { get; set; } public decimal cell_09 { get; set; } public decimal cell_10 { get; set; } public decimal cell_11 { get; set; } public string cell_12 { get; set; } }
then create <g class="gr_ gr_40 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Grammar multiReplace" id="40" data-gr-id="40">a <g class="gr_ gr_39 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling ins-del multiReplace" id="39" data-gr-id="39">api</g></g> controller called PraptraController
public class PraptraController : ApiController { //suppose your model is below List<PraptraEntity> Model = new List<PraptraEntity>() { new PraptraEntity() { pid = 1, r_date = DateTime.Now, r_month ="05", createddate=DateTime.Now , DistrictId = 1 , status = 0 } }; // GET: api/Praptra public IEnumerable<PraptraEntity> Get(string month) { var result = Model.Where(x => x.r_month == month).ToList(); return result; } } public class PraptraEntity { public int pid { get; set; } public DateTime r_date { get; set; } public string r_month { get; set; } public int DistrictId { get; set; } public DateTime createddate { get; set; } public int status { get; set; } public decimal cell_02 { get; set; } public decimal cell_03 { get; set; } public decimal cell_04 { get; set; } public decimal cell_05 { get; set; } public decimal cell_06 { get; set; } public decimal cell_07 { get; set; } public decimal cell_08 { get; set; } public decimal cell_09 { get; set; } public decimal cell_10 { get; set; } public decimal cell_11 { get; set; } public string cell_12 { get; set; } }
now run your project and enter the below <g class="gr_ gr_300 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="300" data-gr-id="300">addres</g> on <g class="gr_ gr_315 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="315" data-gr-id="315">browser</g>
http://localhost:51984/api/Praptra?month=05
just change localhost:51984 to your address
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, July 29, 2018 4:31 AM -
User-369506445 posted
-
Sunday, July 29, 2018 4:32 AM -
User1992938117 posted
As Vahid shared code, follow the same but I would recommend you to read it first.
Monday, July 30, 2018 5:41 AM -
User753101303 posted
Hi,
It should be easier to help if you tell us what is the first problem you have when you try to use the existing documentation.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, July 30, 2018 7:55 AM -
User364480375 posted
vahid i am getting error:
public class PraptraController : ApiController { List<PraptraEntity> Model = (from item in dbo.praptra54 where item.r_month == month// getting error month doesnt exist in current context select new PraptraEntity { pid = item.pid, r_date = item.r_date, r_month = item.r_month, createddate = item.createddate, DistrictId = item.DistrictId, status = item.status, cell_02=item.cell_02 }).ToList(); public IEnumerable<PraptraEntity> Get(string month) { var result = Model.Where(x => x.r_month == month).ToList(); return result; } }
Friday, August 3, 2018 1:57 AM -
User-369506445 posted
You have to remove the out side query in your action.
Your controller must be below like
public class PraptraController : ApiController { public IEnumerable<PraptraEntity> Get(string month) { List<PraptraEntity> Model = (from item in dbo.praptra54 where item.r_month == month// getting error month doesnt exist in current context select new PraptraEntity { pid = item.pid, r_date = item.r_date, r_month = item.r_month, createddate = item.createddate, DistrictId = item.DistrictId, status = item.status, cell_02=item.cell_02 }).ToList(); return Model; } }
Friday, August 3, 2018 5:40 AM -
User364480375 posted
vahid,
i am getting error that:
public IEnumerable<PraptraEntity> Get(string month) { List<PraptraEntity> Model = (from item in dbo.praptra54//dbo doesnt exist in current context
from sql server i have :
select * FROM [landrecord].[dbo].[praptra54]
[landrecord] :database name , table name:[praptra54]
Friday, August 3, 2018 5:58 AM -
User-369506445 posted
dbo.praptra54
the above line <g class="gr_ gr_25 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="25" data-gr-id="25">is call</g> your praptra54 with entity framework,
do you use EntityFarmeWorkor <g class="gr_ gr_96 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="96" data-gr-id="96">Ado.Net ?</g>
Friday, August 3, 2018 6:03 AM -
User364480375 posted
ado.net
Friday, August 3, 2018 6:15 AM -
User-369506445 posted
please try below
public IEnumerable<PraptraEntity> Get(string month) { List<PraptraEntity> model = new List<PraptraEntity>(); string sql = "select pid,r_date,r_month,createddate,DistrictId,status,cell_02" + " from praptra54" + "where r_date='" + month + "'"; using (var con = new SqlConnection(strConnString)) { using (var com = new SqlCommand(sql, con)) { using (SqlDataReader rdr = com.ExecuteReader()) { while (rdr.Read()) { PraptraEntity praptraEntity = new PraptraEntity(); praptraEntity.pid = Convert.ToInt32(rdr["pid"]); praptraEntity.r_date = Convert.ToDateTime(rdr["r_date"]);
. // here put your others fiels like above
.
. model.Add(praptraEntity); } } } } return model; }Friday, August 3, 2018 6:34 AM -
User364480375 posted
hi vahid sir,
thnks for help. now i am unable to run:here is my whole code:
using System; using System.Collections.Generic; using System.Configuration; using System.Data.SqlClient; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; namespace WebApplication3.Controllers { public class PraptraController : ApiController { public IEnumerable<PraptraEntity> Get(string month) { List<PraptraEntity> model = new List<PraptraEntity>(); string sql = "select pid,r_date,r_month,DistrictId,status,cell_02,cell_03,cell_04,cell_05,cell_06,cell_07,cell_08,cell_09,cell_10,cell_11,cell_12" + " from praptra54" + "where r_month='" + month + "'"; using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString)) { using (SqlCommand com = new SqlCommand(sql, con)) { using (SqlDataReader rdr = com.ExecuteReader()) { while (rdr.Read()) { PraptraEntity praptraEntity = new PraptraEntity(); praptraEntity.pid = Convert.ToInt32(rdr["pid"]); praptraEntity.r_date = Convert.ToDateTime(rdr["r_date"]); praptraEntity.r_month = Convert.ToString(rdr["r_month"]); praptraEntity.DistrictId = Convert.ToInt32(rdr["DistrictId"]); praptraEntity.cell_02 = Convert.ToInt32(rdr["cell_02"]); praptraEntity.cell_03 = Convert.ToInt32(rdr["cell_03"]); praptraEntity.cell_04 = Convert.ToInt32(rdr["cell_04"]); praptraEntity.cell_05 = Convert.ToInt32(rdr["cell_05"]); praptraEntity.cell_06 = Convert.ToInt32(rdr["cell_06"]); praptraEntity.cell_07 = Convert.ToInt32(rdr["cell_07"]); praptraEntity.cell_08 = Convert.ToInt32(rdr["cell_08"]); praptraEntity.cell_09 = Convert.ToInt32(rdr["cell_09"]); praptraEntity.cell_10 = Convert.ToInt32(rdr["cell_10"]); praptraEntity.cell_11 = Convert.ToInt32(rdr["cell_11"]); praptraEntity.cell_12 = Convert.ToString(rdr["cell_12"]); model.Add(praptraEntity); } } } } return model; } } // GET: api/Praptra public class PraptraEntity { public int pid { get; set; } public DateTime r_date { get; set; } public string r_month { get; set; } public int DistrictId { get; set; } public DateTime createddate { get; set; } public int status { get; set; } public decimal cell_02 { get; set; } public decimal cell_03 { get; set; } public decimal cell_04 { get; set; } public decimal cell_05 { get; set; } public decimal cell_06 { get; set; } public decimal cell_07 { get; set; } public decimal cell_08 { get; set; } public decimal cell_09 { get; set; } public decimal cell_10 { get; set; } public decimal cell_11 { get; set; } public string cell_12 { get; set; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace WebApplication3.Models { public class PraptraEntity { public int pid { get; set; } public DateTime r_date { get; set; } public string r_month { get; set; } public int DistrictId { get; set; } public DateTime createddate { get; set; } public int status { get; set; } public decimal cell_02 { get; set; } public decimal cell_03 { get; set; } public decimal cell_04 { get; set; } public decimal cell_05 { get; set; } public decimal cell_06 { get; set; } public decimal cell_07 { get; set; } public decimal cell_08 { get; set; } public decimal cell_09 { get; set; } public decimal cell_10 { get; set; } public decimal cell_11 { get; set; } public string cell_12 { get; set; } } }
config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional }
when i tried to run i am getting error: 404 not foundhttp://localhost:xxx/api/Praptra?month=july
Saturday, August 4, 2018 2:12 AM -
User-369506445 posted
hi
it seems all of the things are correct, but your URL can't find the resource
please try attribute Roue below like
public class PraptraController : ApiController { [Route("api/MyPraptra")] public IEnumerable<PraptraEntity> Get(string month) { List<PraptraEntity> model = new List<PraptraEntity>();
and try below <g class="gr_ gr_232 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="232" data-gr-id="232">url</g>
http://localhost:xxx/api/MyPraptra?month=july
Saturday, August 4, 2018 4:11 AM -
User364480375 posted
still getting same error
Saturday, August 4, 2018 4:28 AM -
User-369506445 posted
put here your API controller and WebApiConfig file, please exactly that you used
Saturday, August 4, 2018 4:30 AM -
User364480375 posted
weapicinfig
using System; using System.Collections.Generic; using System.Linq; using System.Web.Http; namespace WebApplication3 { public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Web API configuration and services // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } } }
webconfig:
<?xml version="1.0" encoding="utf-8"?> <!-- For more information on how to configure your ASP.NET application, please visit http://go.microsoft.com/fwlink/?LinkId=301879 --> <configuration> <appSettings> </appSettings> <system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5" /> </system.web> <system.webServer> <handlers> <remove name="ExtensionlessUrlHandler-Integrated-4.0" /> <remove name="OPTIONSVerbHandler" /> <remove name="TRACEVerbHandler" /> <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /> <!--<add name="con" connectionString="Data Source=.;Initial Catalog=record;User ID=sa;Password=12345" providerName="System.Data.SqlClient" />--> </handlers> </system.webServer> <connectionStrings> <!--<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-DAV3-20150302043828.mdf;Initial Catalog=aspnet-DAV3-20150302043828;Integrated Security=True" providerName="System.Data.SqlClient" />--> <!--<add name="connectionString" connectionString="data source=localhost;Initial Catalog=*****;User ID=sa;Password=*****;" providerName="System.Data.SqlClient" />--> <!--<add name="connectionString" connectionString="data source=localhost;Initial Catalog=Davincis3;User ID=*****;Password=*****;" providerName="System.Data.SqlClient" />--> <!--<add name="connectionString" connectionString="data source=DELLLAPTOP-PC\SQLSERVEREXPRESS;Initial Catalog=Davincis3;User ID=sa;Password=*****;" providerName="System.Data.SqlClient" />--> <add name="conn" connectionString="Data Source=.;Initial Catalog=record;User ID=sa;Password=12345" providerName="System.Data.SqlClient" /> </connectionStrings> </configuration>
using System; using System.Collections.Generic; using System.Configuration; using System.Data.SqlClient; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; namespace WebApplication3.Controllers { public class PraptraController : ApiController { [Route("api/MyPraptra")] public IEnumerable<PraptraEntity> Get(string month) { List<PraptraEntity> model = new List<PraptraEntity>(); string sql = "select pid,r_date,r_month,DistrictId,status,cell_02,cell_03,cell_04,cell_05,cell_06,cell_07,cell_08,cell_09,cell_10,cell_11,cell_12" + " from praptra54" + "where r_month='" + month + "'"; using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString)) { using (SqlCommand com = new SqlCommand(sql, con)) { using (SqlDataReader rdr = com.ExecuteReader()) { while (rdr.Read()) { PraptraEntity praptraEntity = new PraptraEntity(); praptraEntity.pid = Convert.ToInt32(rdr["pid"]); praptraEntity.r_date = Convert.ToDateTime(rdr["r_date"]); praptraEntity.r_month = Convert.ToString(rdr["r_month"]); praptraEntity.DistrictId = Convert.ToInt32(rdr["DistrictId"]); praptraEntity.cell_02 = Convert.ToInt32(rdr["cell_02"]); praptraEntity.cell_03 = Convert.ToInt32(rdr["cell_03"]); praptraEntity.cell_04 = Convert.ToInt32(rdr["cell_04"]); praptraEntity.cell_05 = Convert.ToInt32(rdr["cell_05"]); praptraEntity.cell_06 = Convert.ToInt32(rdr["cell_06"]); praptraEntity.cell_07 = Convert.ToInt32(rdr["cell_07"]); praptraEntity.cell_08 = Convert.ToInt32(rdr["cell_08"]); praptraEntity.cell_09 = Convert.ToInt32(rdr["cell_09"]); praptraEntity.cell_10 = Convert.ToInt32(rdr["cell_10"]); praptraEntity.cell_11 = Convert.ToInt32(rdr["cell_11"]); praptraEntity.cell_12 = Convert.ToString(rdr["cell_12"]); model.Add(praptraEntity); } } } } return model; } } // GET: api/Praptra public class PraptraEntity { public int pid { get; set; } public DateTime r_date { get; set; } public string r_month { get; set; } public int DistrictId { get; set; } public DateTime createddate { get; set; } public int status { get; set; } public decimal cell_02 { get; set; } public decimal cell_03 { get; set; } public decimal cell_04 { get; set; } public decimal cell_05 { get; set; } public decimal cell_06 { get; set; } public decimal cell_07 { get; set; } public decimal cell_08 { get; set; } public decimal cell_09 { get; set; } public decimal cell_10 { get; set; } public decimal cell_11 { get; set; } public string cell_12 { get; set; } } }
Saturday, August 4, 2018 4:36 AM -
User-369506445 posted
I checked it, and it was correct
I think you do not enter the correct address
I entered below address and it returns values, you can enter the below address and just change the port number to yours
http://localhost:51984/api/MyPraptra?month=july
Saturday, August 4, 2018 4:47 AM -
User-369506445 posted
please put your Global.asax code here
Saturday, August 4, 2018 4:54 AM -
User364480375 posted
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Http; using System.Web.Routing; namespace WebApplication3 { public class WebApiApplication : System.Web.HttpApplication { protected void Application_Start() { GlobalConfiguration.Configure(WebApiConfig.Register); } } }
Saturday, August 4, 2018 5:10 AM