Here I am explaining GridView CRUD Operations using N-Tier Architecture.
First here I am explaining databases, table parameters, and Stored Procedures to Create, Read, Update and Delete Operations.
- Use [GridData]
- CREATE TABLE [dbo].[OperatingSystem](
- [OSId] [int] IDENTITY(1,1) NOT NULL,
- [OSName] [varchar](100) NULL,
- [CreateDate] [datetime] NULL,
- [Status] [smallint] NULL,
- CONSTRAINT [PK_OperatingSystem] PRIMARY KEY CLUSTERED
- (
- [OSId] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
- GO
- SET ANSI_PADDING OFF
- GO
- ALTER TABLE [dbo].[OperatingSystem] ADD DEFAULT (getdate()) FOR [CreateDate]
- GO
- ALTER TABLE [dbo].[OperatingSystem] ADD DEFAULT ('1') FOR [Status]
- GO
- Stored Procedures are,
- //Create Procedure
- Create Procedure [dbo].[InsertSystemName]
- (
- @OSName varchar(50)
- )
- AS
- BEGIN
- insert into OperatingSystem(OSName) values(@OSName)
- END
- //Get Data Procedure
- ALTER Procedure [dbo].[GetSystemsData]
- As
- Begin
- select * from OperatingSystem where Status='1'
- End
- //Update Procedure
- ALTER Procedure [dbo].[UpdateSystems]
- (
- @Id int,
- @Name varchar(100),
- @Status int
- )
- As
- BEGIN
- update OperatingSystem set OSName=@Name,Status=@Status where OSId=@Id
- END
- //Delete Procedure
- ALTER Procedure [dbo].[DeleteSystemsData]
- (
- @Id int
- )
- As
- Begin
- update OperatingSystem set Status='0' where OSId=@Id
- End
- Next create the solution from Visual Studio. First create a solution add three class libraries named "DAL", "Databaselayer", "EntityLayer" and one web application named "GridViewExample" as ini the following image.
- Next add refernces for the solution. First DAL for the DatabaseLayer and EntityLayer.
- For the GridViewExample add "DAL", "Databaselayer" and "Entitylayer".
- In the DatabaseLayer, add one refernce System.Configuration.
Getting to the Databaselayer, create a classs named "SqlHelper.CS" and write the following code.
Note: Here dbconnection is a connection string for SQL Server.
- public static string CONNECTION_STRING = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
- public static DataSet ExecuteParamerizedSelectCommand(string CommandName, CommandType cmdType, SqlParameter[] param) {
- DataSet ds = new DataSet();
- using(SqlConnection con = new SqlConnection(CONNECTION_STRING)) {
- using(SqlCommand cmd = con.CreateCommand()) {
- cmd.CommandType = cmdType;
- cmd.CommandText = CommandName;
- cmd.Parameters.AddRange(param);
- try {
- if (con.State != ConnectionState.Open) {
- con.Open();
- }
- using(SqlDataAdapter da = new SqlDataAdapter(cmd)) {
- da.Fill(ds);
- }
- } catch {
- throw;
- }
- }
- }
- return ds;
- }
- // This function will be used to execute CUD(CRUD) operation of parameterized commands
- public static bool ExecuteNonQuery(string CommandName, CommandType cmdType, SqlParameter[] pars) {
- int result = 0;
- using(SqlConnection con = new SqlConnection(CONNECTION_STRING)) {
- using(SqlCommand cmd = con.CreateCommand()) {
- cmd.CommandType = cmdType;
- cmd.CommandText = CommandName;
- cmd.Parameters.AddRange(pars);
- try {
- if (con.State != ConnectionState.Open) {
- con.Open();
- }
- result = cmd.ExecuteNonQuery();
- } catch {
- throw;
- }
- }
- }
- return (result > 0);
- }
- //Getting To EntityLayer, add a class and named as GridData.CS and write following code:
- public class OperatingSystemEntity {
- public int OSId {
- get;
- set;
- }
- public string OSName {
- get;
- set;
- }
- public DateTime CreateDate {
- get;
- set;
- }
- public int Status {
- get;
- set;
- }
- }
- //Getting to DAL, Add a class and named as GridData.cs and write the following code:
- public class GridData {
- public bool CreateSystem(OperatingSystemEntity SEntity) {
- SqlParameter[] parameters = new SqlParameter[] {
- new SqlParameter("@OSName", SEntity.OSName),
- };
- return SqlHelper.ExecuteNonQuery("InsertSystemName", CommandType.StoredProcedure, parameters);
- }
- public List < OperatingSystemEntity > GetSystemsData(OperatingSystemEntity SEntity) {
- List < OperatingSystemEntity > ListEntry = null;
- SqlParameter[] parameters = new SqlParameter[] {
- };
- using(DataSet ds = SqlHelper.ExecuteParamerizedSelectCommand("GetSystemsData", CommandType.StoredProcedure, parameters)) {
- if (ds.Tables.Count > 0) {
- ListEntry = new List < OperatingSystemEntity > ();
- foreach(DataRow row2 in ds.Tables[0].Rows) {
- OperatingSystemEntity entry = new OperatingSystemEntity();
- entry.OSId = Convert.ToInt32(row2["OSId"].ToString());
- entry.OSName = row2["OSName"].ToString();
- entry.CreateDate = Convert.ToDateTime(row2["CreateDate"].ToString());
- entry.Status = Convert.ToInt32(row2["Status"].ToString());
- ListEntry.Add(entry);
- }
- }
- }
- return ListEntry;
- }
- public bool UpdateSystems(OperatingSystemEntity SEntity) {
- SqlParameter[] parameters = new SqlParameter[] {
- new SqlParameter("@Id", SEntity.OSId),
- new SqlParameter("@Name", SEntity.OSName),
- new SqlParameter("@Status", SEntity.Status),
- };
- return SqlHelper.ExecuteNonQuery("UpdateSystems", CommandType.StoredProcedure, parameters);
- }
- public bool DeleteSystem(OperatingSystemEntity SEntity) {
- SqlParameter[] parameters = new SqlParameter[] {
- new SqlParameter("@Id", SEntity.OSId),
- };
- return SqlHelper.ExecuteNonQuery("DeleteSystemsData", CommandType.StoredProcedure, parameters);
- }
- }
- }
Then the Solution Explorer will be such as follows:

Getting to GridViewExample Web Application, add a new form named NewGrid.aspx and add the following code:


Bala Bhaskara CharyPosted Jul 10, 2015, 3:38 AM
nice