In this article, we will learn CRUD operation in ASP.NET Core 2.0 with Razor pages using ADO.NET step by step.
We will use Visual Studio 2017 to develop the web application using ASP.NET Core 2.0 with Razor pages. If you don’t have a basic idea of ASP.NET Core and how to set up ASP.NET Core Environment, or want to know how to install Visual Studio 2017, please go to my previous article and click
What ASP.NET Core Is And set up ASP.NET Core Environment.
Prerequisites
- Install .NET Core 2.0.0 or above SDK steps to install click on the link .NET Core 2.0.
- Install Visual Studio 2017 and steps to install click on the link Visual Studio 2017.
- SQL Server 2008 or above.
We will create one small "Group Meeting" web application using ASP.NET Core 2.0. as following.
First of all, create a database script using SQL Server.
Scripts 1
To create the database.
- CREATE DATABASE ProjectMeeting
Scripts 2
To create the database table named as “GroupMeeting”.
- create procedure [dbo].[InsertGroupMeeting]
- (
- @ProjectName varchar(50),
- @GroupMeetingLeadName varchar(50),
- @TeamLeadName varchar(50),
- @Description varchar(50),
- @GroupMeetingDate date
- )
- As
- BEGIN
-
- INSERT INTO GroupMeeting(ProjectName,GroupMeetingLeadName,TeamLeadName,Description,GroupMeetingDate)
- VALUES(@ProjectName,@GroupMeetingLeadName,@TeamLeadName,@Description,@GroupMeetingDate)
-
- END
Scripts 6
Create the stored procedure to update the group meeting.
- create procedure [dbo].[UpdateGroupMeeting]
- (
- @Id int,
- @ProjectName varchar(50),
- @GroupMeetingLeadName varchar(50),
- @TeamLeadName varchar(50),
- @Description varchar(50),
- @GroupMeetingDate date
- )
- As
- BEGIN
- UPDATE GroupMeeting
- SET ProjectName =@ProjectName,
- GroupMeetingLeadName =@GroupMeetingLeadName,
- TeamLeadName = @TeamLeadName,
- Description = @Description,
- GroupMeetingDate =@GroupMeetingDate
- Where Id=@Id
- END
Scripts 7
Create the stored procedure to delete the group meeting.
- create procedure [dbo].[DeleteGroupMeeting]
- (
- @Id int
- )
- As
- BEGIN
- DELETE FROM GroupMeeting WHERE Id=@Id
- END
Now, we have completed our database related changes. Let’s go with the code base changes using Visual Studio 2017.
Step 1
Open Visual Studio 2017.
Step 2
Click on File => Open => New Project, as shown in the image.
Step 3
Select .NET Core from the left side and select the ‘ASP.NET Core Web Application’ from the new open project template. Then, provide the a meaningful name like “GroupMeetingASP.NETCoreWebApp”.
Step 4
Select Web Application(MVC) template from the list of templates and click on "OK" button as follows.
Step 5
The default ASP.NET Core MVC structure gets created as follows. The default model, view and controller folders get created with some default pages. So we will keep as it is the default controller and Views. I will add a new controller as per the requirement.
Step 6
Right click on Models => Click on Add => Click on Class.
Step 7
Provide a meaningful name like “GroupMeeting” and click on "Add" button as follow.
Step 8
Write code to create properties for group meeting class as follow. Also, use the required attribute to validate the class fields. Also, we have taken connection string in using string variable OR you can read from appSettings.json file.
- public class GroupMeeting
- {
- public int GroupMeetingId { get; set; }
- [Required(ErrorMessage ="Enter Project Name!")]
- public string ProjectName { get; set; }
-
- [Required(ErrorMessage = "Enter Group Lead Name!")]
- public string GroupMeetingLeadName { get; set; }
- [Required(ErrorMessage = "Enter Team Lead Name!")]
- public string TeamLeadName { get; set; }
- [Required(ErrorMessage = "Enter Description!")]
- public string Description { get; set; }
- [Required(ErrorMessage = "Enter Group Meeting Date!")]
- public DateTime GroupMeetingDate { get; set; }
-
- static string strConnectionString = "User Id=sa;Password=Shri;Server=Shri\\SQL2014;Database=ProjectMeeting;";
-
- }
Step 9
Write code to get all group meeting details from the database using the stored procedure. The method name is "GetGroupMeetings()" and return type is IEnumerable<GroupMeeting> or List<GroupMeeting> you can use either one of them.
- public static IEnumerable<GroupMeeting> GetGroupMeetings()
- {
- List<GroupMeeting> groupMeetingsList = new List<GroupMeeting>();
- using (SqlConnection con = new SqlConnection(strConnectionString))
- {
- SqlCommand command = new SqlCommand("GetGroupMeetingDetails", con);
- command.CommandType = CommandType.StoredProcedure;
- if (con.State == ConnectionState.Closed)
- con.Open();
-
- SqlDataReader dataReader= command.ExecuteReader();
- while (dataReader.Read())
- {
- GroupMeeting groupMeeting = new GroupMeeting();
- groupMeeting.GroupMeetingId = Convert.ToInt32(dataReader["Id"]);
- groupMeeting.ProjectName = dataReader["ProjectName"].ToString();
- groupMeeting.GroupMeetingLeadName = dataReader["GroupMeetingLeadName"].ToString();
- groupMeeting.TeamLeadName = dataReader["TeamLeadName"].ToString();
- groupMeeting.Description = dataReader["Description"].ToString();
- groupMeeting.GroupMeetingDate = Convert.ToDateTime(dataReader["GroupMeetingDate"]);
- groupMeetingsList.Add(groupMeeting);
- }
- }
- return groupMeetingsList;
- }
Write code to get group meeting detail by groupId using ADO.NET as follows.
- public static GroupMeeting GetGroupMeetingById(int? id)
- {
- GroupMeeting groupMeeting = new GroupMeeting();
- if (id == null)
- return groupMeeting;
-
- using (SqlConnection con = new SqlConnection(strConnectionString))
- {
- SqlCommand command = new SqlCommand("GetGroupMeetingByID", con);
- command.CommandType = CommandType.StoredProcedure;
- command.Parameters.AddWithValue("@Id", id);
- if (con.State == ConnectionState.Closed)
- con.Open();
-
- SqlDataReader dataReader = command.ExecuteReader();
- while (dataReader.Read())
- {
- groupMeeting.GroupMeetingId = Convert.ToInt32(dataReader["Id"]);
- groupMeeting.ProjectName = dataReader["ProjectName"].ToString();
- groupMeeting.GroupMeetingLeadName = dataReader["GroupMeetingLeadName"].ToString();
- groupMeeting.TeamLeadName = dataReader["TeamLeadName"].ToString();
- groupMeeting.Description = dataReader["Description"].ToString();
- groupMeeting.GroupMeetingDate = Convert.ToDateTime(dataReader["GroupMeetingDate"]);
- }
- }
- return groupMeeting;
- }
Step 10
Code to insert the group meeting into the database using ADO.NET is below.
- public static int AddGroupMeeting(GroupMeeting groupMeeting)
- {
- int rowAffected = 0;
- using (SqlConnection con = new SqlConnection(strConnectionString))
- {
- SqlCommand command = new SqlCommand("InsertGroupMeeting", con);
- command.CommandType = CommandType.StoredProcedure;
- command.Parameters.AddWithValue("@ProjectName", groupMeeting.ProjectName);
- command.Parameters.AddWithValue("@GroupMeetingLeadName", groupMeeting.GroupMeetingLeadName);
- command.Parameters.AddWithValue("@TeamLeadName", groupMeeting.TeamLeadName);
- command.Parameters.AddWithValue("@Description", groupMeeting.Description);
- command.Parameters.AddWithValue("@GroupMeetingDate", groupMeeting.GroupMeetingDate);
-
- if (con.State == ConnectionState.Closed)
- con.Open();
-
- rowAffected = command.ExecuteNonQuery();
- }
- return rowAffected;
- }
Code to update the group meeting using ADO.NET is below.
- public static int UpdateGroupMeeting(GroupMeeting groupMeeting)
- {
- int rowAffected = 0;
- using (SqlConnection con = new SqlConnection(strConnectionString))
- {
- SqlCommand command = new SqlCommand("UpdateGroupMeeting", con);
- command.CommandType = CommandType.StoredProcedure;
- command.Parameters.AddWithValue("@Id", groupMeeting.GroupMeetingId);
- command.Parameters.AddWithValue("@ProjectName", groupMeeting.ProjectName);
- command.Parameters.AddWithValue("@GroupMeetingLeadName", groupMeeting.GroupMeetingLeadName);
- command.Parameters.AddWithValue("@TeamLeadName", groupMeeting.TeamLeadName);
- command.Parameters.AddWithValue("@Description", groupMeeting.Description);
- command.Parameters.AddWithValue("@GroupMeetingDate", groupMeeting.GroupMeetingDate);
-
- if (con.State == ConnectionState.Closed)
- con.Open();
-
- rowAffected = command.ExecuteNonQuery();
- }
- return rowAffected;
- }
Code to delete the group meeting using ADO.NET is below.
- public static int DeleteGroupMeeting(int id)
- {
- int rowAffected = 0;
- using (SqlConnection con = new SqlConnection(strConnectionString))
- {
- SqlCommand command = new SqlCommand("DeleteGroupMeeting", con);
- command.CommandType = CommandType.StoredProcedure;
- command.Parameters.AddWithValue("@Id", id);
-
- if (con.State == ConnectionState.Closed)
- con.Open();
-
- rowAffected = command.ExecuteNonQuery();
- }
- return rowAffected;
- }
As of now, we have completed the model related changes Get, Insert, Update, Delete as shown in the above code. After that, we will create a new controller and Razor Views.
Step 11
Right click on Controller folder => Click on “Add” => Click on Controller.
Step 12
Select "MVC Controller - Empty" and click on the "Add" button.
Provide a meaningful name like “GroupMeeting” as follow.
Step 13
After adding the controller, you need to import the required namespace. Then add the following code to get all group meeting details and pass to the view as follows.