Friday, 4 August 2017

How to write Stored Procedure for Insert Data into SQL Server by ASP.Net ?

Stored Procedure is block of SQL Statements or Queries which has some name and it is precompiled code and saved into the database.
Create procedure is used to create the procedure.
Stored procedure for inserting data is given below.

create procedure sp_Insert_StudentRegistration
(@firstname nvarchar(max),
@lastname nvarchar(max),
@MobileNumber nvarchar(50),
@GenderID int,
@DateOfBirth date,
@Email nvarchar(50),
@UserName nvarchar(50),
@Password nvarchar(50)
)
as
Begin
Begin Try
Begin Transaction
INSERT INTO StduentRegistration_tbl(FirstName,LastName,UserName,Password,EmailID,MobileNumber,DOB,GenderID)
Values(@firstname,@lastname,@UserName,@Password,@Email,@MobileNumber,@DateOfBirth,@GenderID)
commit transaction
End Try
Begin Catch
RollBack Transaction
select
ERROR_LINE() as errorline,
ERROR_MESSAGE() as errormessage
End Catch
End


On the above code sp_Insert_StudentRegistration is the stored procedure name.
we use create procedure command for creating a stored procedure.
@firstname nvarchar(max),
@lastname nvarchar(max),
@MobileNumber nvarchar(50),
@GenderID int,
@DateOfBirth date,
@Email nvarchar(50),
@UserName nvarchar(50),
@Password nvarchar(50)
are the parameters , in which we can  get the values from ASP.Net application.. Here @ is compulsory before the Parameter name and type should be exactly same with table datatypes.
name of parameters can be different from columns but datatypes should be same.

Begin   : Stored procedure is begin from here
End : Sp ends here

Begin Try
end Try  
Begin Catch
End catch  are the exception handling in Database.


No comments:

Post a Comment

Creating First WEB API Project from scratch ?

  For creating Web API project open your Visual Studio Editor and Create New Project as shown in below figure. Now you have to select  ASP.N...