TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Tri Setia
982
464
24.4k
How to consume web api from windows form
Apr 3 2021 4:24 AM
hi guys,, l'm learn how to consume wep api from windows form, when I try consume wep api using webform run properly and can cunsume all method, but when I'm consume wep api from windows form I'm getting error with the message
Configuration system failed to initialize
, I'm just try to get all data from
Web APIs
using windows form
.
Any help could be appreciated.
this the code behind
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
using
System.Windows.Forms;
using
System.Web.Script.Serialization;
using
System.Net.Http;
using
System.Net.Http.Formatting;
using
System.Net;
namespace
WinFormConsumeRestfulApi
{
public
partial
class
Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
private
void
button1_Click(
object
sender, EventArgs e)
{
using
(HttpClient _Client =
new
HttpClient())
{
_Client.BaseAddress =
new
Uri(
"http://localhost:1028/RestfullApis/"
);
HttpResponseMessage respon = _Client.GetAsync(
"api/peminjamanBuku"
).Result;
var pinjamBuku = respon.Content.ReadAsAsync<IEnumerable<tbl_peminjaman>>().Result;
dataGridView1.DataSource = pinjamBuku;
}
}
}
}
this the web api Controller
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Net;
using
System.Net.Http;
using
System.Web.Http;
namespace
Restfull_Apis.Controllers
{
public
class
PeminjamanBukuController : ApiController
{
[HttpPost]
public
IEnumerable<tbl_peminjaman> Get()
{
using
(perpustakaanEntities entity =
new
perpustakaanEntities())
{
return
entity.tbl_peminjaman.ToList();
}
}
public
tbl_peminjaman Get(
int
id)
{
using
(perpustakaanEntities entity =
new
perpustakaanEntities())
{
return
entity.tbl_peminjaman.FirstOrDefault(p => p.id_peminjaman == id);
}
}
public
void
Post([FromBody] tbl_peminjaman _PinjamBuku)
{
using
(perpustakaanEntities entity =
new
perpustakaanEntities())
{
entity.tbl_peminjaman.Add(_PinjamBuku);
entity.SaveChanges();
}
}
public
void
Put(
int
id, [FromBody] tbl_peminjaman Pinjam)
{
using
(perpustakaanEntities entity =
new
perpustakaanEntities())
{
var _entity = entity.tbl_peminjaman.FirstOrDefault(p => p.id_peminjaman == id);
_entity.tanggal_pinjam = Pinjam.tanggal_pinjam;
_entity.tanggal_kembali = Pinjam.tanggal_kembali;
_entity.id_buku = Pinjam.id_buku;
_entity.id_anggota = Pinjam.id_anggota;
_entity.id_petugas = Pinjam.id_petugas;
entity.SaveChanges();
}
}
public
void
Delete(
int
id)
{
using
(perpustakaanEntities entity =
new
perpustakaanEntities())
{
entity.tbl_peminjaman.Remove(entity.tbl_peminjaman.FirstOrDefault(p => p.id_peminjaman == id));
entity.SaveChanges();
}
}
}
}
Reply
Answers (
3
)
curd operation by using asp.net mvc and jquery with boostrap
Add new button is missing in sub grid dynamic 365 CRM