Consume ASP.NET Web API In Windows Forms DataGridView Using HttpClient | Web API @CodingKnowledge

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone this is satya prakas welcome to my channel this is part 23 of asp.net web api in this video i'll discuss how to consume web api in windows forms using data grid view data grid view is the control of windows form application so before going through this part 23 please visit my part 20 part 22 of my asp.net web api so there i have shown you how to consume asp.net web api in asp.net grid view using http client and this part 23 is relevant to these two videos called say part 7 is asp.net web api using sql server and entity framework and part part 8 is how to retrieve data in asp.net web api using sql server and entity framework so i'll go to my presentation what point we need to discuss in this video first one is how to add http client to windows forms and point number two is how to consume web api and display records in data grid views so guys here i have open to instance of visual studio this instance is for web api i already have discussed in this in my all the videos it is common to all the videos right and second instance is for windows forms applications so here i have created windows forms application and that is called consume api winform so it is very simple just click on file new project all right and it will show you one windows so here you can see it is a select here under visual scissor windows desktop and you can see windows forms app and dotnet framework right after then you will give your name of that file solution and the path you need to choose and framework and click ok once you do that one you should you can see your solution explorer your project name and project related files right here the properties reference app.config format.cs and program.cs so this is the interface you can see that for form one of windows form application right and i'll i'll i have mentioned few steps so step number one is how to add windows form a project so i already have discussed how to add windows form application and step number two is our http client from new get package manager so for that what i'll do just right click on your project and select the manage nuget packages and guys i already have uh mentioned about this steps to install a installation of http client in my previous videos the same steps you need to follow up here so i already have installed the http client for that the package name is microsoft.sp.net espnet.web.web api dot client so here i have installed that is the as of now the version installed installed version name is 5.2.9 if you not install you just go to browse and browse here the name and install it right and once the installation is successful just expand your reference folder you can see that one blue icon is found that microsoft.spnet.wavp.client and you can see one dll file called system.net.http right that means your http client required packages already installed here okay now go through the step number three that is i need to add a class file for mapping property with web api response so guys here i have created one folder called employee and inside i have added one class name called employee.cs right and in employee.cs i have mentioned all the property names that id first name last name gender and sally with its uh return type all right data type so according to web app response i have created all the properties same as the web api response and the same same employee class i have created in my previous videos for asp.net grid view and same same class name i have created also here right so this is all about create the class for our web for our windows form application to map to our web api response step number four is add data grid view from the toolbox to form1.cs design so guys in windows forms application there is two important files called form1.cs design and form1.cs so form1.cs design is for the frontend and form1.cs is for the codebehind file so here we need to drag and drop two controls one is data grid view so for that you just what i'll do is go to your uh views okay and go to windows format tools here you can get the toolbox right in the views yeah click on views and you can you can see toolbox or you can go to the shortcut called control alt dot x plus x after then you can see one tool box is appear on your right on your left hand side below to the server explorer to that to add the data grid view you can see one data is there so expand it you can see data grid view right here you just drag and drop to the uh form1 dot cs design from the toolbox and no need to design anything right and step number five is we need to add label control from the toolbox uh to form one dot cs design so data grid data grid view is for showing that records from web api and i have added the label control so for that you just expand the command control you can see one level control just drag and drop here right and here i have modified few things for uh web for the label so properties right and here what i'll what i've done is uh i have changed the font uh properties is called the point name and select bold as true right and the name i have mentioned here as microsoft sans serif and after then i change the for color to red and and here i have sent the design name of label is label error it is the id of the level control label to control and in the fronted file it will show the text as label 2 so that it is showing as level 2 but when we configure something in our code behind file then use the id of that label control that label error so that's all about for the label control right and this label control e is for that is showing the exception if anything is found in our code so uh step number six is we need to add some code in form1.cs for consume web api and exception handling so here i will show you that is one form one dot cs right so here what i'll what i've done is i need to add that namespace for access the http client class and one is for access the employee class we need to add other namespace so here the employee dot cs we can find here the namespace so we just mentioned this namespace inside the form1.cs to access the employee class and its uh properties right employee date class and its properties all right so after then public form one it is the load event so it has one one method is there initialize component it is by default when we create the windows form application and one is refresh data so it is a i have done it so here i have created one method called refresh data and here i have used try block right it is for exception handling using try catch so inside try block here in inimitable type of object getting from class name employee date employee date and emp ops is the object of i enumerable so i have mentioned here this code and it i said the object is to set null initially and after then i consume web api using client class so http client is equal to new http client and in uh uri i added asp.net web api url as a string parameter so the code is sc dot based address i and a new uri it is the url of my web api right and next one is i need to consume data so get asking method text parameter a string the request uri and the request in uri is the api control name so i have declared one variable called consume api and the object of http client dot it is the method of method of http client gate async and i mentioned the employees employ nothing but the web api controller and here i set the wait method and next is i need to read the data so var read data equal to consume web api dot result after then i need to check the red condition whether data read ready or not so if read data dot is success status code then added the i list class and this is the type of elements in list they are getting data from employee date class is a method so here i declare one display data variable and here i read data.content using red async i have mentioned the i list collection of class and inside i mentioned the employee date class right so this is the type of element in list they are getting data from i employ that class it is a method right and here i have mentioned the display data dot wait before displaying the record in grid view or data grid view we need to collect the data into the eye numerable object that is emp obj right here i have declared here right in rule object object that is the emp obj so here uh amp obj dot display data dot result and finally i will add this data to our data grid view control so this is the data grid view control id right so data grid view one right here i mentioned in the comment box so data grid view one is getting the data source from ie enumerable object that is the emp obj and then re then bind the data so this is the id of my data grid view control you can see here right if you just right click on this uh control and go to properties you can see the id of the data grid view control you can see here right data grid view y1 so using that id of data grid v1 dot data source equal to emp obj and when the data is successfully faced from web api to data grid view so i will mention here as label control to visible false and inside cache i have mentioned the label error dot text equal to please check whether api is running or not see if any exception is found like web ap is not running and in that time the exception is found so that label control is just showing the warning or error message to the end user that is the please check whether web api is running or not so this is all about the steps guys only six steps after then our code is ready to run so what we'll do here is we just uh right click on the solution and build solution so our build is succeeded so i need to run my application so here you can see one start up button is there so we need to click on it so guys our our form is loaded and you can see this is the interface we have we are getting in the in the windows forms application so this is the grid view it shows empty as well as it is showing one error message that please check whether a web api is running or not so that means if you go to my code you can see the exception is found right that means our web api is not running so that we are getting this exceptions right so we need to close it and we'll make sure the web api application is running or not so go to the web api project so this is the web api project so our app project is not running so that we are getting this exception so we need to run our web api first so just click on it ias express so guys here you can see my web api runs successfully i am getting the result of employee details so after then we need to run our web web form application so go to my web form application and click on start so guys now you can see all the details of employee is faced properly by using web api right there is no exceptions so i can see all the employee details with id first name last name gender and salary so we need to compare the result with our with our web api response so we need to check id 1 so go to my web ap response for id 1 first name is ram 1 last name is pradhan 1 gender is mail sal is 34 000 so we need to check same response in our windows form application so for id 1 first name is ram 1 last name is pradhan 1 gender is mail salary is 34 000 and we need to check here id 16 so for id 16 first name is bill last name is strong gender is male and salary is 23 000 so go to my windows application so for id 16 first name is wills last name is strong gender is male and salary is 23 000 so guys that's all about the steps how to consume web api in our windows form application so i'll go to my presentation here so this is my youtube channel subscribe and share it to get the updated knowledge on dotnet dot and core csr sql server github angular and devops so that's it for today guys thank you for listening have a great day
Info
Channel: Coding Knowledge
Views: 6,953
Rating: undefined out of 5
Keywords: consume web api in windows application c#, consume asp.net web api in windows form, web api in windows forms c#, consume web api in c#, consume web api in mvc application, consume web api in asp.net core mvc, consume web api in asp.net, consume web api in asp.net core, how to consume web api in angular, asp.net web api tutorial, asp.net web api project, asp.net web api interview questions, coding knowledge, consume web api using httpclient, consume asp.net web api from c#, api, web
Id: Qih-8TS1Tnc
Channel Id: undefined
Length: 16min 11sec (971 seconds)
Published: Sun Aug 21 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.