Introduction
In this article, I'm going to create an application to get user details in a model popup in ReactJS with Axios using Web API with the help of an example SQL Server database as back-end. A Web API is used to provide data connectivity between the database and the front-end application for building RESTful services.
On the UI side, I will use bootstrap to create a rich, interactive, device-independent user experience so as to building a beautiful UI. In this example, I will bind user details in a short format ( i.e. will see only necessary columns) and when we want to see the records in detail of a particular user, then will click a specific row. After that, it will show records in a popup window and then clicking the Close button will close the popup. Let us see step by step.
I'm using Visual Studio Code as a tool to build my application. If you don't have Visual Studio Code in your system, then first you have to download and install. Here is the Visual Studio Code download link: Download Visual Studio Code Editor.
Prerequisites
- Visual Studio
- SQL Server
- JavaScript version > 10
- React
- React Axios
- Visual Studio Code
- Bootstrap
- React-bootstrap
Step 1 - Create a database and table
Create a database. Open SQL Server and create a new database and table. As you can see from the following query, I have created the database tables called Branch Details and Company Details.
UserDetails
- CREATE TABLE [dbo].[UserDetails](
- [UserId] [int] IDENTITY(1,1) NOT NULL,
- [FirstName] [varchar](50) NULL,
- [LastName] [varchar](50) NULL,
- [EmailId] [varchar](100) NULL,
- [MobileNo] [varchar](50) NULL,
- [Address] [varchar](500) NULL,
- [PinCode] [char](10) NULL,
- [CompanyName] [varchar](100) NULL,
- [Gender] [nchar](20) NULL,
- [Country] [varchar](50) NULL,
- [State] [varchar](100) NULL,
- [City] [varchar](100) NULL,
- CONSTRAINT [PK_UserDetails] PRIMARY KEY CLUSTERED
- (
- [UserId] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
- GO
- SET ANSI_PADDING OFF
- GO
- SET IDENTITY_INSERT [dbo].[UserDetails] ON
- INSERT [dbo].[UserDetails] VALUES (2, N'Mithilesh', N'kumar', N'[email protected]', N'3049583043', N'hyderabad', N'039458 ', N'Microsoft', N'Male, ', N'India', N'Telangana', N'Hyderabad')
- INSERT [dbo].[UserDetails] VALUES (23, N'Suresh', N'Sura', N'[email protected]', N'9485034934', N'pune', N'346344 ', N'TSC', N'Male', N'India', N'Karnatak', N'Banglore')
- INSERT [dbo].[UserDetails] VALUES (24, N'Mona', N'Sharma', N'[email protected]', N'0982349335', N'Pune', N'893484 ', N'HCL', N'Female', N'India', N'Mahrastra', N'Pune')
- INSERT [dbo].[UserDetails] VALUES (25, N'Neha', N'Bharti', N'[email protected]', N'2495304444', N'Chennai', N'938459 ', N'Bipro', N'Female', N'India', N'TamilNadu', N'Chennai')
- SET IDENTITY_INSERT [dbo].[UserDetails] OFF
Note
You can choose the size of the columns according to your requirement.
You can choose the size of the columns according to your requirement.
Step 2 - Create a Web API Project
Now, we will create a Web API with the functionality of binding records from a database. Go to Visual Studio >> File >> New >> Project, and select Web Application. after that, click OK and you will see the templates. Select the Web API template.
Now, we will create a Web API with the functionality of binding records from a database. Go to Visual Studio >> File >> New >> Project, and select Web Application. after that, click OK and you will see the templates. Select the Web API template.

Click OK.

Click OK.
Step 3 - Add ADO.NET Entity Data Model
Now, select the Models folder. Right-click on it and go to Add >> New Item >> select Data in left panel >> ADO.NET Entity Data Model.

Click "Add".

Click the "Next" button.

Give server name of SQL Server and its credentials. Then, select the database and test connection. Click OK.

Click the "Next" button.

Select tables and click the "Finish" button.
Let us see our table entity.

Step 4 - Add API controller logic
Go to the Controller folder in the API Application and right-click >> Add >> Controller >> Select Web API 2 Controller-Empty.

Click the "Add" button.
Now, we will write the logic for performing the CRUD operation. We will go to the Controller class and set the routing to make it more user-friendly by writing the below code.
- using System;
- using System.Linq;
- using System.Web.Http;
- using ReactCRUDApi.Models;
- namespace ReactCRUDApi.Controllers
- {
- [RoutePrefix("Api/User")]
- public class UserController : ApiController
- {
- ReactDBEntities objEntity = new ReactDBEntities();
- [HttpGet]
- [Route("GetUserDetails")]
- public IQueryable<UserDetail> GetEmaployee()
- {
- try
- {
- return objEntity.UserDetails;
- }
- catch (Exception)
- {
- throw;
- }
- }
- [HttpGet]
- [Route("GetUserDetailsById/{userId}")]
- public IHttpActionResult GetUserById(string userId)
- {
- UserDetail objUser = new UserDetail();
- if (userId != "undefined")
- {
- int ID = Convert.ToInt32(userId);
- try
- {
- objUser = objEntity.UserDetails.Find(ID);
- if (objUser == null)
- {
- return NotFound();
- }
- }
- catch (Exception)
- {
- throw;
- }
- }
- return Ok(objUser);
- }
- }
- }
Now, our API has been completed and as you may see from the above code, it has the functionality to GetAll and GetById records to the table.
Step 5 - Create and Install React.js
Now, we will create a React project through the below command. But before that, just check if Node and NPM are installed or not. And also, since we are using Visual Studio Code for writing Angular code for UI application, so first, make sure it is installed. If you have not installed the VS Code, then go to this link for download.
Let's create a React project. Open a new terminal and run the following command to install and create a React project.
npx create-react-app modelexample

React project has been created successfully.
Step 6 - Set Visual Studio Code for React code
Open Visual Studio Code and open the project inside it.

Select the project folder.


Step 7 - Check React dependency









Join the conversation! Your thoughts help the community grow.