Skip to main content

Featured Post

How to use Tabs in ASP.NET CORE

I want to show Components in a tabs , so first of all create few components. In this project we have three components, First View Component  public class AllViewComponent : ViewComponent     {         private readonly UserManager<ApplicationUser> _userManager;         public AllViewComponent(UserManager<ApplicationUser> userManager)         {             _userManager = userManager;         }         public async Task<IViewComponentResult> InvokeAsync()         {             List<StudentViewModel> allUsers = new List<StudentViewModel>();             var items = await _userManager.Users.ToListAsync();             foreach (var item in items)             {                 allUsers.Add(new StudentViewModel {Id=item.Id, EnrollmentNo = item.EnrollmentNo, FatherName = item.FatherName, Name = item.Name, Age = item.Age, Birthdate = item.Birthdate, Address = item.Address, Gender = item.Gender, Email = item.Email });             }            

Create Stored Procedure for accessing data from database in ASP.NET, Example

In my previous post we have discussed about how to design Department table for shopping cart. Now, we will learn, How to create stored procedure for retrieving  information from database table. You need to create the GetDepartments stored procedure, which returns department information from the Department table. This stored procedure is part of the data tier and will be accessed from the business tier. The final goal is to have this data displayed in the user control.

The SQL code that retrieves the necessary data and that you need to save to the database as the GetDepartments stored procedure is the following:

Select DepartmentID, Name, Description From Department

This command returns all the department information

Note: unless you have a specific reason to do so, never ask for all columns (using the * wildcard) when you only need a part of them. This generate more traffic and stress on the database server then necessary and slows down performance. Moreover, even if you do need to ask for all columns in the table, it's safer to mention them explicitly to protect your application in case the number of order of columns changes in future.

Saving the Query As a Stored Procedure

As with data tables, after you know the structure, implementing the stored procedure is a piece of cake. Now that you know the SQL code, the tools will help you save the query as a stored procedure easily.

The Syntax for creating a stored procedure that has no input or output parameters is as follows:

CREATE PROCEDURE <procedure name>
AS
<stored procedure code>

if the procedure already exists and you just want to update its code, use alter PROCEDURE instead of above. Stored procedures can have input or output parameters. Because GetDepartments doesn't have any parameters, you don't have to bother about them right now.

Lets take a simple example of writing the Stored Procedure

Step-1: Make sure the data connection to the database is expanded and selected in server explorer. Choose data->Add New--> Stored Procedure. Alternatively, you can right-click the Stored Procedure node in server explorer and select Add New Stored Procedure.

Step-2: Replace the default text with GetDepartment's stored procedure

CREATE PROCEDURE GetDepartments 

AS
SELECT DepartmentID, Name, Description
From Department

Step-3: Press Ctrl+S to save the stored procedure. Unlike with the tables, you won't be asked for a name because the database already knows that you're talking about the GetDepartment's stored procedure

Note: Saving the Stored Procedure actually executes the SQL code you entered, which creates the stored procedure in the database, after saving the procedure, the CREATE keyword becomes ALTER, which is the SQL command that changes the code of an existing procedure.

Step-4: Now test your first stored procedure to see that it's actually working. Navigate to the GetDepartments stored procedure node in server explorer and select execute.

Computer Programming : How to execute stored procedure


Step-5: After running the stored procedure, you can see the results in the output window.

Computer Programming : Output window of stored Procedure

Comments

Popular Post

Polynomial representation using Linked List for Data Structure in 'C'

Polynomial representation using Linked List The linked list can be used to represent a polynomial of any degree. Simply the information field is changed according to the number of variables used in the polynomial. If a single variable is used in the polynomial the information field of the node contains two parts: one for coefficient of variable and the other for degree of variable. Let us consider an example to represent a polynomial using linked list as follows: Polynomial:      3x 3 -4x 2 +2x-9 Linked List: In the above linked list, the external pointer ‘ROOT’ point to the first node of the linked list. The first node of the linked list contains the information about the variable with the highest degree. The first node points to the next node with next lowest degree of the variable. Representation of a polynomial using the linked list is beneficial when the operations on the polynomial like addition and subtractions are performed. The resulting polynomial can also

How to use Tabs in ASP.NET CORE

I want to show Components in a tabs , so first of all create few components. In this project we have three components, First View Component  public class AllViewComponent : ViewComponent     {         private readonly UserManager<ApplicationUser> _userManager;         public AllViewComponent(UserManager<ApplicationUser> userManager)         {             _userManager = userManager;         }         public async Task<IViewComponentResult> InvokeAsync()         {             List<StudentViewModel> allUsers = new List<StudentViewModel>();             var items = await _userManager.Users.ToListAsync();             foreach (var item in items)             {                 allUsers.Add(new StudentViewModel {Id=item.Id, EnrollmentNo = item.EnrollmentNo, FatherName = item.FatherName, Name = item.Name, Age = item.Age, Birthdate = item.Birthdate, Address = item.Address, Gender = item.Gender, Email = item.Email });             }            

Memory representation of Linked List Data Structures in C Language

                                 Memory representation of Linked List              In memory the linked list is stored in scattered cells (locations).The memory for each node is allocated dynamically means as and when required. So the Linked List can increase as per the user wish and the size is not fixed, it can vary.                Suppose first node of linked list is allocated with an address 1008. Its graphical representation looks like the figure shown below:       Suppose next node is allocated at an address 506, so the list becomes,   Suppose next node is allocated with an address with an address 10,s the list become, The other way to represent the linked list is as shown below:  In the above representation the data stored in the linked list is “INDIA”, the information part of each node contains one character. The external pointer root points to first node’s address 1005. The link part of the node containing information I contains 1007, the address of