ASP.Net 4.6 CRUD For MySQL Using SP and Helper Class

This article explains in detail how to insert/update/delete and select data from a MySQL database using a helper class and Stored Procedure using ASP.NET 4.6.

Introduction



This article explains in detail how to insert/update/delete and select data from a MySQL database using a helper class and Stored Procedure using ASP.Net 4.6.

Prerequisites

Visual Studio 2015: You can download it from Visual Studio (in my example I have used Visual Studio Community 2015 RC).

MySQL: The reason why I have used MySQL is it's open source. For study and small project purposes we can use MySQL. You can download MySQL from their website.

Download MySQL Installer

I have used mysql-installer-community-5.6.24.0 (Windows Installer).

You also need to download and install MySQL Connector for .NET that is available here:

Download Connector/Net

In this article, we will see the following.

  • Introduction to ASP.NET 5.
  • Create Table and Stored Procedure in MySQL Database.
  • Creating MySQL helper Class for ASP.NET 5.
  • Create a simple Webform website for performing CRUD operation for MySQL Database.

Introduction to ASP.NET 5

I began working with Visual Studio 2003 (V1.1) and today we have reached Visual Studio 2015 (V4.6). Working with web projects will always make us energetic. Let's see few important and new features available in ASP.NET 5.

ASP.NET 5 is:

  • Cross-platform (Windows, Mac and Linux)
  • Faster Development Cycle
  • Open Source
  • Easy to create cloud-based web applications

To learn more in detail about ASP.NET 5, refer to the following links.

Using the Code

Create Table in MySQL: Let us see how to write a query for creating a table and inserting a sample record.

After MySQL has been installed, open it.



Click on the local instance of MySQL, then enter your MySQL password and press Enter.



After successful login to MySQL, we can see an editor where we can write our queries or create a Stored Procedure and so on.



Creating ItemMaster Table: let's create a simple ItemMaster table. This table will be used to do all our CRUD operations from our ASP.NET 5 web application.
First we create a database and we create our ItemMaster table inside our InventoryManagement database.

  1. Create Database InvnetoryManagement  
  2. CREATE DATABASE InvnetoryManagement;  
In the editor, select the query to be executed and press the symbol from the editor. For example now if I want to select my database then I will write the query.

Use Database
  1. use InvnetoryManagement;  


Here we can see I have selected the query "use Inventorymanagement" and then press the something. In the output window we can see the result is success with the Green icon. If it has an error then it will display the error icon.

In the same way next is our create table query. We write our query and execute it similarly.

Create table Item Master

  1. CREATE TABLE ItemMaster   
  2. (  
  3. Item_Code int NOT NULL AUTO_INCREMENT,  
  4. Item_Name varchar(100) NOT NULL,  
  5. Price int,  
  6. TAX1 int,  
  7. Description varchar(100),  
  8. IN_DATE datetime,  
  9. IN_USR_ID varchar(50),  
  10. DeleteStatus varchar(10),  
  11. PRIMARY KEY (Item_Code)  
  12. );  
Now let's insert some sample records using the insert query.

Insert sample Record to Univ Master

insert into ItemMaster(Item_Name,Price,TAX1,Description,IN_DATE,IN_USR_ID,DeleteStatus) values ('headPhone',600,2,'head Phone',now(),'SHANU','N');
insert into ItemMaster(Item_Name,Price,TAX1,Description,IN_DATE,IN_USR_ID,DeleteStatus) values ('Mouse',30,0,'Mousee',now(),'SHANU','N');

Test Select Statement
  1. select * from ItemMaster;  
Execute all the queries one-by-one and test with a select query.

Create our First ASP.NET 5 Web Application

After installing our Visual Studio 2015, click Start then select Programs then select Visual Studio 2015. Click Visual Studio 2015 RC.



Click New -> Project then select Web -> ASP.NET Web Application. Select your project's location and enter your web application name.



Select Web Forms and click OK. Since we are developing our ASP.NET web form here we select Web Forms.



After creating our website we can create a class file for our “MySQLHelper” class and “BizLogic” class.



I have created both classes inside a folder as in the following.

First we need to add the “Mysql.Data.dll” file to our reference.

There are two ways to add the DLL.

The same as before, we use Visual Studio 2010 or 2008. Add a reference and select Mysql.Data.dll.



Another method is by adding from Manage NuGet packages.



Search for MySQL and click Install. It will add the MySQL.Data.dll to your reference.



“ShanuMySqlHelper.CS” this is the class that I used to create a helper class to connect to a MySQL database and do all ExecuteQuery, ExecuteNonQuery, return DataSet and DataTable. In this class I created a separate function for ExecuteNonQuery, ExecuteQuery, return DataSet, Execute by Query, Execute by Stored Procedure and so on.

For example, here we can see a simple method that will be executed by a query and do the insert/update and delete actions using the ExecuteNonQuery method. To this method I will pass the query from my web application to our businsess classs and from the business class I will pass the query to this MySQL Helper class.

  1. #region ExecuteNonQuery  
  2. for insert / Update and Delete  
  3. //For Insert/Update/Delete  
  4. public int ExecuteNonQuery_IUD(String Querys) {  
  5.     int result = 0;  
  6.     //open connection  
  7.     if (OpenConnection() == true) {  
  8.         //create command and assign the query and connection from the constructor  
  9.         MySqlCommand cmd = new MySqlCommand(Querys, connection);  
  10.         //Execute command  
  11.         result = cmd.ExecuteNonQuery();  
  12.         //close connection  
  13.         CloseConnection();  
  14.     }  
  15.     return result;  
  16. }#endregion  

Here is another example to execute the Stored Procedure and return the result as the Dataset.

  1. #region Dataset  
  2. for Stored Procedure and  
  3. return as DataTable  
  4. //for select result and return as DataTable  
  5. public DataSet SP_DataTable_return(String ProcName, params MySqlParameter[] commandParameters) {  
  6.     DataSet ds = new DataSet();  
  7.     //open connection  
  8.     if (OpenConnection() == true)   
  9.     {  
  10.         //for Select Query   
  11.         MySqlCommand cmdSel = new MySqlCommand(ProcName, connection);  
  12.         cmdSel.CommandType = CommandType.StoredProcedure;  
  13.         // Assign the provided values to these parameters based on parameter order  
  14.         AssignParameterValues(commandParameters, commandParameters);  
  15.         AttachParameters(cmdSel, commandParameters);  
  16.         MySqlDataAdapter da = new MySqlDataAdapter(cmdSel);  
  17.         da.Fill(ds);  
  18.         //close connection  
  19.         CloseConnection();  
  20.     }  
  21.     return ds;  
  22. }  

Complete helper class

The following is my complete helper class.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using MySql.Data;  
  6. using MySql.Data.MySqlClient;  
  7. using MySql.Data.Types;  
  8. using System.Configuration;  
  9. using System.Data;  
  10. /// <summary>  
  11. /// Author : Shanu  
  12. /// Create date : 2015-05-09  
  13. /// Description : Biz Class  
  14. /// Latest  
  15. /// Modifier :   
  16. /// Modify date :   
  17. /// </summary>  
  18. namespace ShanuVS2015.DBClass.shanuMYSQLHelper {  
  19.     public class shanuMySqlHelper {  
  20.         public String ConnectionString = ConfigurationManager.ConnectionStrings["shanu"].ToString();  
  21.         public MySqlConnection connection;  
  22.         #region Initiallize  
  23.         public shanuMySqlHelper() {  
  24.             Initialize();  
  25.         }  
  26.         //Initialize values  
  27.         private void Initialize() {  
  28.             ConnectionString = ReadConnectionString();  
  29.             connection = new MySqlConnection(ConnectionString);  
  30.         }  
  31.         public String ReadConnectionString() {  
  32.             return ConnectionString = ConfigurationManager.ConnectionStrings["shanu"].ToString();  
  33.         }  
  34.         #endregion  
  35.         #region DB ConnectionOpen  
  36.         public bool OpenConnection() {  
  37.             try {  
  38.                 connection.Open();  
  39.                 return true;  
  40.             } catch (MySqlException ex) {  
  41.             }  
  42.             return false;  
  43.         }#endregion  
  44.         #region DB Connection Close  
  45.         //Close connection  
  46.         public bool CloseConnection() {  
  47.             try {  
  48.                 connection.Close();  
  49.                 return true;  
  50.             } catch (MySqlException ex) {  
  51.                 return false;  
  52.             }  
  53.         }  
  54.         #endregion  
  55.         #region ExecuteNonQuery  
  56.         for insert / Update and Delete  
  57.         //For Insert/Update/Delete  
  58.         public int ExecuteNonQuery_IUD(String Querys) {  
  59.             int result = 0;  
  60.             //open connection  
  61.             if (OpenConnection() == true) {  
  62.                 //create command and assign the query and connection from the constructor  
  63.                 MySqlCommand cmd = new MySqlCommand(Querys, connection);  
  64.                 //Execute command  
  65.                 result = cmd.ExecuteNonQuery();  
  66.                 //close connection  
  67.                 CloseConnection();  
  68.             }  
  69.             return result;  
  70.         }#endregion  
  71.         #region Dataset  
  72.         for select result and  
  73.         return as Dataset  
  74.         //for select result and return as Dataset  
  75.         public DataSet DataSet_return(String Querys) {  
  76.             DataSet ds = new DataSet();  
  77.             //open connection  
  78.             if (OpenConnection() == true) {  
  79.                 //for Select Query   
  80.                 MySqlCommand cmdSel = new MySqlCommand(Querys, connection);  
  81.                 MySqlDataAdapter da = new MySqlDataAdapter(cmdSel);  
  82.                 da.Fill(ds);  
  83.                 //close connection  
  84.                 CloseConnection();  
  85.             }  
  86.             return ds;  
  87.         }#endregion  
  88.         #region DataTable  
  89.         for select result and  
  90.         return as DataTable  
  91.         //for select result and return as DataTable  
  92.         public DataTable DataTable_return(String Querys) {  
  93.             DataTable dt = new DataTable();  
  94.             //open connection  
  95.             if (OpenConnection() == true) {  
  96.                 //for Select Query   
  97.                 MySqlCommand cmdSel = new MySqlCommand(Querys, connection);  
  98.                 MySqlDataAdapter da = new MySqlDataAdapter(cmdSel);  
  99.                 da.Fill(dt);  
  100.                 //close connection  
  101.                 CloseConnection();  
  102.             }  
  103.             return dt;  
  104.         }#endregion  
  105.         #region Dataset  
  106.         for Stored Procedure and  
  107.         return as DataTable  
  108.         //for select result and return as DataTable  
  109.         public DataSet SP_DataTable_return(String ProcName, params MySqlParameter[] commandParameters) {  
  110.             DataSet ds = new DataSet();  
  111.             //open connection  
  112.             if (OpenConnection() == true) {  
  113.                 //for Select Query   
  114.                 MySqlCommand cmdSel = new MySqlCommand(ProcName, connection);  
  115.                 cmdSel.CommandType = CommandType.StoredProcedure;  
  116.                 // Assign the provided values to these parameters based on parameter order  
  117.                 AssignParameterValues(commandParameters, commandParameters);  
  118.                 AttachParameters(cmdSel, commandParameters);  
  119.                 MySqlDataAdapter da = new MySqlDataAdapter(cmdSel);  
  120.                 da.Fill(ds);  
  121.                 //close connection  
  122.                 CloseConnection();  
  123.             }  
  124.             return ds;  
  125.         }  
  126.         private static void AttachParameters(MySqlCommand command, MySqlParameter[] commandParameters) {  
  127.             if (command == nullthrow new ArgumentNullException("command");  
  128.             if (commandParameters != null) {  
  129.                 foreach(MySqlParameter p in commandParameters) {  
  130.                     if (p != null) {  
  131.                         // Check for derived output value with no value assigned  
  132.                         if ((p.Direction == ParameterDirection.InputOutput || p.Direction == ParameterDirection.Input) && (p.Value == null)) {  
  133.                             p.Value = DBNull.Value;  
  134.                         }  
  135.                         command.Parameters.Add(p);  
  136.                     }  
  137.                 }  
  138.             }  
  139.         }  
  140.         private static void AssignParameterValues(MySqlParameter[] commandParameters, object[] parameterValues) {  
  141.             if ((commandParameters == null) || (parameterValues == null)) {  
  142.                 // Do nothing if we get no data  
  143.                 return;  
  144.             }  
  145.             // We must have the same number of values as we pave parameters to put them in  
  146.             if (commandParameters.Length != parameterValues.Length) {  
  147.                 throw new ArgumentException("Parameter count does not match Parameter Value count.");  
  148.             }  
  149.             // Iterate through the SqlParameters, assigning the values from the corresponding position in the   
  150.             // value array  
  151.             for (int i = 0, j = commandParameters.Length; i < j; i++) {  
  152.                 // If the current array value derives from IDbDataParameter, then assign its Value property  
  153.                 if (parameterValues[i] is IDbDataParameter) {  
  154.                     IDbDataParameter paramInstance = (IDbDataParameter) parameterValues[i];  
  155.                     if (paramInstance.Value == null) {  
  156.                         commandParameters[i].Value = DBNull.Value;  
  157.                     } else {  
  158.                         commandParameters[i].Value = paramInstance.Value;  
  159.                     }  
  160.                 } else if (parameterValues[i] == null) {  
  161.                     commandParameters[i].Value = DBNull.Value;  
  162.                 } else {  
  163.                     commandParameters[i].Value = parameterValues[i];  
  164.                 }  
  165.             }  
  166.         }#endregion  
  167.     }  
  168. }  

Next is our “shanuBizClasscs”. Here this will be our Business Class from our webform. We pass all the queries and parameters to the Busness class and from the Business class we pass all the parameters and queries or SP to our MySQL helper class.

For example, here we can see I have created an object for our MysqlHelperclass and from the Business class method passed the Stored Procedure name and parameters to the helperclass method.

  1. shanuMYSQLHelper.shanuMySqlHelper objDAL = new shanuMYSQLHelper.shanuMySqlHelper();  
  2. //All Business Method here  
  3. public DataSet SelectList(String SP_NAME, SortedDictionary < stringstring > sd) {  
  4.     try {  
  5.         return objDAL.SP_DataTable_return(SP_NAME, GetSdParameter(sd));  
  6.     } catch (Exception ex) {  
  7.         throw ex;  
  8.     }  
  9. }  

Design your web page and do CRUD Operations

In my sample application let's see:

  • How to search for an item by Item_Code and by Item_Name. Using Stored Procedure.
  • Add new Item to ItemMaster. Using Stored Procedure.
  • Edit Item from ItemMaster. Using Stored Procedure.
  • Delete item from ItemMaster. Using update Query. (For the delete, I will not delete the record from the table. Instead of deleting the record from the table, I have a field called DeleteStatus and by default during an insert I will use the status "N" for deletion that I will update to "Y".

Search Item: An item can be searched for by Item Code and by Item Name. In SQL Server we use % like to display all the records that start with the character. For MySQL we use CONCAT(TRIM(COLUMN_NAME),’%’).

Stored Procedure

The following is the Stored Procedure to search by ItemCode and Item Name:

  1. DELIMITER // CREATE PROCEDURE USP_SelectItemmaster(  
  2. IN P_ItemCode varchar(100),  
  3. IN P_ItemName varchar(100)  
  4. BEGIN  
  5. SELECT  
  6. Item_Code,  
  7. Item_Name,  
  8. Price,  
  9. TAX1,  
  10. Description,  
  11. IN_DATE,  
  12. IN_USR_ID,  
  13. DeleteStatus  
  14. FROM  
  15. ItemMaster  
  16. where  
  17. Item_Code like CONCAT(  
  18. TRIM(  
  19. IFNULL(P_ItemCode, '')  
  20. ),  
  21. '%'  
  22. )  
  23. and Item_Name like CONCAT(  
  24. TRIM(  
  25. IFNULL(P_ItemName, '')  
  26. ),  
  27. '%'  
  28. )  
  29. AND DeleteStatus = 'N';  
  30. END // DELIMITER;  


Search Button Click: In the search button click we pass both the itemcode and Itemname TextBox value as parameter to the search Stored Procedure to return the result.

  1. protected void btnSearch_Click(object sender, ImageClickEventArgs e)   
  2. {  
  3.     SelectList();  
  4. }  
  5. //This Method is used for the search result bind in Grid  
  6. private void SelectList()   
  7. {  
  8.     SortedDictionary < string, string > sd = new SortedDictionary < string, string > () {};  
  9.     sd.Add("@P_ItemCode", txtSitemCDE.Text.Trim());  
  10.     sd.Add("@P_ItemName", txtSItemNme.Text.Trim());  
  11.     DataSet ds = new DataSet();  
  12.     ds = bizObj.SelectList("USP_SelectItemmaster", sd);  
  13.     GridView1.DataSource = ds;  
  14.     GridView1.DataBind();  
  15. }  
Add new Item to ItemMaster: By clicking the New Button I will display all the TextBoxes to get the user input to store new Item information to the MySQL Database.

Insert Stored Procedure: In the insert Stored Procedure I will check whether or not the ItemName already exists. If the Item Name exists then I will display the message to the user indicating the item already exists. If the item does not exist then I will insert the new Item record into the MySQL Database.

Insert Stored Procedure for Item master
  1. DELIMITER // CREATE PROCEDURE USP_InsertItemmaster(  
  2. IN P_Item_Name varchar(100),  
  3. IN P_Price int,  
  4. IN P_TAX1 int,  
  5. IN P_Description varchar(100),  
  6. IN P_IN_USR_ID varchar(100)  
  7. BEGIN IF NOT EXISTS(  
  8. SELECT  
  9. 1  
  10. FROM  
  11. ItemMaster  
  12. WHERE  
  13. Item_Name = P_Item_Name  
  14. and DeleteStatus = 'N'  
  15. THEN BEGIN insert into ItemMaster(  
  16. Item_Name, Price, TAX1, Description,  
  17. IN_DATE, IN_USR_ID, DeleteStatus  
  18. )  
  19. values  
  20. (  
  21. P_Item_Name, P_Price, P_TAX1, P_Description,  
  22. now(), P_IN_USR_ID, 'N'  
  23. );  
  24. select  
  25. "inserted" as "Result";  
  26. end;  
  27. ELSE  
  28. select  
  29. "Exists" as "Result";  
  30. ENd IF;  
  31. END // DELIMITER;  


In save Button Click: I will pass all the parameters to the Insert Stored Procedure.The Procedure function will return as a dataset. If the item is inserted, I will return the result “Inserted”. If the item already exists and is not inserted, then I will return the result “Exists”. Depending on the result, the following result will be displayed to the end user.

  1. private void InsertCall() {  
  2.     SortedDictionary < stringstring > sd = new SortedDictionary < stringstring > () {};  
  3.     sd.Add("@P_Item_Name", txtitemName.Text.Trim());  
  4.     sd.Add("@P_Price", txtPrice.Text.Trim());  
  5.     sd.Add("@P_TAX1", txtTax.Text.Trim());  
  6.     sd.Add("@P_Description", txtdescription.Text.Trim());  
  7.     sd.Add("@P_IN_USR_ID", txtuser.Text.Trim());  
  8.     DataSet ds = new DataSet();  
  9.     ds = bizObj.SelectList("USP_InsertItemmaster", sd);  
  10.     if (ds.Tables.Count > 0) {  
  11.         if (ds.Tables[0].Rows[0].ItemArray[0].ToString() == "Exists") {  
  12.             Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert""alert('Item already Exist !')"true);  
  13.             txtitemName.Focus();  
  14.         }  
  15.     } else {  
  16.         clearControls();  
  17.         SelectList();  
  18.     }  
Edit Item from ItemMaster: The user can edit an item by clicking edit item from the GridView. When the user clicks on the edit button; in the grid, I will change the New Icon to edit and in the Item Code, I will display the selected Item Code with other details. The user can update the details by the Item Code selected.


Update Stored Procedure

Update Stored Procedure for Item master
  1. DELIMITER // CREATE PROCEDURE USP_UpdateItemmaster(  
  2. IN P_Item_Code int,  
  3. IN P_Item_Name varchar(100),  
  4. IN P_Price int,  
  5. IN P_TAX1 int,  
  6. IN P_Description varchar(100),  
  7. IN P_IN_USR_ID varchar(100)  
  8. BEGIN  
  9. update  
  10. ItemMaster  
  11. SET  
  12. Price = P_Price,  
  13. TAX1 = P_TAX1,  
  14. Description = P_Description  
  15. where  
  16. Item_Code = P_Item_Code;  
  17. select  
  18. "updated" as "Result";  
  19. END // DELIMITER; 

In save Button Click: I will pass all the parameters to the update Stored Procedure. After the update is complete, I will refresh the grid to see the changes.

  1. private void UpdateCall()   
  2. {  
  3.     SortedDictionary < string, string > sd = new SortedDictionary < string, string > () {};  
  4.     sd.Add("@P_Item_Code", txtitemCode.Text.Trim());  
  5.     sd.Add("@P_Item_Name", txtitemName.Text.Trim());  
  6.     sd.Add("@P_Price", txtPrice.Text.Trim());  
  7.     sd.Add("@P_TAX1", txtTax.Text.Trim());  
  8.     sd.Add("@P_Description", txtdescription.Text.Trim());  
  9.     sd.Add("@P_IN_USR_ID", txtuser.Text.Trim());  
  10.     DataSet ds = new DataSet();  
  11.     ds = bizObj.SelectList("USP_UpdateItemmaster", sd);  
  12.     SelectList();  
  13.     clearControls();  
  14. }  
Delete Item from ItemMaster: The user can delete an item by clicking delete item from the GridView. When the user clicks on the delete button in the grid, the selected item will get deleted by ItemCode. For deleting, I will not delete the record from table. Instead of deleting the record from the table, I have a field called DeleteStatus that is by default, during the insert, I will use the status "N" and for deletion, I will update it to "Y". Here we can see that I have deleted our previous Item “Samsung Mobile S6”.


In Delete Button Click from Grid: For deleting, I don't use the Stored Procedure. For sample update query, I have used function deletestatus. In GridView rowCommand, I will check for the something.
  1. // This method will delete the selected Rocord from DB  
  2. private void DeleteItem(String ItemCode) {  
  3.     int inserStatus = bizObj.ExecuteNonQuery_IUD("update ItemMaster SET DeleteStatus='Y' where Item_Code='" + ItemCode + "'");  
  4.   
  5.     SelectList();  
  6. }

In the Selectlist() stored procedure, I will select an item with DeleteStatus='N'. So, the deleted item will not bound again in the grid.

Here is the ASPX html Code

  1. <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ShanuVS2015._Default" %>  
  2. <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">  
  3.     <img src="../Images/blank.gif" alt="" width="1" height="10" />  
  4.     <table width="99%" class="search">  
  5.         <tr>  
  6.             <td class="search_es" width="100" align=left>  
  7. Item Code :  
  8. </td>  
  9.             <td align=left>  
  10.                 <asp:TextBox ID="txtSitemCDE" runat="server" MaxLength="30" />  
  11.             </td>  
  12.             <td class="search_es" width="100" align=left>  
  13. Item NAME :  
  14. </td>  
  15.             <td align=left>  
  16.                 <asp:TextBox ID="txtSItemNme" runat="server" MaxLength="30" />  
  17.             </td>  
  18.             <td class="searchbtn" align="center">  
  19.                 <asp:ImageButton ID="btnSearch" runat="server"   
  20. ImageUrl="~/Images/btnSearch.jpg" onclick="btnSearch_Click" />  
  21.             </td>  
  22.         </tr>  
  23.     </table>  
  24.     <img src="../Images/blank.gif" alt="" width="1" height="10" />  
  25.     <table style='width: 99%;table-layout:fixed;' >  
  26.         <tr>  
  27.             <td align="center" valign="middle" >  
  28.                 <table style='width: 99%;table-layout:fixed;'>  
  29.                     <tr>  
  30.                         <td >  
  31.                             <table width="100%" >  
  32.                                 <tr>  
  33.                                     <td class="style1" align=left>  
  34.                                         <table width="100%" class="title">  
  35.                                             <tr>  
  36.                                                 <td width="100%">  
  37.                                                     <img src="Images/Item.gif" />  
  38.                                                     <asp:ImageButton ID="btnAdd" runat="server"   
  39. ImageUrl="~/Images/btnNew.jpg" onclick="btnAdd_Click" />  
  40.                                                 </td>  
  41.                                             </tr>  
  42.                                         </table>  
  43.                                     </td>  
  44.                                 </tr>  
  45.                                 <tr >  
  46.                                     <td id="tdADD" runat="server" visible="false">  
  47.                                         <table width="100%" class="search">  
  48.                                             <tr>  
  49.                                                 <td class="search_es" width="100" align=left>  
  50. Item Code :  
  51. </td>  
  52.                                                 <td align=left>  
  53.                                                     <asp:TextBox ID="txtitemCode" runat="server" MaxLength="30" Enabled="False" />  
  54.                                                 </td>  
  55.                                                 <td class="search_es" width="100" align=left>  
  56. Item NAME :  
  57. </td>  
  58.                                                 <td align=left>  
  59.                                                     <asp:TextBox ID="txtitemName" runat="server" MaxLength="30" />  
  60.                                                     <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtitemName" ErrorMessage="Item Name"></asp:RequiredFieldValidator>  
  61.                                                 </td>  
  62.                                                 <td class="search_es" width="100" align=left>  
  63. Price :  
  64. </td>  
  65.                                                 <td align=left>  
  66.                                                     <asp:TextBox ID="txtPrice" runat="server" MaxLength="30" >0</asp:TextBox>  
  67.                                                     <asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="txtPrice" runat="server"   
  68. ErrorMessage="Only Numbers allowed" ValidationExpression="\d+"></asp:RegularExpressionValidator>  
  69.                                                 </td>  
  70.                                                 <td class="searchbtn" rowspan = "2">  
  71.                                                     <asp:ImageButton ID="btnSave" runat="server" OnClientClick="return ValidCheck();"  
  72. ImageUrl="~/Images/btnSave.jpg" onclick="btnSave_Click"/>  
  73.                                                     <asp:ImageButton ID="btnCancel" runat="server"   
  74. ImageUrl="~/Images/btnCancel.jpg" onclick="btnCancel_Click" />  
  75.                                                 </td>  
  76.                                             </tr>  
  77.                                             <tr>  
  78.                                                 <td class="search_es" width="100" align=left>  
  79. Tax1 :  
  80. </td>  
  81.                                                 <td align=left>  
  82.                                                     <asp:TextBox ID="txtTax" runat="server" MaxLength="30" >0</asp:TextBox>  
  83.                                                     <asp:RegularExpressionValidator ID="RegularExpressionValidator2" ControlToValidate="txtTax" runat="server"   
  84. ErrorMessage="Only Numbers allowed" ValidationExpression="\d+"></asp:RegularExpressionValidator>  
  85.                                                 </td>  
  86.                                                 <td class="search_es" width="100" align=left>  
  87. User :  
  88. </td>  
  89.                                                 <td align=left>  
  90.                                                     <asp:TextBox ID="txtuser" runat="server" MaxLength="30" >0</asp:TextBox>  
  91.                                                 </td>  
  92.                                                 <td class="search_es" width="110" align=left>  
  93. Description :  
  94. </td>  
  95.                                                 <td align=left >  
  96.                                                     <asp:TextBox ID="txtdescription" runat="server" MaxLength="30" TextMode="MultiLine"   
  97. Width="220px" />  
  98.                                                 </td>  
  99.                                             </tr>  
  100.                                         </table>  
  101.                                     </td>  
  102.                                 </tr>  
  103.                             </table>  
  104.                         </td>  
  105.                     </tr>  
  106.                 </table>  
  107.             </td>  
  108.         </tr>  
  109.     </table>  
  110.     <img src="../Images/blank.gif" alt="" width="1" height="10" />  
  111.     <table style='width: 99%;table-layout:fixed;' >  
  112.         <tr>  
  113.             <td align="center" valign="middle" >  
  114.                 <table style='width: 99%;table-layout:fixed;'>  
  115.                     <tr>  
  116.                         <td >  
  117.                             <table width="100%" >  
  118.                                 <tr>  
  119.                                     <td class="style1" align=left>  
  120.                                         <table width="100%" class="title">  
  121.                                             <tr>  
  122.                                                 <td width="100%">  
  123.                                                     <h3>Details:</h3>  
  124.                                                 </td>  
  125.                                             </tr>  
  126.                                         </table>  
  127.                                     </td>  
  128.                                 </tr>  
  129.                                 <tr>  
  130.                                     <td>  
  131.                                         <table width="100%" class="search">  
  132.                                             <tr>  
  133.                                                 <td>  
  134.                                                     <asp:GridView ID="GridView1" runat="server" Width="100%"   
  135. AutoGenerateColumns="False" BackColor="#ECF3F4" BorderColor="#336699"   
  136. BorderStyle="Solid" BorderWidth="1px" onrowcommand="GridView1_RowCommand">  
  137.                                                         <AlternatingRowStyle BackColor="#C5DAE4" />  
  138.                                                         <EditRowStyle BorderColor="#336699" BorderStyle="Solid" BorderWidth="1px" />  
  139.                                                         <HeaderStyle BackColor="#336699" BorderColor="AliceBlue" BorderStyle="Solid"   
  140. BorderWidth="1px" ForeColor="White" />  
  141.                                                         <SelectedRowStyle BackColor="#8CB3D9" />  
  142.                                                         <Columns>  
  143.                                                             <asp:TemplateField HeaderText=" Edit ">  
  144.                                                                 <HeaderStyle HorizontalAlign="Center" Width="5%" CssClass="title"></HeaderStyle>  
  145.                                                                 <ItemStyle HorizontalAlign="Center" CssClass="normal"></ItemStyle>  
  146.                                                                 <ItemTemplate>  
  147.                                                                     <asp:ImageButton ID="btnEdit" runat="server"   
  148. ImageUrl="Images/edit.gif" ToolTip="Edit row" CommandName="edits" CommandArgument='  
  149.                                                                         <%#Eval("Item_Code")%>'/>  
  150.                                                                     </ItemTemplate>  
  151.                                                                 </asp:TemplateField>  
  152.                                                                 <asp:TemplateField HeaderText=" Delete ">  
  153.                                                                     <HeaderStyle HorizontalAlign="Center" Width="5%" CssClass="title"></HeaderStyle>  
  154.                                                                     <ItemStyle HorizontalAlign="Center" CssClass="normal"></ItemStyle>  
  155.                                                                     <ItemTemplate>  
  156.                                                                         <asp:ImageButton ID="btnDelete" runat="server" OnClientClick="return DeleteConfirm();"  
  157. ImageUrl="Images/delete.gif" ToolTip="Delete row" CommandName="deletes" CommandArgument='  
  158.                                                                             <%#Eval("Item_Code")%>'/>  
  159.                                                                         </ItemTemplate>  
  160.                                                                     </asp:TemplateField>  
  161.                                                                     <asp:BoundField DataField="Item_Code" HeaderText="ItemCode" >  
  162.                                                                         <HeaderStyle HorizontalAlign="Center" />  
  163.                                                                     </asp:BoundField>  
  164.                                                                     <asp:BoundField DataField="Item_Name" HeaderText="ItemName" >  
  165.                                                                         <HeaderStyle HorizontalAlign="Center" />  
  166.                                                                     </asp:BoundField>  
  167.                                                                     <asp:BoundField DataField="Price" HeaderText="Price" >  
  168.                                                                         <HeaderStyle HorizontalAlign="Center" />  
  169.                                                                         <ItemStyle HorizontalAlign="Right" />  
  170.                                                                     </asp:BoundField>  
  171.                                                                     <asp:BoundField DataField="TAX1" HeaderText="TAX" >  
  172.                                                                         <HeaderStyle HorizontalAlign="Center" />  
  173.                                                                         <ItemStyle HorizontalAlign="Right" />  
  174.                                                                     </asp:BoundField>  
  175.                                                                     <asp:BoundField DataField="Description" HeaderText="Description" >  
  176.                                                                         <HeaderStyle HorizontalAlign="Center" />  
  177.                                                                     </asp:BoundField>  
  178.                                                                     <asp:BoundField DataField="IN_USR_ID" HeaderText="User" >  
  179.                                                                         <HeaderStyle HorizontalAlign="Center" />  
  180.                                                                     </asp:BoundField>  
  181.                                                                 </Columns>  
  182.                                                             </asp:GridView>  
  183.                                                         </td>  
  184.                                                     </tr>  
  185.                                                 </table>  
  186.                                             </td>  
  187.                                         </tr>  
  188.                                     </table>  
  189.                                 </td>  
  190.                             </tr>  
  191.                         </table>  
  192.                     </td>  
  193.                 </tr>  
  194.             </table>  
  195.             <asp:HiddenField ID="hidsaveType" Value="Add" runat="server" />  
  196.             <asp:HiddenField ID="hiduser_ID" Value="Add" runat="server" />  
  197.         </asp:Content>  

CSS File

I have added a bit of CSS to the root Site.css file and used the default MasterPage. If the CSS is not added to the MasterPage, then kindly add it. The CSS file can be found inside the folder Content in Visual Studio 2015.