Insert, Update, Delete and Search by Linq with User Store Procedure
There are very simple steps for Insert, Update, Delete and Search by Linq with User Store Procedure in ASP.NET
Step 1:
We are using SQL SERVER database, so we create table tblStudent and four user store procedure
Step 2:
Create application and give name LinqTestStoreProcedure as shown below:-
Step 3:
Now, Go to Solution Explorer and Right Click on LinqTestStoreProcedure -->Add-->Add New Item-->Dialog Box open and Select Webform and give name Default.aspx as shown in below:-
Step 4:
Now, Go to Solution Explorer and Right Click on LinqTestStoreProcedure -->Add-->Add New Item-->Dialog Box open and Select Linq to SQL SERVER and give name LinqTestStoreProcedureDataClasses as shown in below:-
Step 5:
Now, Go to Server Explorer-->Stored Procedures and Drag and drop four Stored Procedures to LinqTestStoreProcedureDataClasses.dbml file as shown in below:-
Step 6:
Now, Go to Default.aspx page and write the following code:-
Step 6:
Now, Go to Default.aspx.cs page and write the following code:-
Now, run the application and perform operations.
Step 1:
We are using SQL SERVER database, so we create table tblStudent and four user store procedure
CREATE DATABASE dbSantoshTest use dbSantoshTest CREATE TABLE tblStudent ( Name varachar(100), Rollno int Primary key, Branch varchar(50) ) --Create User Store Procedure ---InserttblStudent--- CREATE PROC USP_InserttblStudent @Name varchar(100),@Rollno int, @Branch varchar(50),@Status varchar(50) output AS BEGIN begin try begin tran Insert into tblStudent values(@Name,@Rollno,@Branch) set @Status='Student record has been inserted!' commit tran end try begin catch set @Status='Student record has not been inserted!' rollback end catch END ----UpdateStudent---- CREATE PROC USP_UpdateStudent @Name varchar(100),@Rollno int,@Branch varchar(50),@Status varchar(50) output AS BEGIN begin try begin tran Update tblStudent set Name=@Name,Branch=@Branch where Rollno=@Rollno set @Status='Student record has been updated!.' commit tran end try begin catch set @Status='Student record has not been updated!.' rollback end catch END ----DeleteStudent------ CREATE PROC USP_DeleteStudent @Rollno int,@Status varchar(50) output AS BEGIN begin try begin tran Delete from tblStudent where Rollno=@Rollno set @Status='Student record has been deleted!.' commit tran end try begin catch set @Status='Student record has not been deleted!.' rollback end catch END -----GetStudent----- CREATE PROC USP_GetStudent AS BEGIN Select * from tblStudent END
Step 2:
Create application and give name LinqTestStoreProcedure as shown below:-
Step 3:
Now, Go to Solution Explorer and Right Click on LinqTestStoreProcedure -->Add-->Add New Item-->Dialog Box open and Select Webform and give name Default.aspx as shown in below:-
Step 4:
Now, Go to Solution Explorer and Right Click on LinqTestStoreProcedure -->Add-->Add New Item-->Dialog Box open and Select Linq to SQL SERVER and give name LinqTestStoreProcedureDataClasses as shown in below:-
Step 5:
Now, Go to Server Explorer-->Stored Procedures and Drag and drop four Stored Procedures to LinqTestStoreProcedureDataClasses.dbml file as shown in below:-
Step 6:
Now, Go to Default.aspx page and write the following code:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
Step 6:
Now, Go to Default.aspx.cs page and write the following code:-
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Drawing; public partial class _Default : System.Web.UI.Page { string Status; protected void Page_Load(object sender, EventArgs e) { } //check text box field is empty code public bool validate() { lblMessage.ForeColor = Color.Red; if (txtName.Text == "") { lblMessage.Text = "Name field should not be blank."; return false; } if (txtRollno.Text == "") { lblMessage.Text = "Roll number field should not be blank."; return false; } if (txtBranch.Text == "") { lblMessage.Text = "Branch field should not be blank."; return false; } return true; } //Insert code private void Insert() { try { LinqTestStoreProcedureDataClassesDataContext objDC = new LinqTestStoreProcedureDataClassesDataContext(); objDC.USP_InserttblStudent(txtName.Text, Convert.ToInt32(txtRollno.Text), txtBranch.Text, ref Status); lblMessage.Text = Status; } catch (Exception ex) { lblMessage.ForeColor = Color.Red; lblMessage.Text = ex.Message; } } //Update code private void Update() { try { LinqTestStoreProcedureDataClassesDataContext objDC = new LinqTestStoreProcedureDataClassesDataContext(); objDC.USP_UpdateStudent(txtName.Text, Convert.ToInt32(txtRollno.Text), txtBranch.Text, ref Status); lblMessage.Text = Status; } catch (Exception ex) { lblMessage.ForeColor = Color.Red; lblMessage.Text = ex.Message; } } //Delete code private void Delete() { try { LinqTestStoreProcedureDataClassesDataContext objDC = new LinqTestStoreProcedureDataClassesDataContext(); objDC.USP_DeleteStudent(Convert.ToInt32(txtRollno.Text), ref Status); lblMessage.Text = Status; } catch (Exception ex) { lblMessage.ForeColor = Color.Red; lblMessage.Text = ex.Message; } } //GetStudent code private void GetStudentRecord() { LinqTestStoreProcedureDataClassesDataContext objDC = new LinqTestStoreProcedureDataClassesDataContext(); gvDetail.DataSource = objDC.USP_GetStudent(); gvDetail.DataBind(); } //Clear textbox code protected void Clear() { txtName.Text = ""; txtRollno.Text = ""; txtBranch.Text = ""; } protected void btnInsert_Click(object sender, EventArgs e) { if (validate() == false) { return; } Insert(); GetStudentRecord(); Clear(); } protected void btnUpdate_Click(object sender, EventArgs e) { if (validate() == false) { return; } Update(); GetStudentRecord(); Clear(); } protected void btnDelete_Click(object sender, EventArgs e) { if (txtRollno.Text == "") { lblMessage.ForeColor = Color.Red; lblMessage.Text = "Roll number field should not be blank."; return; } Delete(); GetStudentRecord(); Clear(); } protected void btnSearch_Click(object sender, EventArgs e) { GetStudentRecord(); } protected void btnClear_Click(object sender, EventArgs e) { Clear(); } }
Now, run the application and perform operations.