Header add

In this article we will discuss Connect with SQL Server Database without Entity Framework in ASP.NET Core.

Here we will explain how to use ADO.Net to connect with SQL Server Database in ASP.Net Core.

Before start this article, please visit our previous article Using ConfigurationManager.AppSettings in .Net Core


The below is the table design of Books followed by dummy data.

Let's create a model class inside Models folder. The following Model class has two properties BookId and BookName.

Connection String

Let's add connection string details in appsettings.json file like below.

The Controller consists of two Action methods.
Action method for handling GET operation
  • Inside this Action method, the GetBookList method is called. Inside the GetBookList  method, the records from the Books table are fetched from Database Table through a DataReader and generic list collection of BookModel class is populated.
To get the connection string from appsettings.json file we use here IConfiguration.
  • Then generic list collection of BookModel class objects is copied to SelectList class object which is used for populating DropDownList in .Net Core.
  • Finally, the SelectList class object is sent to the View.
Action method for handling POST operation
  • This Action method handles the call made from the POST function from the View.
  • When the Form is submitted, the Text and Value of the selected DropDownList Item are captured through the two parameters i.e. BookId and BookName.
  • The values of BookId and BookName are fetched and are set into a ViewBag object which will be later displayed in View using JavaScript Alert Message Box.
  • Also after post back the result we call the select list to populate again


The View consists of an HTML Form with following ASP.Net Tag Helpers attributes.

asp-action – Name of the Action. In this case the name is Index.
asp-controller – Name of the Controller. In this case the name is Home.
method – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
  • The Form consists of a DropDownList, a Hidden Field and a Submit Button.
  • The Model data has been assigned to the DropDownList using the asp-items Tag Helpers attribute;
  • The DropDownList has been assigned a jQuery OnChange event handler, when an item is selected in the DropDownList, the Text of the selected item is copied in the Hidden Field.
  • When the Submit Button is clicked, the Form gets submitted and the BookId and BookName values are sent to the Controller.
Finally, the BookId and BookName values of the selected Book are displayed using JavaScript Alert Message Box.


The final output screen should like below.

Post a Comment

Previous Post Next Post