How to display a table when no records are present in the Database? This is the most frequently asked question for beginners, even for intermediate developers, while displaying data on the web forms when there are no records present in the Database. Most of us, to satisfy our requirements, switch the EmptyDataText property setting of the GridView control or any DataControl by specifying the value as "No Records Found". But there are sometimes a requirement to display a table on the web page if there is no records found in the database so that the user can add a new record by filling in the fields on the table and by clicking the button. In such cases we mostly make use of a table control along with the GridView control whereby setting its (the table's) visibility to true or false depending on whether there are records in the DB or not. The alternative is to use an EmptyDataTemplate of the GridView control or the data control, whichever you are using. Whereby the user must input the data in the respective fields and on the click of the button, all the data entered by the user is stored in the DB and finally the stored Data is displayed in the GridView control. Something about the EmptyDataTemplate is that it's a template that can display any piece of information in a specified format when no records are present in the Database. You need to explicitly define the EmptyDataTemplate for your Data Control. It's simple to define the only thing that you need to do, which is to create a class that will implement the ITemplate interface and define your layout of your EmptyDataTemplate. And then finally you need to set the GridView.EmptyDataTemplate to your Template (that you created).
Ok now let's move on towards our example, as you all know. Now let's try to implement our goal.
1. Starting with our That Queries. Over here we'll be making use of an Employees table stored in an Oracle Database. The following is the script for that:
- create table Employees
- (
- EId int not null,
- EName varchar(30),
- EAddress varchar(30),
- EAge Number,
- ECountry varchar(30),
- EDesignation varchar(30)
- )
- //For Accessing the Data stored in Employees Table
- create or replace procedure prcGetEmployees
- (
- temp_cursor out sys_refCursor
- )
- as
- begin
- open temp_cursor for
- select * from Employees;
- end;
- //For Inserting the Data in Employees Table
- create or replace procedure prcAddEmp
- (
- v_name varchar2,
- v_address varchar2,
- n_age number,
- v_country varchar2,
- v_designation varchar2
- )
- as
- v_id number:=0;
- begin
- select count(*) into v_id from Employees;
- v_id:=v_id+1;
- insert into Employees
- values (v_id,v_name,v_address,n_age,v_country,v_designation);
- end;
Until now we have created a table and a procedure that will allow us to read the Employees table information or insert data into the Employees table. Now let's use a webform in Visual Studio 2010 (named test.aspx in our project) and add a gridview to the test.aspx page and on the page load event call the procedure that provides us Employee data, in our case prcGetEmployees is the procedure that, when executed, will return the resultset of the Employees table.
Test.aspx
Test.aspx.cs Code Behind
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data;
- public partial class test : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- LoadData();
- }
- private void LoadData()
- {
- DAL objDal = new DAL();
- DataSet ds = objDal.GetRecords();
- if (ds != null)
- {
- GridView1.DataSource = ds.Tables[0].DefaultView;
- GridView1.DataBind();
- }
- }
- }
Now when I try to run the file all I get is a blank page like the following.
This is because I neither set the EmptyDataTextProperty to "No Records Found" nor did I use an EmptyDataTemplate.
Ok, now let's move toward creating an EmptyDataTemplate for our Employee Table. For creating a table we need the cell, in other words the Column and the Row. The following are the two classes that we can call; they have helper classes that will allow us to create a column and a row for our GridView's EmptyDataTemplate.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI.WebControls;
- using System.ComponentModel;
- using System.Reflection;
- using System.Collections;
- using System.Web.UI;
-
-
-
-
- public class EmptyColumn
- {
-
- public string ColumnName { get; set; }
-
- public bool IsReadOnly { get; set; }
-
- public WebControl ColumnControl { get; set; }
-
- public EmptyColumn()
- {
- ColumnControl = new Label();
- }
- }
-
-
-
-
- public class EmptyGridRow
- {
-
- public EmptyColumn Columns { get; set; }
-
- public const int ColumnSize = 2;
- }
Now we are ready to create/define our EmptyDataTemplate Class. Since all the helper classes are done, the following is the source code for it.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.HtmlControls;
- using System.Reflection;
- using System.Text;
-
-
-
-
- public class EmptyGridTemplate : ITemplate
- {
- private ListItemType _itemType;
- private const string _openTable = "table";
- private const string _closeTable = "/table";
- private const string _openAngularBracket = "<";
- private const string _closeAngularBracket = ">";
- private const string _openCell = "td";
- private const string _closeCell = "/td";
- private const string _openRow = "tr";
- private const string _closeRow = "/tr";
- private const string _space = " ";
- private const string _doubleQuotes = "\"";
- private const string _idValueDelimeter = "=";
- private const string _rowName = "row";
- private const string _cellName = "cell";
- private const string _id = "id";
- private const string _tableID = "table1";
- private const string _tableStyle = "style=\"width:100%;border:1px solid silver;\"";
- private const string _colSpan = "colspan=\"2\"";
- private const string _alignRight = "align=\"right\"";
- private int _rowCounter = 0, _cellCounter = 0;
- private List<EmptyGridRow> _lstGridRows = null;
- private string _redirectPath = string.Empty;
-
- private UpdatePanel udpMain = new UpdatePanel()
- {
- ID = "udpMain",
- RenderMode = UpdatePanelRenderMode.Block,
- };
- public EmptyGridTemplate(ListItemType item, List<EmptyGridRow> lstRows, string rediredPath)
- {
- _lstGridRows = lstRows;
- _itemType = item;
- _redirectPath = rediredPath;
- }
-
-
-
-
- public void CreateRows(List<EmptyGridRow> lstGridRows)
- {
- foreach (EmptyGridRow row in lstGridRows)
- {
- Literal ltrlRows = new Literal()
- {
- Text = _openAngularBracket + _openRow + _space + _id +
- _idValueDelimeter + _doubleQuotes + _rowName + _rowCounter + _doubleQuotes + _closeAngularBracket
- };
- udpMain.ContentTemplateContainer.Controls.Add(ltrlRows);
- EmptyColumn col = row.Columns;
-
- Literal ltrlCells = new Literal()
- {
- Text = _openAngularBracket + _openCell + _space + _id + _idValueDelimeter + _doubleQuotes +
- _rowName + _rowCounter + _cellName + _cellCounter + _doubleQuotes + _closeAngularBracket
- };
- udpMain.ContentTemplateContainer.Controls.Add(ltrlCells);
-
- Literal ltrlCell0 = new Literal()
- {
- Text = col.ColumnName + _openAngularBracket + _closeCell + _closeAngularBracket
- };
-
- udpMain.ContentTemplateContainer.Controls.Add(ltrlCell0);
-
- _cellCounter++;
- Literal ltrlCell1 = new Literal()
- {
- Text = _openAngularBracket + _openCell + _space + _id + _idValueDelimeter + _doubleQuotes +
- _rowName + _rowCounter + _cellName + _cellCounter + _doubleQuotes + _closeAngularBracket
- };
- udpMain.ContentTemplateContainer.Controls.Add(ltrlCell1);
-
- Literal ltrlCell1Close = new Literal()
- {
- Text = _openAngularBracket + _closeCell + _closeAngularBracket
- };
- _rowCounter++;
- _cellCounter = 0;
- }
- Literal ltrl = new Literal()
- {
-
- Text = _openAngularBracket + _openRow + _space + _id +
- _idValueDelimeter + _doubleQuotes + _rowName + _rowCounter + _doubleQuotes + _closeAngularBracket +
-
- _openAngularBracket + _openCell + _space + _id + _idValueDelimeter + _doubleQuotes +
- _rowName + _rowCounter + _cellName + _cellCounter + _doubleQuotes + _colSpan + _alignRight + _closeAngularBracket
- };
- udpMain.ContentTemplateContainer.Controls.Add(ltrl);
- Button btnAdd = new Button()
- {
- ID = "btnAdd",
- Text = "Save",
- CommandName = "AddNew",
- CausesValidation = false,
- BackColor = System.Drawing.Color.White,
- ForeColor = System.Drawing.Color.Black,
- };
- if (_redirectPath.Equals("Default.aspx", StringComparison.InvariantCultureIgnoreCase))
- btnAdd.OnClientClick = "readEmptyGridData();";
- else
- btnAdd.Click += new EventHandler(btnAdd_Click);
- PostBackTrigger trgPostBack = new PostBackTrigger();
- trgPostBack.ControlID = btnAdd.ID;
- udpMain.Triggers.Add(trgPostBack);
- udpMain.ContentTemplateContainer.Controls.Add(btnAdd);
- }
-
- private void btnAdd_Click(object sender, EventArgs e)
- {
- string Data = "";
- foreach (EmptyGridRow row in _lstGridRows)
- {
- if (!row.Columns.IsReadOnly)
- {
- WebControl webControl = (WebControl)row.Columns.ColumnControl;
- if (webControl.GetType().Equals(typeof(DropDownList)))
- {
- DropDownList ddl = (DropDownList)webControl;
- Data += ddl.SelectedItem.Text + "|";
- }
- else if (webControl.GetType().Equals(typeof(TextBox)))
- {
- TextBox txt = (TextBox)webControl;
- Data += txt.Text + "|";
- }
- }
- }
- string eName = string.Empty;
- string eAdd = string.Empty;
- int eAge = 0;
- string eCountry = string.Empty;
- string eDesignation = string.Empty;
- string[] eData = Data.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
- eName = eData[0];
- eAdd = eData[1];
- eAge = Convert.ToInt32(eData[2]);
- eCountry = eData[3];
- eDesignation = eData[4];
- DAL objDal = new DAL();
- objDal.AddRecord(eName, eAdd, eAge, eCountry, eDesignation);
- HttpContext.Current.Response.Redirect(_redirectPath);
- }
- public void InstantiateIn(Control container)
- {
- switch (_itemType)
- {
- case ListItemType.Header:
- Literal ltrlTable = new Literal()
- {
- Text = _openAngularBracket + _openTable + _space + _id + _idValueDelimeter + _doubleQuotes + _tableID
- + _doubleQuotes + _space + _tableStyle + _closeAngularBracket
- };
- udpMain.ContentTemplateContainer.Controls.Add(ltrlTable);
- CreateRows(_lstGridRows);
- container.Controls.Add(udpMain);
- break;
- default:
- break;
- }
- }
- }
NOTE: if your observe the code above with in the lime shade, you will find that if the _redirectpage is default.aspx then we are setting the button onclientclick property to some JavaScript function or else we are setting the server side function. For now, just keep this in mind because it will be used at the end of the article where we will try to insert records into the Employee table using an xmlHttpRequest object.
If you observe the source code you will find the following things:-
-
Our EmptyGridTemplate inherits from the ITemplate class that has one function named InstantiateIn (Control container) that is called automatically when you bind this class to your data control; in our case the GridView control's EmptyDataTemplate.
-
There is a CreateRow function that accepts a List<EmptyGridRow> class, in other words this list will have all the columns along with the necessary information such as column name to be displayed, whether the column is read-only or not and the control for that column (that is to be used for accepting the input from the user for that column, for instance for accepting a name we will provide a TextBox control in front of the Name column).
-
In the InstantiateIn function we are using an UpdatePanel, in other words all of our table's data will be placed inside an updatepanel.
-
After adding all the table data to the updatePanel control, we are adding the update panel to the container control inside the InstantiateIn function.
-
Finally, on the Button click event we are calling the DAL function to add the record for the Employee.
Now our EmptyGridTemplate is ready, we can make use of it. For testing it let's try to add a page with the name test2.aspx.
NOTE: Remember that our EmptyGridTemplate makes use of UpdatePanel for adding the control on the page. In other words, in our test2.aspx page we will need to add a ScriptManager class.
The following is the markup file of the test2.aspx page:
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="test2.aspx.cs" Inherits="test2" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:ScriptManager ID="ScriptManager1" runat="server">
- </asp:ScriptManager>
- <asp:GridView ID="GridView1" runat="server">
- </asp:GridView>
- </div>
- </form>
- </body>
- </html>
Following is the Source Code of test2.aspx Page.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data;
- public partial class test2 : System.Web.UI.Page
- {
- private string _pageName = string.Empty;
- protected void Page_Load(object sender, EventArgs e)
- {
- _pageName = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
- System.IO.FileInfo oInfo = new System.IO.FileInfo(_pageName);
- _pageName = oInfo.Name;
- LoadData();
- }
- private void LoadData()
- {
- DAL objDal = new DAL();
- DataSet ds = objDal.GetRecords();
- if (ds != null)
- {
- GridView1.DataSource = ds.Tables[0].DefaultView;
- GridView1.DataBind();
- }
- else
- {
-
-
- int[] txtFieldsIndex = { 1, 2, 3, 4 };
- int[] ddlFieldsIndex = { 5 };
-
- string[] colNames = { "EID", "EName", "EAddress", "EAge", "EDesignation", "ECountry" };
- string errorMsg = "";
-
- EmptyColumn[] cols = new EmptyColumn[colNames.Length];
- for (byte i = 0; i < cols.Length; i++)
- {
- cols[i] = new EmptyColumn()
- {
- ColumnName = colNames[i],
- IsReadOnly = txtFieldsIndex.Contains(i) == true ? false : ddlFieldsIndex.Contains(i) == true ? false : true;
- ColumnControl = txtFieldsIndex.Contains(i) == true ? new TextBox()
- {
- ID = string.Concat("txt", colNames[i]),
- ForeColor = System.Drawing.Color.Black,
- Width = new Unit(200),
- Height = 15,
- }
- :
- ddlFieldsIndex.Contains(i) == true ? (WebControl)new DropDownList()
- {
- ID = string.Concat("ddl", colNames[i]),
- ForeColor = System.Drawing.Color.Black,
- Width = new Unit(200),
- Height = 20,
- AutoPostBack = true,
-
- DataSource = new ListItemCollection { new ListItem("Select"), new ListItem("India"), new ListItem("China") }
- }
- :
- (WebControl)new Label()
- {
-
-
- ID = string.Concat("lbl", colNames[i]),
- ForeColor = System.Drawing.Color.Black,
- Width = new Unit(200),
- Text = "0",
- Height = 15,
- }
- };
- }
-
- List<EmptyGridRow> lstRows = new List<EmptyGridRow>();
- foreach (EmptyColumn col in cols)
- {
- lstRows.Add(new EmptyGridRow() { Columns = col });
- }
-
- EmptyGridTemplate objTemplate = new EmptyGridTemplate(ListItemType.Header, lstRows, _pageName);
-
- GridView1.EmptyDataTemplate = objTemplate;
- GridView1.DataBind();
- }
- }
- }
Finally now we can test the output of our test2.aspx page. If you look at the following diagram, you will find that the column that we declared is displayed as a column heading and also the controls that we used are displayed respective to the columns on the right hand side on the second cell of the row. Since we just now created the Employees, it will not have any records because we are testing the EmptyDataTemplate that will be rendered only if there are no records found in the database.
Now the user can enter the required data and when the user clicks on the Save button all the data entered by the user will be saved in the DB (the Employees table). Let's try to save one record and see the output for it.
Finally now after clicking the SAVE button, you'll find that your data from the EmptyGridTemplate is being saved in your Employees table and you will be able to see the following output:
With this we have completed our work that we want to do. Now let's talk about something relevant to this module. It would be a better approach for us if we check the column, in other words whether the user has entered the proper column name, in other words with no duplication of columns with the same name has been done. Also, for inserting data you can do it directly through JavaScript by making use of the AJAX xmlhttprequest (that we discussed in the NOTE above) or if needed by using Code Behind. In the example above we inserted records by firing the server side button onClick event. Ok now let's try to sort it out one by one.
Checking Column names
For this I've created a class with the name PropertyChecker and one Extension Method CheckItem (that is used on List<EmptyGridRows>) that will check whether the List<EmptyGridRow> has a column with the same name and value that will be added. If it has a column with the same name and value then it will prompt the user with the error details stating the column was already added. The PropertyChecker class is basically checking whether the properties of list elements and that of the targetItem element matches and also their values. If the value of the list elements and that of the targetItem matches then it returns false or else returns true. True denotes the targetItem can be added as a new row in the List<EmptyGridRow> list. It's a generic class.
The following is the source code for it.
Extension Method (CheckItem) Source Code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
-
-
-
-
-
- public static class ExtendedColumnCheck
- {
- public static bool CheckItem<T, U>(this List<T> lstT, U addItem, out string errorMsg)
- where T : class,new()
- where U : class,new()
- {
- errorMsg = "";
- bool status = true;
- if (addItem == null)
- {
- errorMsg = "Added Item cannot be null";
- return false;
- }
- if (lstT.Count == 0)
- status = true;
- else
- {
- status = PropertyMatcher.MatchPropertyValues<T, U>(lstT, addItem, out errorMsg);
- }
- return status;
- }
- }
The PropertyMatcher class (that will check whether the property of two objects are the same or not; if the same then return false else true):
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Reflection;
- using System.Collections;
-
-
-
-
- public static class PropertyMatcher
- {
- public static bool MatchPropertyValues<TSource, TTarget>(List<TSource> lstT, TTarget targetItem, out string errorMsg, string propName = "")
- where TSource : class
- where TTarget : class,new()
- {
- errorMsg = "";
- List<TSource> _lstSource = null;
- TTarget _targetItem = null;
-
- _lstSource = lstT;
- _targetItem = targetItem;
- bool status = true;
- Dictionary<string, object> dicObj = new Dictionary<string, object>();
- Dictionary<string, object> dicSourceObj = new Dictionary<string, object>();
- if (_lstSource == null || _targetItem == null)
- throw new ArgumentException("Source List or Target Item cannot be null");
- if (_lstSource.Count == 0)
- throw new ArgumentException("List Should Contain atleast one item");
-
- PropertyInfo[] targetProperties = _targetItem.GetType().GetProperties(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
-
- dicObj = targetProperties.ToDictionary(prop => prop.Name, prop => prop.GetValue(_targetItem, null)).ToDictionary(prop => prop.Key, prop => prop.Value);
-
- foreach (var source in _lstSource)
- {
-
-
- PropertyInfo[] props = source.GetType().GetProperties();
-
- dicSourceObj = props.ToDictionary(prop => prop.Name,
- prop => prop.GetValue(source, null)).
- ToDictionary(prop => prop.Key, prop => prop.Value);
-
- foreach (KeyValuePair<string, object> keyvalue in dicObj)
- {
-
-
- foreach (KeyValuePair<string, object> sourceKeyValue in dicSourceObj)
- {
-
- PropertyInfo prop = typeof(TSource).GetProperty(sourceKeyValue.Key);
-
- Type type = prop.PropertyType;
-
- PropertyInfo[] mainProps = type.GetProperties();
-
- foreach (PropertyInfo p in mainProps)
- {
- if (!(p.Name.Equals("IsReadOnly", StringComparison.InvariantCultureIgnoreCase) || p.Name.Equals("ColumnControl", StringComparison.InvariantCultureIgnoreCase)))
- {
- if (p.Name.Equals(keyvalue.Key, StringComparison.InvariantCultureIgnoreCase) && object.Equals(p.GetValue(sourceKeyValue.Value, null), keyvalue.Value))
- {
- status = false;
- errorMsg = "Key : " + p.Name + " With Value = " + keyvalue.Value +
- " Already added";
- return status;
- }
- }
- }
- }
- }
- }
- return status;
- }
- }
Now let's try to test it. For testing I've created a web form with the name as test3.aspx.
Mark up code of the Test3.aspx page:
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="test3.aspx.cs" Inherits="test3" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:GridView ID="GridView1" runat="server">
- </asp:GridView>
- </div>
- </form>
- </body>
- </html>
Code behind for the same
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data;
- public partial class test3 : System.Web.UI.Page
- {
- private string _pageName = string.Empty;
- protected void Page_Load(object sender, EventArgs e)
- {
- _pageName = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
- System.IO.FileInfo oInfo = new System.IO.FileInfo(_pageName);
- _pageName = oInfo.Name;
- LoadData();
- }
- private void LoadData()
- {
- DAL objDal = new DAL();
- DataSet ds = objDal.GetRecords();
- if (ds != null)
- {
- GridView1.DataSource = ds.Tables[0].DefaultView;
- GridView1.DataBind();
- }
- else
- {
- bool tableStatus = true;
-
- int[] txtFieldsIndex = { 1, 2, 3, 4 };
- int[] ddlFieldsIndex = { 5 };
-
-
- string[] colNames = { "EID", "EName", "EName", "EAge", "EDesignation", "ECountry" };
- string errorMsg = "";
- EmptyColumn[] cols = new EmptyColumn[colNames.Length];
- for (byte i = 0; i < cols.Length; i++)
- {
- cols[i] = new EmptyColumn()
- {
- ColumnName = colNames[i],
- IsReadOnly = txtFieldsIndex.Contains(i) == true ? false : ddlFieldsIndex.Contains(i) == true ? false : true,
- ColumnControl = txtFieldsIndex.Contains(i) == true ? new TextBox()
- {
- ID = string.Concat("txt", colNames[i]),
- ForeColor = System.Drawing.Color.Black,
- Width = new Unit(200),
- Height = 15,
- }
- :
- ddlFieldsIndex.Contains(i) == true ? (WebControl)new DropDownList()
- {
- ID = string.Concat("ddl", colNames[i]),
- ForeColor = System.Drawing.Color.Black,
- Width = new Unit(200),
- Height = 20,
- AutoPostBack = true,
- DataSource = new ListItemCollection { new ListItem("Select"), new ListItem("India"), new ListItem("China") }
- }
- :
- (WebControl)new Label()
- {
- ID = string.Concat("lbl", colNames[i]),
- ForeColor = System.Drawing.Color.Black,
- Width = new Unit(200),
- Text = "0",
- Height = 15,
- }
- };
- }
-
- List<EmptyGridRow> lstRows = new List<EmptyGridRow>();
- foreach (EmptyColumn col in cols)
- {
-
- if (lstRows.CheckItem<EmptyGridRow, EmptyColumn>(col, out errorMsg))
- lstRows.Add(new EmptyGridRow() { Columns = col });
- else
- {
- tableStatus = false;
- ClientScript.RegisterStartupScript(this.GetType(), "error", "<script>alert('" + errorMsg + "');</script>");
- }
- }
- if (tableStatus == true)
- {
-
- EmptyGridTemplate objTemplate = new EmptyGridTemplate(ListItemType.Header, lstRows, _pageName);
- GridView1.EmptyDataTemplate = objTemplate;
- GridView1.DataBind();
- }
- }
- }
- }
Now let's run the test3.aspx page and see the following output. Before running it, we will truncate the Employees table so that there are no records and our EmptyGridTemplate can be applied to the GridView's EmptyDataTemplate. Now when you run your test3.aspx page you'll see the following output.
By looking at the following output we are sure that our propertymatcher class is working fine.
Now for the last part of the article, in other words the insertion of data using an xmlhttprequest object. For this I'm adding an external js file with the name jsHelper.js and also the AjaxHelper.aspx page that will be called by an xmlHttpRequest object.
The following is the code for that:
External Js File (jsHelper.js)
The AjaxHelper.js file is placed inside the Ajax folder within the application. The following is the markup and code behind file for that:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data;
- using System.Data.OracleClient;
- public partial class Ajax_AjaxHelper : System.Web.UI.Page
- {
- const string _dbCon = "DATA SOURCE=DHQMDB;WORD=hqm321;PERSIST SECURITY INFO=True;USER ID=HQM_ADMIN";
- OracleConnection con;
- OracleCommand cmd;
- protected void Page_Load(object sender, EventArgs e)
- {
- if (Request.QueryString.Count > 0 && Request.QueryString["eData"] != null)
- {
- AddDataToEmployee(Request.QueryString["eData"].ToString());
- }
- }
-
-
-
-
-
- private string AddDataToEmployee(string eData)
- {
-
- string[] arr = eData.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
- List<string> lstValues = new List<string>();
- string value = "", returnValue = "";
- foreach (string str in arr)
- {
-
-
-
- value = str.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries)[1];
- lstValues.Add(value);
- }
- DAL obj = new DAL();
- int rows = obj.AddRecord(lstValues[1], lstValues[2], Convert.ToInt32(lstValues[3]), lstValues[4], lstValues[5]);
- if (rows > 0)
- returnValue = "Successfull";
- else
- returnValue = "Error";
- return returnValue;
- }
- }
Now to test it I've added a page with the name Default.aspx.
On the Markup page (the default.aspx page) we just took a gridview control.
Default.aspx Code behind
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Configuration;
- using System.Data.OracleClient;
- using System.Data;
- public partial class _Default : System.Web.UI.Page
- {
- private string _pageName = string.Empty;
- protected void Page_Load(object sender, EventArgs e)
- {
- _pageName = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
- System.IO.FileInfo oInfo = new System.IO.FileInfo(_pageName);
- _pageName = oInfo.Name;
- LoadData();
- }
- private void LoadData()
- {
- DAL objDal = new DAL();
- DataSet ds = objDal.GetRecords();
- if (ds != null)
- {
- GridView1.DataSource = ds.Tables[0].DefaultView;
- GridView1.DataBind();
- }
- else
- {
- bool tableStatus = true;
-
- int[] txtFieldsIndex = { 1, 2, 3, 4 };
- int[] ddlFieldsIndex = { 5 };
-
- string[] colNames = { "EID", "EName", "EAddress", "EAge", "EDesignation", "ECountry" };
- string errorMsg = "";
- EmptyColumn[] cols = new EmptyColumn[colNames.Length];
- for (byte i = 0; i < cols.Length; i++)
- {
- cols[i] = new EmptyColumn()
- {
- ColumnName = colNames[i],
- IsReadOnly = txtFieldsIndex.Contains(i) == true ? false : ddlFieldsIndex.Contains(i) == true ? false : true,
- ColumnControl = txtFieldsIndex.Contains(i) == true ? new TextBox()
- {
- ID = string.Concat("txt", colNames[i]),
- ForeColor = System.Drawing.Color.Black,
- Width = new Unit(200),
- Height = 15,
- }
- :
- ddlFieldsIndex.Contains(i) == true ? (WebControl)new DropDownList()
- {
- ID = string.Concat("ddl", colNames[i]),
- ForeColor = System.Drawing.Color.Black,
- Width = new Unit(200),
- Height = 20,
- AutoPostBack = true,
- DataSource = new ListItemCollection { new ListItem("Select"), new ListItem("India"), new ListItem("China") }
- }
- :
- (WebControl)new Label()
- {
- ID = string.Concat("lbl", colNames[i]),
- ForeColor = System.Drawing.Color.Black,
- Width = new Unit(200),
- Text = "0",
- Height = 15,
- }
- };
- }
-
- List<EmptyGridRow> lstRows = new List<EmptyGridRow>();
- foreach (EmptyColumn col in cols)
- {
-
- if (lstRows.CheckItem<EmptyGridRow, EmptyColumn>(col, out errorMsg))
- lstRows.Add(new EmptyGridRow() { Columns = col });
- else
- {
- tableStatus = false;
- ClientScript.RegisterStartupScript(this.GetType(), "error", "<script>alert('" + errorMsg + "');</script>");
- }
- }
- if (tableStatus == true)
- {
-
- EmptyGridTemplate objTemplate = new EmptyGridTemplate(ListItemType.Header, lstRows, _pageName);
- GridView1.EmptyDataTemplate = objTemplate;
- GridView1.DataBind();
- }
- }
- }
- }
Let's try to run the Default.aspx page. Before running we will truncate the Employees table so that there are no records and our EmptyGridTemplate can be applied to the GridView's EmptyDataTemplate. Now when you run your Default.aspx page you'll see the following output.
On the Button click event it will class the jsHelper function that will insert the data into my Employees table by making use of the AjaxHeper.aspx file and you will get the following output:
I hope you all liked the article.