There are Data Access Layer classes for each collection Employee, Office & Department which can use ADO or the Enterprise library to call stored procedures to access data.
EmpDir.cs
Credit Due: for GridView Paging code - Powlo’s site (Paul Robinson’s SP Site)
using System;
using System.Collections.Generic;
using System.Text;
//add
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.Common;
using System.Web.Configuration;
using System.Data.SqlTypes;
using System.Xml.Serialization;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint;
using System.IO;
[assembly: System.Security.AllowPartiallyTrustedCallers]
namespace EmpDir
{
public class EmpDir: WebPart
{
private Label lblMessage;
private TextBox txtSearchFN;
private TextBox txtSearchLN;
private TextBox txtSearchPhone;
private TextBox txtSearchEmployee;
private TextBox txtSearchInitials;
private Button btnSearch;
private Button btnClear;
private SPGridView oGrid;
private DataSet oDataset;
private DataView oView;
private DropDownList ddDepartment;
private DropDownList ddOffice;
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
}
protected override void RenderContents(HtmlTextWriter writer)
{
writer.Write("<table border='0' width='600px'>");
writer.Write("<tr><td align='right' nowrap='nowrap'>Intials:</td>");
writer.Write("<td align='left' >");
txtSearchInitials.RenderControl(writer);
writer.Write("</td><td align='right' nowrap='nowrap'>Emp#:</td>");
writer.Write("<td align='left' >");
txtSearchEmployee.RenderControl(writer);
writer.Write("</td><td></td></tr>");
writer.Write("<tr><td align='right' nowrap='nowrap'>First Name:</td>");
writer.Write("<td align='left' >");
txtSearchFN.RenderControl(writer);
writer.Write("</td><td align='right' nowrap='nowrap'>Last Name:</td>");
writer.Write("<td align='left' >");
txtSearchLN.RenderControl(writer);
writer.Write("</td><td></td></tr>");
writer.Write("<tr><td align='right' nowrap='nowrap'>Phone:</td>");
writer.Write("<td align='left' >");
txtSearchPhone.RenderControl(writer);
writer.Write("</td><td align='right' nowrap='nowrap'>Department:</td>");
writer.Write("<td align='left' >");
ddDepartment.RenderControl(writer);
writer.Write("</td><td></td></tr>");
writer.Write("<tr><td align='right' nowrap='nowrap'>Office:</td>");
writer.Write("<td align='left' >");
ddOffice.RenderControl(writer);
writer.Write("</td><td align='right' nowrap='nowrap'></td>");
writer.Write("<td align='left' >");
//ddDepartment.RenderControl(writer);
writer.Write("</td><td></td></tr>");
// BUTTONS
writer.Write("<tr><td colspan='3'>");
btnSearch.RenderControl(writer);
writer.Write("&nbsp;&nbsp;");
btnClear.RenderControl(writer);
writer.Write("</table>");
// GRID
writer.Write("<table border='0'>");
writer.Write("</td></tr>");
writer.Write("<tr><td colspan='3'>");
oGrid.RenderControl(writer);
writer.Write("</td><td></td></tr>");
writer.Write("</table>");
}
protected override void CreateChildControls()
{
base.CreateChildControls();
lblMessage = new Label();
btnSearch = new Button();
btnClear = new Button();
txtSearchFN = new TextBox();
txtSearchLN = new TextBox();
txtSearchPhone = new TextBox();
txtSearchEmployee = new TextBox();
txtSearchInitials = new TextBox();
ddDepartment = new DropDownList();
ddOffice = new DropDownList();
Department.loadDepartmentDropdown(this.ddDepartment, "--- All Departments --");
Office.loadOfficeDropdown(this.ddOffice, "--- All Offices ---");
this.lblMessage.Width = new Unit("800px");
this.btnSearch.Text = "Search";
this.btnSearch.Click += new EventHandler(btnSubmit_Click);
this.btnClear.Text = "Clear";
this.btnClear.Click += new EventHandler(btnClear_Click);
this.oDataset = new DataSet();
this.oView = new DataView();
this.oGrid = new SPGridView();
this.oGrid.DataSource = oView;
this.oGrid.AutoGenerateColumns = false;
this.oGrid.AllowSorting = true;
this.oGrid.Sorting += new GridViewSortEventHandler(oGrid_Sorting);
this.oGrid.AllowPaging = true;
oGrid.Columns.Add(Utilities.buildColumn("Employee", "Emp #"));
// Add Cols using a utility method
oGrid.Columns.Add(Utilities.buildColumn("FirstName", "First Name"));
oGrid.Columns.Add(Utilities.buildColumn("LastName", "Last Name"));
oGrid.Columns.Add(Utilities.buildColumn("WorkPhone", "Direct"));
oGrid.Columns.Add(Utilities.buildColumn("WorkPhoneExt", "Ext"));
oGrid.Columns.Add(Utilities.buildColumn("MobilePhone", "Cell"));
oGrid.Columns.Add(Utilities.buildColumn("Office", "Office"));
oGrid.Columns.Add(Utilities.buildColumn("PrimaryDept", "Primary Dept"));
oGrid.Columns.Add(Utilities.buildColumn("SecondaryDept", "Secondary Dept"));
this.Controls.Add(lblMessage);
this.Controls.Add(ddDepartment);
this.Controls.Add(ddOffice);
this.Controls.Add(txtSearchFN);
this.Controls.Add(txtSearchLN);
this.Controls.Add(txtSearchEmployee);
this.Controls.Add(txtSearchInitials);
this.Controls.Add(txtSearchPhone);
this.Controls.Add(btnSearch);
this.Controls.Add(btnClear);
this.Controls.Add(oGrid);
this.oGrid.PageSize = 10;
this.oGrid.AllowPaging = true;
this.oGrid.PageIndexChanging += new GridViewPageEventHandler(oGrid_PageIndexChanging);
this.oGrid.PagerTemplate = null; // Must be called after Controls.Add(oGrid)
this.lblMessage.Width = new Unit("400px");
this.ddDepartment.Width = new Unit("180px");
this.ddOffice.Width = new Unit("180px");
this.btnSearch.Width = new Unit("80px");
this.btnClear.Width = new Unit("80px");
this.txtSearchFN.Width = new Unit("180px");
this.txtSearchLN.Width = new Unit("180px");
this.txtSearchEmployee.Width = new Unit("180px");
this.txtSearchInitials.Width = new Unit("180px");
this.txtSearchPhone.Width = new Unit("180px");
this.oGrid.Width = new Unit("800px");
}
void oGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
oGrid.PageIndex = e.NewPageIndex;
//oGrid.DataBind();
bindGrid(String.Empty);
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
bindGrid(String.Empty);
}
protected void btnClear_Click(object sender, EventArgs e)
{
txtSearchFN.Text = string.Empty;
txtSearchLN.Text = string.Empty;
txtSearchEmployee.Text = string.Empty;
txtSearchPhone.Text = string.Empty;
txtSearchInitials.Text = string.Empty;
ddDepartment.SelectedIndex = 0;
ddOffice.SelectedIndex = 0;
Utilities.BindGridView(oGrid,true);
}
private void bindGrid(string sort)
{
string sFN = txtSearchFN.Text.Trim().Replace(" ", "%");
string sLN = txtSearchLN.Text.Trim().Replace(" ", "%");
string sEM = txtSearchEmployee.Text.Trim().Replace(" ", "%");
string sPH = txtSearchPhone.Text.Trim().Replace(" ", "%");
string sIN = txtSearchInitials.Text.Trim().Replace(" ", "%");
string sDept = string.Empty;
if (ddDepartment.SelectedIndex > 0)
sDept = "%" + ddDepartment.SelectedValue.ToString() + "%";
string sOffice = string.Empty;
if (ddOffice.SelectedIndex > 0)
sOffice = ddOffice.SelectedValue.ToString();
oDataset = Employee.searchEmployees(sFN, sLN, sIN, sPH, sOffice, sDept, sEM);
oView = new DataView(oDataset.Tables[0]);
oGrid.DataSource = oView;
if (Utilities.hasData(sort))
oView.Sort = sort;
//oGrid.DataBind();
Utilities.BindGridView(oGrid, oView, true);
}
void oGrid_Sorting(object sender, GridViewSortEventArgs e)
{
string lastExpression = "";
if (ViewState["SortExpression"] != null)
lastExpression = ViewState["SortExpression"].ToString();
string lastDirection = "asc";
if (ViewState["SortDirection"] != null)
lastDirection = ViewState["SortDirection"].ToString();
string newDirection = "asc";
if (e.SortExpression == lastExpression)
newDirection = (lastDirection == "asc") ? "desc" : "asc";
ViewState["SortExpression"] = e.SortExpression;
ViewState["SortDirection"] = newDirection;
string sSort = e.SortExpression + " " + newDirection;
bindGrid(sSort);
}
}
}
// Utilities.cs
using System;
using System.Collections.Generic;
using System.Text;
//add
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.Common;
using System.Web.Configuration;
using System.Data.SqlTypes;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint;
using System.IO;
namespace BWEmpDir
{
class Utilities
{
static private string _connR = WebConfigurationManager.ConnectionStrings["IntranetUserR"].ConnectionString;
static private string _connRW = WebConfigurationManager.ConnectionStrings["IntranetUserRW"].ConnectionString;
static public SqlConnection getConnR()
{
return (new SqlConnection(_connR));
}
static public SqlConnection getConnRW()
{
return (new SqlConnection(_connRW));
}
static public bool hasData(string f)
{
bool bReturn = false;
if (f != null && f.Trim().Length > 0)
bReturn = true;
return bReturn;
}
static public bool hasData(TextBox f)
{
bool bReturn = false;
if (f != null && f.Text.Trim().Length > 0)
bReturn = true;
return bReturn;
}
static public bool hasNoData(string f)
{
bool bReturn = false;
if (f == null || f.Trim().Length == 0)
bReturn = true;
return bReturn;
}
static public bool hasNoData(TextBox f)
{
bool bReturn = false;
if (f == null || f.Text.Trim().Length == 0)
bReturn = true;
return bReturn;
}
static public void BindGridView(SPGridView gv, bool bRebind)
{
gv.DataSource = new string[] { }; //clear
if (bRebind)
gv.DataBind();
}
static public void BindGridView(SPGridView gv, DataSet ds, bool bRebind)
{
gv.DataSource = new string[] { }; //clear
gv.DataSource = ds;
if (bRebind)
gv.DataBind();
}
static public void BindGridView(SPGridView gv, DataView ds, bool bRebind)
{
gv.DataSource = new string[] { }; //clear
gv.DataSource = ds;
if (bRebind)
gv.DataBind();
}
static public BoundField buildColumn(string sData, string sHeader, string SortExp)
{
BoundField col = new BoundField();
col.DataField = sData;
col.HeaderText = sHeader;
col.SortExpression = SortExp;
return (col);
}
static public BoundField buildColumn(string sData, string sHeader)
{
return(buildColumn(sData, sHeader, sData));
}
}
}