Admin Side Coding (Dhaval)
Admin Side Coding (Dhaval)
AdminMasterMenu.aspx
<head runat="server">
<title>Easy Leave</title>
function toggleAllCheckboexs(toggle)
n = document.forms[0].length;
for(i=0;i<frm.length;i++)
if(frm.elements[i].type=="checkbox")
if (frm.elements[i].name.indexOf('Cbx')==0)
frm.elements[i].checked=toggle;
}
function onlyNumbers(evt)
return true;
else
return false ;
function OnlyChars(evt)
if ((charCode > 64 && charCode < 91)||(charCode > 96 && charCode < 123) || charCode==32)
return true;
else
return false ;
function onlyNumbersdot(evt)
else
return false ;
function onlyNumbershifen(evt)
return true;
else
return false ;
function onlyNumbersUnderScore(evt)
return true;
else
return false ;
</script>
</head>
<body>
</asp:ScriptManager>
<div>
<tr>
<tr >
<tr>
Easy Leave</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
Orientation="Horizontal" StaticEnableDefaultPopOutImage="False"
StaticSubMenuIndent="10px"
Width="100%" Height="25px">
<Items>
</asp:MenuItem>
</asp:MenuItem>
</asp:MenuItem>
</asp:MenuItem>
</asp:MenuItem>
</asp:MenuItem>
</asp:MenuItem>
Value="Manage Leave"></asp:MenuItem>
Value="Manage Employee"></asp:MenuItem>
<asp:MenuItem NavigateUrl="~/Admin/frmMonthlyLeaveTransactions.aspx"
Text="Monthly Leave"
Value="Monthly Leave"></asp:MenuItem>
Value="Yearly Leave"></asp:MenuItem>
<asp:MenuItem NavigateUrl="~/Admin/frmPresentedEmpList.aspx"
Text="PresentEmployeesList"
Value="PresentEmployeesList"></asp:MenuItem>
<asp:MenuItem NavigateUrl="~/Admin/frmNextDayEmpList.aspx"
Text="NextdayEmployeesList"
Value="NextdayEmployeesList"></asp:MenuItem>
</asp:MenuItem>
</asp:MenuItem>
</Items>
</asp:Menu>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<div style="height:420px">
</asp:contentplaceholder>
</div>
</td>
</tr>
<tr>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Frmcity.aspx
<tr >
<tr>
Add City
</td>
</tr>
<tr>
</td>
</tr>
<tr>
*- Mandatory fields</td>
</tr>
<tr>
</td>
<td style="width:2%">:</td>
<td style="width:18%">
</td>
<td>
</td>
</tr>
<tr>
<td >
City Description
</td>
<td>:</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
<td>
:</td>
<td>
</asp:DropDownList>
</td>
</td>
</tr>
<tr>
<td>
<td>
:</td>
<td>
</asp:DropDownList>
</td>
</td>
</tr>
<tr>
<td >
Status<span class="lblerror">*</span>
</td>
<td>:</td>
<td>
<asp:ListItem>Select</asp:ListItem>
<asp:ListItem>Active</asp:ListItem>
<asp:ListItem>InActive</asp:ListItem>
</asp:DropDownList>
</td>
</td>
</tr>
<tr>
<td colspan="2" style="height: 26px">
</td>
</td>
</tr>
</table></td></tr></table>
</asp:Content>
Adminheadermenu.aspx
<tr><td>
<Items>
</asp:MenuItem>
</asp:MenuItem>
Value="Manage Employee"></asp:MenuItem>
Value="Monthly Leave"></asp:MenuItem>
Value="Yearly Leave"></asp:MenuItem>
<asp:MenuItem NavigateUrl="~/Admin/frmPresentedEmpList.aspx"
Text="PresentEmployeesList"
Value="PresentEmployeesList"></asp:MenuItem>
<asp:MenuItem NavigateUrl="~/Admin/frmNextDayEmpList.aspx"
Text="NextdayEmployeesList"
Value="NextdayEmployeesList"></asp:MenuItem>
</asp:MenuItem>
</Items>
</asp:Menu>
</td></tr>
</table>
Loginclass.cs
using System;
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 Microsoft.ApplicationBlocks.Data;
/// <summary>
/// Summary description for clsApplication_Logic
/// </summary>
public class clsApplication_Logic
{
string _UserName, _StartingDate, _EndingDate, _ApplaiyingDate, _LeavePurpose;
int _LeaveTypeId, _NoOfDays, _AppStatusId, _ApplicationNo;
public string Sort_On;
public clsApplication_Logic()
{
//
// TODO: Add constructor logic here
//
}
public string UserName
{
get { return _UserName; }
set { _UserName = value; }
}
public string StartingDate
{
get { return _StartingDate; }
set { _StartingDate = value; }
}
public string EndingDate
{
get { return _EndingDate; }
set { _EndingDate = value; }
}
public string ApplaiyingDate
{
get { return _ApplaiyingDate; }
set { _ApplaiyingDate = value; }
}
public string LeavePurpose
{
get { return _LeavePurpose; }
set { _LeavePurpose = value; }
}
SqlHelper.ExecuteNonQuery(ClsConnectionString.getConnectionString(),CommandType.StoredPro
cedure,"spAddLeaveApplication",p);
}
clsBLAdminLogin_Logic.cs
using System;
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 Microsoft.ApplicationBlocks.Data;
/// <summary>
/// Summary description for ClsBLAdminLogin
/// </summary>
public class clsAdminLogin_Logic
{
public clsAdminLogin_Logic()
{
//
// TODO: Add constructor logic here
//
}
clsCity_Logic.cs
using System;
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 Microsoft.ApplicationBlocks.Data;
///<summary >
///This is BOL City_Logic Class
/// Created By: Team A PropertyNet
/// Created On: 16-Oct-2007
/// Last Modified By: Team A PropertyNet
/// Last Modified On: 16-Oct-2007
/// </summary>
public class clsCity_Logic
{
private int _CityId;
private string _CityName;
private string _CityDescr;
public string Sort_On;
private int _StatusId;
private string _StatusName;
private string _CountryName;
private int _CountryId;
private int _StateId;
private string _StateName;
//private clsDbConnector objDbConnector;
public clsCity_Logic()
{
//objDbConnector = new clsDbConnector();
}
public int pro_CityId
{
get
{
return _CityId;
}
set
{
_CityId = value;
}
}
public string pro_CityName
{
get
{
return _CityName;
}
set
{
if (value.ToString() == "")
{
_CityName = "";
}
else
{
_CityName = value;
}
}
}
public string pro_CityDescription
{
get
{
return _CityDescr;
}
set
{
if (value.ToString() == "")
{
_CityDescr = "";
}
else
{
_CityDescr = value;
}
}
}
}
}
public int pro_StatusId
{
get
{
return _StatusId;
}
set
{
_StatusId = value;
}
}
public string pro_CountryName
{
get
{
return _CountryName;
}
set
{
_CountryName = value;
}
}
public DataSet GetCountry()
{
string strSql = string.Empty;
strSql += "select CountryName,CountryId from tbl_Country";
DataSet dsTemp;
//ds = objDbConnector.GetDataSet(strSql);
dsTemp = SqlHelper.ExecuteDataset(ClsConnectionString.getConnectionString(),
CommandType.Text, strSql);
return dsTemp;
clsCommon_Logic.cs
using System;
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 Microsoft.ApplicationBlocks.Data;
///<summary>
//This is BOL Common_Logic Class
/// Created By: Team A PropertyNet
/// Created On: 19-Oct-2007
/// Last Modified By: Team A PropertyNet
/// Last Modified On: 19-Oct-2007
/// </summary
public class clsCommon_Logic
{
public clsCommon_Logic()
{
}
public string pro_CountryName
{
get
{
return _CountryName;
}
set
{
_CountryName = value;
}
}
public int pro_CountryId
{
get
{
return _CountryId;
}
set
{
_CountryId = value;
}
}
public int pro_StateId
{
get
{
return _StateId;
}
set
{
_StateId = value;
}
}
public string pro_StateName
{
get
{
return _StateName;
}
set
{
_StateName = value;
}
}
public int pro_CityId
{
get
{
return _CityId;
}
set
{
_CityId = value;
}
}
public string pro_CityName
{
get
{
return _CityName;
}
set
{
if (value.ToString() == "")
{
_CityName = "";
}
else
{
_CityName = value;
}
}
}
clsCountry_Logic.cs
using System;
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 Microsoft.ApplicationBlocks.Data;
///<summary >
///This is BOL Country_Logic Class
/// Created By: Team A PropertyNet
/// Created On: 16-Oct-2007
/// Last Modified By: Team A PropertyNet
/// Last Modified On: 16-Oct-2007
/// </summary>
public class clsCountry_Logic
{
private int _CountryId;
private string _CountryName;
private string _CountryDescr;
private string _StatusName;
public string Sort_On;
private int _StatusId;
//int statusId;
//private clsDbConnector objDbConnector;
public clsCountry_Logic()
{
//objDbConnector = new clsDbConnector();
}
public int pro_CountryId
{
get
{
return _CountryId;
}
set
{
_CountryId = value;
}
}
}
public int pro_StatusId
{
get
{
return _StatusId;
}
set
{
_StatusId = value;
}
Deptlodic.cs
using System;
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 Microsoft.ApplicationBlocks.Data;
/// <summary>
/// Summary description for clsDept_Logic
/// </summary>
public class clsDept_Logic
{
private int _DeptId;
private string _DeptName;
private string _DeptDescr;
private string _StatusName;
public string Sort_On;
private int _StatusId;
//int statusId;
//private clsDbConnector objDbConnector;
public clsDept_Logic()
{
//objDbConnector = new clsDbConnector();
}
public int pro_DeptId
{
get
{
return _DeptId;
}
set
{
_DeptId = value;
}
}
}
public int pro_StatusId
{
get
{
return _StatusId;
}
set
{
_StatusId = value;
}
clsEmployee_Logic.cs
using System;
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 Microsoft.ApplicationBlocks.Data;
/// <summary>
/// Summary description for clsEmployee_Logic
/// </summary>
public class clsEmployee_Logic
{
string _Password, _OldPassword, _EmployeeLoginId, _ContactNo, _Address, _EmailId,
_EmpName,_Designation,_Role,_UserName;
int _EmpId, _CityId, _StateId, _CountryId,_DeptId,_projId;
clsDbConnector objdDbConnector = new clsDbConnector();
public string Sort_On;
public clsEmployee_Logic()
{
//
// TODO: Add constructor logic here
//
}
DataSet dsEmployeeLoginDetails=
SqlHelper.ExecuteDataset(ClsConnectionString.getConnectionString(),
CommandType.StoredProcedure, "spEmployeeLoginDetails", p);
return dsEmployeeLoginDetails;
}
public int EmployeeChangePassword()
{
//SqlParameter[] p = new SqlParameter[2];
SqlHelper.ExecuteNonQuery(ClsConnectionString.getConnectionString(),
CommandType.StoredProcedure, "spUpdataEmployee", p);
dsTemp = SqlHelper.ExecuteDataset(ClsConnectionString.getConnectionString(),
CommandType.StoredProcedure,"spGetEmpDetails",p);
DataRowCollection drc = dsTemp.Tables[0].Rows;
if (drc.Count > 0)
{
_UserName = drc[0]["UserName"].ToString();
_EmpName = drc[0]["EmpName"].ToString();
_Designation = drc[0]["EmpDesigName"].ToString();
_ContactNo = drc[0]["ContactNo"].ToString();
_Address = drc[0]["Address"].ToString();
_EmailId = drc[0]["EmailId"].ToString();
_CityId = Convert.ToInt32(drc[0]["CityId"].ToString());
_StateId = Convert.ToInt32(drc[0]["StateId"].ToString());
_CountryId = Convert.ToInt32(drc[0]["CountryId"].ToString());
_DeptId=Convert.ToInt32(drc[0]["DeptId"].ToString());
}
else
{
_EmpName = "";
_ContactNo = "";
_Address = "";
_EmailId = "";
}
DataSet ds =
SqlHelper.ExecuteDataset(ClsConnectionString.getConnectionString(),
CommandType.StoredProcedure, "spCheckUserName", p);
DataTable dtable = ds.Tables[0];
if (dtable.Rows.Count > 0)
return true;
else
return false;
* *
}*/
return SqlHelper.ExecuteDataset(ClsConnectionString.getConnectionString(),
CommandType.Text, "spGetPresentDayEmpList");
}
return SqlHelper.ExecuteDataset(ClsConnectionString.getConnectionString(),
CommandType.Text, "spGetNextDayEmpList");
}
clsEmployeeLogin_Logic.cs
using System;
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 Microsoft.ApplicationBlocks.Data;
/// <summary>
/// Summary description for clsEmployeeLogin_Logic
/// </summary>
public class clsEmployeeLogin_Logic
{
public clsEmployeeLogin_Logic()
{
//
// TODO: Add constructor logic here
//
}
clsLeave_Logic.cs
using System;
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 Microsoft.ApplicationBlocks.Data;
/// <summary>
/// Summary description for clsLeave_Logic
/// </summary>
public class clsLeave_Logic
{
private string
_LeaveTypeName,_UserName,_StartingMonth,_EndingMonth,_StartingDate,_EndingDate;
int _LeaveTypeId, _NoOfLeavesPerYear, _UsedLeaves, _BalanceLeaves, _ApplicationNo,
_AppStatusId;
int _CasualBalanceLeaves, _EarnBalanceLeaves, _HalfPaidBalanceLeaves,
_MedicalBalanceLeaves;
public string Sort_On;
public clsLeave_Logic()
{
//
// TODO: Add constructor logic here
//
}
SqlHelper.ExecuteDataset(ClsConnectionString.getConnectionString(),
CommandType.StoredProcedure, "spGetBalanceDays", p);
//return i;
return (int)p[2].Value;
}
public DataSet GetLeaveType()
{
return SqlHelper.ExecuteDataset(ClsConnectionString.getConnectionString(),
CommandType.StoredProcedure, "spGetLeaveType");
}
//Jan 04 11.09am
public void GetTotalBalanceDays()
{
SqlParameter[] p = new SqlParameter[5];
p[0] = new SqlParameter("@UserName", SqlDbType.VarChar);
p[0].Value = _UserName;
p[1] = new SqlParameter("@CasualBalanceLeaves", SqlDbType.Int);
p[1].Direction = ParameterDirection.Output;
p[2] = new SqlParameter("@EarnBalanceLeaves", SqlDbType.Int);
p[2].Direction = ParameterDirection.Output;
p[3] = new SqlParameter("@HalfPaidBalanceLeaves", SqlDbType.Int);
p[3].Direction = ParameterDirection.Output;
p[4] = new SqlParameter("@MedicalBalanceLeaves", SqlDbType.Int);
p[4].Direction = ParameterDirection.Output;
SqlHelper.ExecuteDataset(ClsConnectionString.getConnectionString(),
CommandType.StoredProcedure, "spGetTotalBalanceDays", p);
_CasualBalanceLeaves = (int)p[1].Value;
_EarnBalanceLeaves = (int)p[2].Value;
_HalfPaidBalanceLeaves = (int)p[3].Value;
_MedicalBalanceLeaves = (int)p[4].Value;
Statelogic.cs
using System;
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 Microsoft.ApplicationBlocks.Data;
///<summary>
///This is BOL State_Logic Class
/// Created By: Team A PropertyNet
/// Created On: 15-Oct-2007
/// Last Modified By: Team A PropertyNet
/// Last Modified On: 15-Oct-2007
/// </summary>
public class clsState_Logic
{
private int _StateId;
private string _StateName;
private string _StateDescr;
public string Sort_On;
private int _StatusId;
private string _StatusName;
private string _CountryName;
private int _CountryId;
//private clsDbConnector objDbConnector;
public clsState_Logic()
{
//objDbConnector = new clsDbConnector();
}
public int pro_StateId
{
get
{
return _StateId;
}
set
{
_StateId = value;
}
}
public string pro_StateName
{
get
{
return _StateName;
}
set
{
if (value.ToString() == "")
{
_StateName = "";
}
else
{
_StateName = value;
}
}
}
public string pro_StateDescription
{
get
{
return _StateDescr;
}
set
{
if (value.ToString() == "")
{
_StateDescr = "";
}
else
{
_StateDescr = value;
}
}
}
}
public string pro_CountryName
{
get
{
return _CountryName;
}
set
{
_CountryName= value;
}
}
if (_CountryName != "select" && _CountryName!=null)
strSql = strSql + " and CountryName='" + _CountryName + "'";
//else
//{
// strSql = strSql + " and CountryName='" + _CountryName + "'";
//}
if (Sort_On != "")
{
strSql = strSql + " ORDER BY " + Sort_On;
}
else
{
strSql = strSql + " Order By StateName";
}
DataSet dsTemp;
//dsTemp = objDbConnector.GetDataSet(strSql);
dsTemp = SqlHelper.ExecuteDataset(ClsConnectionString.getConnectionString(),
CommandType.Text, strSql);
return dsTemp;
}
public void AddState()
{
string strSql = string.Empty;
Statuslogic.cs
using System;
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 Microsoft.ApplicationBlocks.Data;
/// <summary>
/// This is BOL Status_Logic Class
/// Created By: Team A PropertyNet
/// Created On: 13-Oct-2007
/// Last Modified By: Team A PropertyNet
/// Last Modified On: 13-Oct-2007
/// </summary>
public class clsStatus_Logic
{
private int _StatusId;
private string _StatusName;
private string _StatusDescr;
public string Sort_On;
//private clsDbConnector objDbConnector;
public clsStatus_Logic()
{
//objDbConnector = new clsDbConnector();
}
public int pro_StatusId
{
get
{
return _StatusId;
}
set
{
_StatusId = value;
}
}
public string pro_StatusName
{
get
{
return _StatusName;
}
set
{
if (value.ToString() == "")
{
_StatusName = "";
}
else
{
_StatusName = value;
}
}
}
public string pro_StatusDescription
{
get
{
return _StatusDescr;
}
set
{
if (value.ToString() == "")
{
_StatusDescr = "";
}
else
{
_StatusDescr = value;
}
}
}
Connection.cs
using System;
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;
/// <summary>
/// Summary description for ClsConnectionstring
/// </summary>
public class ClsConnectionString
{
public ClsConnectionString()
{
}
public static string getConnectionString()
{
return ConfigurationManager.AppSettings["connstr"];
}
}
Sqldbclass.cs
using System;
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.Web.Configuration;
/// <summary>
/// This is DAL Class
/// Created By: Team A PropertyNet
/// Created On: 09-Oct-2007
/// Last Modified By: Team A PropertyNet
/// Last Modified On: 09-Oct-2007
/// </summary>
public class clsDbConnector
{
private SqlConnection dbConnection;
private SqlDataAdapter dbAdapter;
private SqlCommand cmd;
public clsDbConnector()
{
this.dbConnection = new
SqlConnection(ConfigurationManager.AppSettings["connstr"]);
//Or
//this.dbConnection = new
SqlConnection(ConfigurationManager.ConnectionStrings["conString"]);
if (dbConnection.State == ConnectionState.Closed)
{
try
{
if (dbConnection.State == ConnectionState.Broken)
{
dbConnection.Close();
dbConnection.Open();
}
}
catch (Exception err)
{
throw err;
}
}
if (dbConnection.State == ConnectionState.Broken)
{
dbConnection.Close();
dbConnection.Open();
}
cmd = new SqlCommand();
}
dbConnection.Open();
SqlTransaction t;
SqlCommand dbCommand = new SqlCommand();
t = dbConnection.BeginTransaction();
dbCommand.Connection = dbConnection;
dbCommand.Transaction = t;
try
{
for (int i = 0; i < strSql.Length; i++)
{
if (strSql[i].ToString() == null)
break;
dbCommand.CommandText = strSql[i].ToString();
dbCommand.ExecuteNonQuery();
}
//Commit Transactions
t.Commit();
t = null;
dbCommand.Dispose();
dbConnection.Close();
return true;
}
catch (Exception ex)
{
//Rollback Transactions
t.Rollback();
t = null;
string str = ex.Message;
dbCommand.Dispose();
dbConnection.Close();
return false;
}
}
}
Sqlhandler.cs
//===============================================================================
// Microsoft Data Access Application Block for .NET
// http://msdn.microsoft.com/library/en-us/dnbda/html/daab-rm.asp
//
// SQLHelper.cs
//
// This file contains the implementations of the SqlHelper and SqlHelperParameterCache
// classes.
//
// For more information see the Data Access Application Block Implementation Overview.
//
//===============================================================================
// Copyright (C) 2000-2001 Microsoft Corporation
// All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR
// FITNESS FOR A PARTICULAR PURPOSE.
//==============================================================================
using System;
using System.Data;
using System.Xml;
using System.Data.SqlClient;
using System.Collections;
namespace Microsoft.ApplicationBlocks.Data
{
/// <summary>
/// The SqlHelper class is intended to encapsulate high performance, scalable best
practices for
/// common uses of SqlClient.
/// </summary>
//Since this class provides only static methods, make the default
constructor private to prevent
//instances from being created with "new SqlHelper()".
private SqlHelper() {}
/// <summary>
/// This method is used to attach array of SqlParameters to a SqlCommand.
///
/// This method will assign a value of DbNull to any parameter with a
direction of
/// InputOutput and a value of null.
///
/// This behavior will prevent default values from being used, but
/// this will be the less common case than an intended pure output
parameter (derived as InputOutput)
/// where the user provided no input value.
/// </summary>
/// <param name="command">The command to which the parameters will be
added</param>
/// <param name="commandParameters">an array of SqlParameters tho be added
to command</param>
private static void AttachParameters(SqlCommand command, SqlParameter[]
commandParameters)
{
foreach (SqlParameter p in commandParameters)
{
//check for derived output value with no value assigned
if ((p.Direction == ParameterDirection.InputOutput) &&
(p.Value == null))
{
p.Value = DBNull.Value;
}
command.Parameters.Add(p);
}
}
/// <summary>
/// This method assigns an array of values to an array of SqlParameters.
/// </summary>
/// <param name="commandParameters">array of SqlParameters to be assigned
values</param>
/// <param name="parameterValues">array of objects holding the values to be
assigned</param>
private static void AssignParameterValues(SqlParameter[] commandParameters,
object[] parameterValues)
{
if ((commandParameters == null) || (parameterValues == null))
{
//do nothing if we get no data
return;
}
/// <summary>
/// This method opens (if necessary) and assigns a connection, transaction,
command type and parameters
/// to the provided command.
/// </summary>
/// <param name="command">the SqlCommand to be prepared</param>
/// <param name="connection">a valid SqlConnection, on which to execute
this command</param>
/// <param name="transaction">a valid SqlTransaction, or 'null'</param>
/// <param name="commandType">the CommandType (stored procedure, text,
etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL
command</param>
/// <param name="commandParameters">an array of SqlParameters to be
associated with the command or 'null' if no parameters are required</param>
private static void PrepareCommand(SqlCommand command, SqlConnection
connection, SqlTransaction transaction, CommandType commandType, string commandText,
SqlParameter[] commandParameters)
{
//if the provided connection is not open, we will open it
if (connection.State != ConnectionState.Open)
{
connection.Open();
}
return;
}
#region ExecuteNonQuery
/// <summary>
/// Execute a SqlCommand (that returns no resultset and takes no
parameters) against the database specified in
/// the connection string.
/// </summary>
/// <remarks>
/// e.g.:
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure,
"PublishOrders");
/// </remarks>
/// <param name="connectionString">a valid connection string for a
SqlConnection</param>
/// <param name="commandType">the CommandType (stored procedure, text,
etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL
command</param>
/// <returns>an int representing the number of rows affected by the
command</returns>
public static int ExecuteNonQuery(string connectionString, CommandType
commandType, string commandText)
{
//pass through the call providing null for the set of SqlParameters
return ExecuteNonQuery(connectionString, commandType, commandText,
(SqlParameter[])null);
}
/// <summary>
/// Execute a SqlCommand (that returns no resultset) against the database
specified in the connection string
/// using the provided parameters.
/// </summary>
/// <remarks>
/// e.g.:
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure,
"PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">a valid connection string for a
SqlConnection</param>
/// <param name="commandType">the CommandType (stored procedure, text,
etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL
command</param>
/// <param name="commandParameters">an array of SqlParamters used to
execute the command</param>
/// <returns>an int representing the number of rows affected by the
command</returns>
public static int ExecuteNonQuery(string connectionString, CommandType
commandType, string commandText, params SqlParameter[] commandParameters)
{
//create & open a SqlConnection, and dispose of it after we are
done.
using (SqlConnection cn = new SqlConnection(connectionString))
{
cn.Open();
/// <summary>
/// Execute a stored procedure via a SqlCommand (that returns no resultset)
against the database specified in
/// the connection string using the provided parameter values. This method
will query the database to discover the parameters for the
/// stored procedure (the first time each stored procedure is called), and
assign the values based on parameter order.
/// </summary>
/// <remarks>
/// This method provides no access to output parameters or the stored
procedure's return value parameter.
///
/// e.g.:
/// int result = ExecuteNonQuery(connString, "PublishOrders", 24, 36);
/// </remarks>
/// <param name="connectionString">a valid connection string for a
SqlConnection</param>
/// <param name="spName">the name of the stored prcedure</param>
/// <param name="parameterValues">an array of objects to be assigned as the
input values of the stored procedure</param>
/// <returns>an int representing the number of rows affected by the
command</returns>
public static int ExecuteNonQuery(string connectionString, string spName,
params object[] parameterValues)
{
//if we receive parameter values, we need to figure out where they
go
if ((parameterValues != null) && (parameterValues.Length > 0))
{
//pull the parameters for this stored procedure from the
parameter cache (or discover them & populate the cache)
SqlParameter[] commandParameters =
SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
/// <summary>
/// Execute a SqlCommand (that returns no resultset and takes no
parameters) against the provided SqlConnection.
/// </summary>
/// <remarks>
/// e.g.:
/// int result = ExecuteNonQuery(conn, CommandType.StoredProcedure,
"PublishOrders");
/// </remarks>
/// <param name="connection">a valid SqlConnection</param>
/// <param name="commandType">the CommandType (stored procedure, text,
etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL
command</param>
/// <returns>an int representing the number of rows affected by the
command</returns>
public static int ExecuteNonQuery(SqlConnection connection, CommandType
commandType, string commandText)
{
//pass through the call providing null for the set of SqlParameters
return ExecuteNonQuery(connection, commandType, commandText,
(SqlParameter[])null);
}
/// <summary>
/// Execute a SqlCommand (that returns no resultset) against the specified
SqlConnection
/// using the provided parameters.
/// </summary>
/// <remarks>
/// e.g.:
/// int result = ExecuteNonQuery(conn, CommandType.StoredProcedure,
"PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="connection">a valid SqlConnection</param>
/// <param name="commandType">the CommandType (stored procedure, text,
etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL
command</param>
/// <param name="commandParameters">an array of SqlParamters used to
execute the command</param>
/// <returns>an int representing the number of rows affected by the
command</returns>
public static int ExecuteNonQuery(SqlConnection connection, CommandType
commandType, string commandText, params SqlParameter[] commandParameters)
{
//create a command and prepare it for execution
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, connection, (SqlTransaction)null, commandType,
commandText, commandParameters);
/// <summary>
/// Execute a stored procedure via a SqlCommand (that returns no resultset)
against the specified SqlConnection
/// using the provided parameter values. This method will query the
database to discover the parameters for the
/// stored procedure (the first time each stored procedure is called), and
assign the values based on parameter order.
/// </summary>
/// <remarks>
/// This method provides no access to output parameters or the stored
procedure's return value parameter.
///
/// e.g.:
/// int result = ExecuteNonQuery(conn, "PublishOrders", 24, 36);
/// </remarks>
/// <param name="connection">a valid SqlConnection</param>
/// <param name="spName">the name of the stored procedure</param>
/// <param name="parameterValues">an array of objects to be assigned as the
input values of the stored procedure</param>
/// <returns>an int representing the number of rows affected by the
command</returns>
public static int ExecuteNonQuery(SqlConnection connection, string spName,
params object[] parameterValues)
{
//if we receive parameter values, we need to figure out where they
go
if ((parameterValues != null) && (parameterValues.Length > 0))
{
//pull the parameters for this stored procedure from the
parameter cache (or discover them & populate the cache)
SqlParameter[] commandParameters =
SqlHelperParameterCache.GetSpParameterSet(connection.ConnectionString, spName);
/// <summary>
/// Execute a SqlCommand (that returns no resultset and takes no
parameters) against the provided SqlTransaction.
/// </summary>
/// <remarks>
/// e.g.:
/// int result = ExecuteNonQuery(trans, CommandType.StoredProcedure,
"PublishOrders");
/// </remarks>
/// <param name="transaction">a valid SqlTransaction</param>
/// <param name="commandType">the CommandType (stored procedure, text,
etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL
command</param>
/// <returns>an int representing the number of rows affected by the
command</returns>
public static int ExecuteNonQuery(SqlTransaction transaction, CommandType
commandType, string commandText)
{
//pass through the call providing null for the set of SqlParameters
return ExecuteNonQuery(transaction, commandType, commandText,
(SqlParameter[])null);
}
/// <summary>
/// Execute a SqlCommand (that returns no resultset) against the specified
SqlTransaction
/// using the provided parameters.
/// </summary>
/// <remarks>
/// e.g.:
/// int result = ExecuteNonQuery(trans, CommandType.StoredProcedure,
"GetOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="transaction">a valid SqlTransaction</param>
/// <param name="commandType">the CommandType (stored procedure, text,
etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL
command</param>
/// <param name="commandParameters">an array of SqlParamters used to
execute the command</param>
/// <returns>an int representing the number of rows affected by the
command</returns>
public static int ExecuteNonQuery(SqlTransaction transaction, CommandType
commandType, string commandText, params SqlParameter[] commandParameters)
{
//create a command and prepare it for execution
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, transaction.Connection, transaction,
commandType, commandText, commandParameters);
/// <summary>
/// Execute a stored procedure via a SqlCommand (that returns no resultset)
against the specified
/// SqlTransaction using the provided parameter values. This method will
query the database to discover the parameters for the
/// stored procedure (the first time each stored procedure is called), and
assign the values based on parameter order.
/// </summary>
/// <remarks>
/// This method provides no access to output parameters or the stored
procedure's return value parameter.
///
/// e.g.:
/// int result = ExecuteNonQuery(conn, trans, "PublishOrders", 24, 36);
/// </remarks>
/// <param name="transaction">a valid SqlTransaction</param>
/// <param name="spName">the name of the stored procedure</param>
/// <param name="parameterValues">an array of objects to be assigned as the
input values of the stored procedure</param>
/// <returns>an int representing the number of rows affected by the
command</returns>
public static int ExecuteNonQuery(SqlTransaction transaction, string
spName, params object[] parameterValues)
{
//if we receive parameter values, we need to figure out where they
go
if ((parameterValues != null) && (parameterValues.Length > 0))
{
//pull the parameters for this stored procedure from the
parameter cache (or discover them & populate the cache)
SqlParameter[] commandParameters =
SqlHelperParameterCache.GetSpParameterSet(transaction.Connection.ConnectionString,
spName);
#endregion ExecuteNonQuery
#region ExecuteDataSet
/// <summary>
/// Execute a SqlCommand (that returns a resultset and takes no parameters)
against the database specified in
/// the connection string.
/// </summary>
/// <remarks>
/// e.g.:
/// DataSet ds = ExecuteDataset(connString, CommandType.StoredProcedure,
"GetOrders");
/// </remarks>
/// <param name="connectionString">a valid connection string for a
SqlConnection</param>
/// <param name="commandType">the CommandType (stored procedure, text,
etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL
command</param>
/// <returns>a dataset containing the resultset generated by the
command</returns>
public static DataSet ExecuteDataset(string connectionString, CommandType
commandType, string commandText)
{
//pass through the call providing null for the set of SqlParameters
return ExecuteDataset(connectionString, commandType, commandText,
(SqlParameter[])null);
}
/// <summary>
/// Execute a SqlCommand (that returns a resultset) against the database
specified in the connection string
/// using the provided parameters.
/// </summary>
/// <remarks>
/// e.g.:
/// DataSet ds = ExecuteDataset(connString, CommandType.StoredProcedure,
"GetOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">a valid connection string for a
SqlConnection</param>
/// <param name="commandType">the CommandType (stored procedure, text,
etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL
command</param>
/// <param name="commandParameters">an array of SqlParamters used to
execute the command</param>
/// <returns>a dataset containing the resultset generated by the
command</returns>
//***//
public static DataSet ExecuteDataset(string connectionString, CommandType
commandType, string commandText, params SqlParameter[] commandParameters)
{
//create & open a SqlConnection, and dispose of it after we are
done.
using (SqlConnection cn = new SqlConnection(connectionString))
{
cn.Open();
/// <summary>
/// Execute a stored procedure via a SqlCommand (that returns a resultset)
against the database specified in
/// the connection string using the provided parameter values. This method
will query the database to discover the parameters for the
/// stored procedure (the first time each stored procedure is called), and
assign the values based on parameter order.
/// </summary>
/// <remarks>
/// This method provides no access to output parameters or the stored
procedure's return value parameter.
///
/// e.g.:
/// DataSet ds = ExecuteDataset(connString, "GetOrders", 24, 36);
/// </remarks>
/// <param name="connectionString">a valid connection string for a
SqlConnection</param>
/// <param name="spName">the name of the stored procedure</param>
/// <param name="parameterValues">an array of objects to be assigned as the
input values of the stored procedure</param>
/// <returns>a dataset containing the resultset generated by the
command</returns>
public static DataSet ExecuteDataset(string connectionString, string
spName, params object[] parameterValues)
{
//if we receive parameter values, we need to figure out where they
go
if ((parameterValues != null) && (parameterValues.Length > 0))
{
//pull the parameters for this stored procedure from the
parameter cache (or discover them & populate the cache)
SqlParameter[] commandParameters =
SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
/// <summary>
/// Execute a SqlCommand (that returns a resultset and takes no parameters)
against the provided SqlConnection.
/// </summary>
/// <remarks>
/// e.g.:
/// DataSet ds = ExecuteDataset(conn, CommandType.StoredProcedure,
"GetOrders");
/// </remarks>
/// <param name="connection">a valid SqlConnection</param>
/// <param name="commandType">the CommandType (stored procedure, text,
etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL
command</param>
/// <returns>a dataset containing the resultset generated by the
command</returns>
public static DataSet ExecuteDataset(SqlConnection connection, CommandType
commandType, string commandText)
{
//pass through the call providing null for the set of SqlParameters
return ExecuteDataset(connection, commandType, commandText,
(SqlParameter[])null);
}
/// <summary>
/// Execute a SqlCommand (that returns a resultset) against the specified
SqlConnection
/// using the provided parameters.
/// </summary>
/// <remarks>
/// e.g.:
/// DataSet ds = ExecuteDataset(conn, CommandType.StoredProcedure,
"GetOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="connection">a valid SqlConnection</param>
/// <param name="commandType">the CommandType (stored procedure, text,
etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL
command</param>
/// <param name="commandParameters">an array of SqlParamters used to
execute the command</param>
/// <returns>a dataset containing the resultset generated by the
command</returns>
public static DataSet ExecuteDataset(SqlConnection connection, CommandType
commandType, string commandText, params SqlParameter[] commandParameters)
{
//create a command and prepare it for execution
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, connection, (SqlTransaction)null, commandType,
commandText, commandParameters);
//create the DataAdapter & DataSet
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
//fill the DataSet using default values for DataTable names, etc.
da.Fill(ds);
/// <summary>
/// Execute a stored procedure via a SqlCommand (that returns a resultset)
against the specified SqlConnection
/// using the provided parameter values. This method will query the
database to discover the parameters for the
/// stored procedure (the first time each stored procedure is called), and
assign the values based on parameter order.
/// </summary>
/// <remarks>
/// This method provides no access to output parameters or the stored
procedure's return value parameter.
///
/// e.g.:
/// DataSet ds = ExecuteDataset(conn, "GetOrders", 24, 36);
/// </remarks>
/// <param name="connection">a valid SqlConnection</param>
/// <param name="spName">the name of the stored procedure</param>
/// <param name="parameterValues">an array of objects to be assigned as the
input values of the stored procedure</param>
/// <returns>a dataset containing the resultset generated by the
command</returns>
public static DataSet ExecuteDataset(SqlConnection connection, string
spName, params object[] parameterValues)
{
//if we receive parameter values, we need to figure out where they
go
if ((parameterValues != null) && (parameterValues.Length > 0))
{
//pull the parameters for this stored procedure from the
parameter cache (or discover them & populate the cache)
SqlParameter[] commandParameters =
SqlHelperParameterCache.GetSpParameterSet(connection.ConnectionString, spName);
/// <summary>
/// Execute a SqlCommand (that returns a resultset and takes no parameters)
against the provided SqlTransaction.
/// </summary>
/// <remarks>
/// e.g.:
/// DataSet ds = ExecuteDataset(trans, CommandType.StoredProcedure,
"GetOrders");
/// </remarks>
/// <param name="transaction">a valid SqlTransaction</param>
/// <param name="commandType">the CommandType (stored procedure, text,
etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL
command</param>
/// <returns>a dataset containing the resultset generated by the
command</returns>
public static DataSet ExecuteDataset(SqlTransaction transaction,
CommandType commandType, string commandText)
{
//pass through the call providing null for the set of SqlParameters
return ExecuteDataset(transaction, commandType, commandText,
(SqlParameter[])null);
}
/// <summary>
/// Execute a SqlCommand (that returns a resultset) against the specified
SqlTransaction
/// using the provided parameters.
/// </summary>
/// <remarks>
/// e.g.:
/// DataSet ds = ExecuteDataset(trans, CommandType.StoredProcedure,
"GetOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="transaction">a valid SqlTransaction</param>
/// <param name="commandType">the CommandType (stored procedure, text,
etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL
command</param>
/// <param name="commandParameters">an array of SqlParamters used to
execute the command</param>
/// <returns>a dataset containing the resultset generated by the
command</returns>
public static DataSet ExecuteDataset(SqlTransaction transaction,
CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
//create a command and prepare it for execution
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, transaction.Connection, transaction,
commandType, commandText, commandParameters);
//create the DataAdapter & DataSet
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
//fill the DataSet using default values for DataTable names, etc.
da.Fill(ds);
/// <summary>
/// Execute a stored procedure via a SqlCommand (that returns a resultset)
against the specified
/// SqlTransaction using the provided parameter values. This method will
query the database to discover the parameters for the
/// stored procedure (the first time each stored procedure is called), and
assign the values based on parameter order.
/// </summary>
/// <remarks>
/// This method provides no access to output parameters or the stored
procedure's return value parameter.
///
/// e.g.:
/// DataSet ds = ExecuteDataset(trans, "GetOrders", 24, 36);
/// </remarks>
/// <param name="transaction">a valid SqlTransaction</param>
/// <param name="spName">the name of the stored procedure</param>
/// <param name="parameterValues">an array of objects to be assigned as the
input values of the stored procedure</param>
/// <returns>a dataset containing the resultset generated by the
command</returns>
public static DataSet ExecuteDataset(SqlTransaction transaction, string
spName, params object[] parameterValues)
{
//if we receive parameter values, we need to figure out where they
go
if ((parameterValues != null) && (parameterValues.Length > 0))
{
//pull the parameters for this stored procedure from the
parameter cache (or discover them & populate the cache)
SqlParameter[] commandParameters =
SqlHelperParameterCache.GetSpParameterSet(transaction.Connection.ConnectionString,
spName);
#endregion ExecuteDataSet
#region ExecuteReader
/// <summary>
/// this enum is used to indicate whether the connection was provided by
the caller, or created by SqlHelper, so that
/// we can set the appropriate CommandBehavior when calling ExecuteReader()
/// </summary>
private enum SqlConnectionOwnership
{
/// <summary>Connection is owned and managed by SqlHelper</summary>
Internal,
/// <summary>Connection is owned and managed by the caller</summary>
External
}
/// <summary>
/// Create and prepare a SqlCommand, and call ExecuteReader with the
appropriate CommandBehavior.
/// </summary>
/// <remarks>
/// If we created and opened the connection, we want the connection to be
closed when the DataReader is closed.
///
/// If the caller provided the connection, we want to leave it to them to
manage.
/// </remarks>
/// <param name="connection">a valid SqlConnection, on which to execute
this command</param>
/// <param name="transaction">a valid SqlTransaction, or 'null'</param>
/// <param name="commandType">the CommandType (stored procedure, text,
etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL
command</param>
/// <param name="commandParameters">an array of SqlParameters to be
associated with the command or 'null' if no parameters are required</param>
/// <param name="connectionOwnership">indicates whether the connection
parameter was provided by the caller, or created by SqlHelper</param>
/// <returns>SqlDataReader containing the results of the command</returns>
private static SqlDataReader ExecuteReader(SqlConnection connection,
SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[]
commandParameters, SqlConnectionOwnership connectionOwnership)
{
//create a command and prepare it for execution
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, connection, transaction, commandType,
commandText, commandParameters);
//create a reader
SqlDataReader dr;
// call ExecuteReader with the appropriate CommandBehavior
if (connectionOwnership == SqlConnectionOwnership.External)
{
dr = cmd.ExecuteReader();
}
else
{
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
return dr;
}
/// <summary>
/// Execute a SqlCommand (that returns a resultset and takes no parameters)
against the database specified in
/// the connection string.
/// </summary>
/// <remarks>
/// e.g.:
/// SqlDataReader dr = ExecuteReader(connString,
CommandType.StoredProcedure, "GetOrders");
/// </remarks>
/// <param name="connectionString">a valid connection string for a
SqlConnection</param>
/// <param name="commandType">the CommandType (stored procedure, text,
etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL
command</param>
/// <returns>a SqlDataReader containing the resultset generated by the
command</returns>
public static SqlDataReader ExecuteReader(string connectionString,
CommandType commandType, string commandText)
{
//pass through the call providing null for the set of SqlParameters
return ExecuteReader(connectionString, commandType, commandText,
(SqlParameter[])null);
}
/// <summary>
/// Execute a SqlCommand (that returns a resultset) against the database
specified in the connection string
/// using the provided parameters.
/// </summary>
/// <remarks>
/// e.g.:
/// SqlDataReader dr = ExecuteReader(connString,
CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">a valid connection string for a
SqlConnection</param>
/// <param name="commandType">the CommandType (stored procedure, text,
etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL
command</param>
/// <param name="commandParameters">an array of SqlParamters used to
execute the command</param>
/// <returns>a SqlDataReader containing the resultset generated by the
command</returns>
public static SqlDataReader ExecuteReader(string connectionString,
CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
//create & open a SqlConnection
SqlConnection cn = new SqlConnection(connectionString);
cn.Open();
try
{
//call the private overload that takes an internally owned
connection in place of the connection string
return ExecuteReader(cn, null, commandType, commandText,
commandParameters,SqlConnectionOwnership.Internal);
}
catch
{
//if we fail to return the SqlDatReader, we need to close the
connection ourselves
cn.Close();
throw;
}
}
/// <summary>
/// Execute a stored procedure via a SqlCommand (that returns a resultset)
against the database specified in
/// the connection string using the provided parameter values. This method
will query the database to discover the parameters for the
/// stored procedure (the first time each stored procedure is called), and
assign the values based on parameter order.
/// </summary>
/// <remarks>
/// This method provides no access to output parameters or the stored
procedure's return value parameter.
///
/// e.g.:
/// SqlDataReader dr = ExecuteReader(connString, "GetOrders", 24, 36);
/// </remarks>
/// <param name="connectionString">a valid connection string for a
SqlConnection</param>
/// <param name="spName">the name of the stored procedure</param>
/// <param name="parameterValues">an array of objects to be assigned as the
input values of the stored procedure</param>
/// <returns>a SqlDataReader containing the resultset generated by the
command</returns>
public static SqlDataReader ExecuteReader(string connectionString, string
spName, params object[] parameterValues)
{
//if we receive parameter values, we need to figure out where they
go
if ((parameterValues != null) && (parameterValues.Length > 0))
{
//pull the parameters for this stored procedure from the
parameter cache (or discover them & populate the cache)
SqlParameter[] commandParameters =
SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
/// <summary>
/// Execute a SqlCommand (that returns a resultset and takes no parameters)
against the provided SqlConnection.
/// </summary>
/// <remarks>
/// e.g.:
/// SqlDataReader dr = ExecuteReader(conn, CommandType.StoredProcedure,
"GetOrders");
/// </remarks>
/// <param name="connection">a valid SqlConnection</param>
/// <param name="commandType">the CommandType (stored procedure, text,
etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL
command</param>
/// <returns>a SqlDataReader containing the resultset generated by the
command</returns>
public static SqlDataReader ExecuteReader(SqlConnection connection,
CommandType commandType, string commandText)
{
//pass through the call providing null for the set of SqlParameters
return ExecuteReader(connection, commandType, commandText,
(SqlParameter[])null);
}
/// <summary>
/// Execute a SqlCommand (that returns a resultset) against the specified
SqlConnection
/// using the provided parameters.
/// </summary>
/// <remarks>
/// e.g.:
/// SqlDataReader dr = ExecuteReader(conn, CommandType.StoredProcedure,
"GetOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="connection">a valid SqlConnection</param>
/// <param name="commandType">the CommandType (stored procedure, text,
etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL
command</param>
/// <param name="commandParameters">an array of SqlParamters used to
execute the command</param>
/// <returns>a SqlDataReader containing the resultset generated by the
command</returns>
public static SqlDataReader ExecuteReader(SqlConnection connection,
CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
//pass through the call to the private overload using a null
transaction value and an externally owned connection
return ExecuteReader(connection, (SqlTransaction)null, commandType,
commandText, commandParameters, SqlConnectionOwnership.External);
}
/// <summary>
/// Execute a stored procedure via a SqlCommand (that returns a resultset)
against the specified SqlConnection
/// using the provided parameter values. This method will query the
database to discover the parameters for the
/// stored procedure (the first time each stored procedure is called), and
assign the values based on parameter order.
/// </summary>
/// <remarks>
/// This method provides no access to output parameters or the stored
procedure's return value parameter.
///
/// e.g.:
/// SqlDataReader dr = ExecuteReader(conn, "GetOrders", 24, 36);
/// </remarks>
/// <param name="connection">a valid SqlConnection</param>
/// <param name="spName">the name of the stored procedure</param>
/// <param name="parameterValues">an array of objects to be assigned as the
input values of the stored procedure</param>
/// <returns>a SqlDataReader containing the resultset generated by the
command</returns>
public static SqlDataReader ExecuteReader(SqlConnection connection, string
spName, params object[] parameterValues)
{
//if we receive parameter values, we need to figure out where they
go
if ((parameterValues != null) && (parameterValues.Length > 0))
{
SqlParameter[] commandParameters =
SqlHelperParameterCache.GetSpParameterSet(connection.ConnectionString, spName);
AssignParameterValues(commandParameters, parameterValues);
/// <summary>
/// Execute a SqlCommand (that returns a resultset) against the specified
SqlTransaction
/// using the provided parameters.
/// </summary>
/// <remarks>
/// e.g.:
/// SqlDataReader dr = ExecuteReader(trans, CommandType.StoredProcedure,
"GetOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="transaction">a valid SqlTransaction</param>
/// <param name="commandType">the CommandType (stored procedure, text,
etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL
command</param>
/// <param name="commandParameters">an array of SqlParamters used to
execute the command</param>
/// <returns>a SqlDataReader containing the resultset generated by the
command</returns>
public static SqlDataReader ExecuteReader(SqlTransaction transaction,
CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
//pass through to private overload, indicating that the connection
is owned by the caller
return ExecuteReader(transaction.Connection, transaction,
commandType, commandText, commandParameters, SqlConnectionOwnership.External);
}
/// <summary>
/// Execute a stored procedure via a SqlCommand (that returns a resultset)
against the specified
/// SqlTransaction using the provided parameter values. This method will
query the database to discover the parameters for the
/// stored procedure (the first time each stored procedure is called), and
assign the values based on parameter order.
/// </summary>
/// <remarks>
/// This method provides no access to output parameters or the stored
procedure's return value parameter.
///
/// e.g.:
/// SqlDataReader dr = ExecuteReader(trans, "GetOrders", 24, 36);
/// </remarks>
/// <param name="transaction">a valid SqlTransaction</param>
/// <param name="spName">the name of the stored procedure</param>
/// <param name="parameterValues">an array of objects to be assigned as the
input values of the stored procedure</param>
/// <returns>a SqlDataReader containing the resultset generated by the
command</returns>
public static SqlDataReader ExecuteReader(SqlTransaction transaction,
string spName, params object[] parameterValues)
{
//if we receive parameter values, we need to figure out where they
go
if ((parameterValues != null) && (parameterValues.Length > 0))
{
SqlParameter[] commandParameters =
SqlHelperParameterCache.GetSpParameterSet(transaction.Connection.ConnectionString,
spName);
AssignParameterValues(commandParameters, parameterValues);
#endregion ExecuteReader
#region ExecuteScalar
/// <summary>
/// Execute a SqlCommand (that returns a 1x1 resultset and takes no
parameters) against the database specified in
/// the connection string.
/// </summary>
/// <remarks>
/// e.g.:
/// int orderCount = (int)ExecuteScalar(connString,
CommandType.StoredProcedure, "GetOrderCount");
/// </remarks>
/// <param name="connectionString">a valid connection string for a
SqlConnection</param>
/// <param name="commandType">the CommandType (stored procedure, text,
etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL
command</param>
/// <returns>an object containing the value in the 1x1 resultset generated
by the command</returns>
public static object ExecuteScalar(string connectionString, CommandType
commandType, string commandText)
{
//pass through the call providing null for the set of SqlParameters
return ExecuteScalar(connectionString, commandType, commandText,
(SqlParameter[])null);
}
/// <summary>
/// Execute a SqlCommand (that returns a 1x1 resultset) against the
database specified in the connection string
/// using the provided parameters.
/// </summary>
/// <remarks>
/// e.g.:
/// int orderCount = (int)ExecuteScalar(connString,
CommandType.StoredProcedure, "GetOrderCount", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">a valid connection string for a
SqlConnection</param>
/// <param name="commandType">the CommandType (stored procedure, text,
etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL
command</param>
/// <param name="commandParameters">an array of SqlParamters used to
execute the command</param>
/// <returns>an object containing the value in the 1x1 resultset generated
by the command</returns>
public static object ExecuteScalar(string connectionString, CommandType
commandType, string commandText, params SqlParameter[] commandParameters)
{
//create & open a SqlConnection, and dispose of it after we are
done.
using (SqlConnection cn = new SqlConnection(connectionString))
{
cn.Open();
/// <summary>
/// Execute a stored procedure via a SqlCommand (that returns a 1x1
resultset) against the database specified in
/// the connection string using the provided parameter values. This method
will query the database to discover the parameters for the
/// stored procedure (the first time each stored procedure is called), and
assign the values based on parameter order.
/// </summary>
/// <remarks>
/// This method provides no access to output parameters or the stored
procedure's return value parameter.
///
/// e.g.:
/// int orderCount = (int)ExecuteScalar(connString, "GetOrderCount", 24,
36);
/// </remarks>
/// <param name="connectionString">a valid connection string for a
SqlConnection</param>
/// <param name="spName">the name of the stored procedure</param>
/// <param name="parameterValues">an array of objects to be assigned as the
input values of the stored procedure</param>
/// <returns>an object containing the value in the 1x1 resultset generated
by the command</returns>
public static object ExecuteScalar(string connectionString, string spName,
params object[] parameterValues)
{
//if we receive parameter values, we need to figure out where they
go
if ((parameterValues != null) && (parameterValues.Length > 0))
{
//pull the parameters for this stored procedure from the
parameter cache (or discover them & populate the cache)
SqlParameter[] commandParameters =
SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
/// <summary>
/// Execute a SqlCommand (that returns a 1x1 resultset and takes no
parameters) against the provided SqlConnection.
/// </summary>
/// <remarks>
/// e.g.:
/// int orderCount = (int)ExecuteScalar(conn, CommandType.StoredProcedure,
"GetOrderCount");
/// </remarks>
/// <param name="connection">a valid SqlConnection</param>
/// <param name="commandType">the CommandType (stored procedure, text,
etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL
command</param>
/// <returns>an object containing the value in the 1x1 resultset generated
by the command</returns>
public static object ExecuteScalar(SqlConnection connection, CommandType
commandType, string commandText)
{
//pass through the call providing null for the set of SqlParameters
return ExecuteScalar(connection, commandType, commandText,
(SqlParameter[])null);
}
/// <summary>
/// Execute a SqlCommand (that returns a 1x1 resultset) against the
specified SqlConnection
/// using the provided parameters.
/// </summary>
/// <remarks>
/// e.g.:
/// int orderCount = (int)ExecuteScalar(conn, CommandType.StoredProcedure,
"GetOrderCount", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="connection">a valid SqlConnection</param>
/// <param name="commandType">the CommandType (stored procedure, text,
etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL
command</param>
/// <param name="commandParameters">an array of SqlParamters used to
execute the command</param>
/// <returns>an object containing the value in the 1x1 resultset generated
by the command</returns>
public static object ExecuteScalar(SqlConnection connection, CommandType
commandType, string commandText, params SqlParameter[] commandParameters)
{
//create a command and prepare it for execution
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, connection, (SqlTransaction)null, commandType,
commandText, commandParameters);
/// <summary>
/// Execute a stored procedure via a SqlCommand (that returns a 1x1
resultset) against the specified SqlConnection
/// using the provided parameter values. This method will query the
database to discover the parameters for the
/// stored procedure (the first time each stored procedure is called), and
assign the values based on parameter order.
/// </summary>
/// <remarks>
/// This method provides no access to output parameters or the stored
procedure's return value parameter.
///
/// e.g.:
/// int orderCount = (int)ExecuteScalar(conn, "GetOrderCount", 24, 36);
/// </remarks>
/// <param name="connection">a valid SqlConnection</param>
/// <param name="spName">the name of the stored procedure</param>
/// <param name="parameterValues">an array of objects to be assigned as the
input values of the stored procedure</param>
/// <returns>an object containing the value in the 1x1 resultset generated
by the command</returns>
public static object ExecuteScalar(SqlConnection connection, string spName,
params object[] parameterValues)
{
//if we receive parameter values, we need to figure out where they
go
if ((parameterValues != null) && (parameterValues.Length > 0))
{
//pull the parameters for this stored procedure from the
parameter cache (or discover them & populate the cache)
SqlParameter[] commandParameters =
SqlHelperParameterCache.GetSpParameterSet(connection.ConnectionString, spName);
/// <summary>
/// Execute a SqlCommand (that returns a 1x1 resultset and takes no
parameters) against the provided SqlTransaction.
/// </summary>
/// <remarks>
/// e.g.:
/// int orderCount = (int)ExecuteScalar(trans,
CommandType.StoredProcedure, "GetOrderCount");
/// </remarks>
/// <param name="transaction">a valid SqlTransaction</param>
/// <param name="commandType">the CommandType (stored procedure, text,
etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL
command</param>
/// <returns>an object containing the value in the 1x1 resultset generated
by the command</returns>
public static object ExecuteScalar(SqlTransaction transaction, CommandType
commandType, string commandText)
{
//pass through the call providing null for the set of SqlParameters
return ExecuteScalar(transaction, commandType, commandText,
(SqlParameter[])null);
}
/// <summary>
/// Execute a SqlCommand (that returns a 1x1 resultset) against the
specified SqlTransaction
/// using the provided parameters.
/// </summary>
/// <remarks>
/// e.g.:
/// int orderCount = (int)ExecuteScalar(trans,
CommandType.StoredProcedure, "GetOrderCount", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="transaction">a valid SqlTransaction</param>
/// <param name="commandType">the CommandType (stored procedure, text,
etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL
command</param>
/// <param name="commandParameters">an array of SqlParamters used to
execute the command</param>
/// <returns>an object containing the value in the 1x1 resultset generated
by the command</returns>
public static object ExecuteScalar(SqlTransaction transaction, CommandType
commandType, string commandText, params SqlParameter[] commandParameters)
{
//create a command and prepare it for execution
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, transaction.Connection, transaction,
commandType, commandText, commandParameters);
/// <summary>
/// Execute a stored procedure via a SqlCommand (that returns a 1x1
resultset) against the specified
/// SqlTransaction using the provided parameter values. This method will
query the database to discover the parameters for the
/// stored procedure (the first time each stored procedure is called), and
assign the values based on parameter order.
/// </summary>
/// <remarks>
/// This method provides no access to output parameters or the stored
procedure's return value parameter.
///
/// e.g.:
/// int orderCount = (int)ExecuteScalar(trans, "GetOrderCount", 24, 36);
/// </remarks>
/// <param name="transaction">a valid SqlTransaction</param>
/// <param name="spName">the name of the stored procedure</param>
/// <param name="parameterValues">an array of objects to be assigned as the
input values of the stored procedure</param>
/// <returns>an object containing the value in the 1x1 resultset generated
by the command</returns>
public static object ExecuteScalar(SqlTransaction transaction, string
spName, params object[] parameterValues)
{
//if we receive parameter values, we need to figure out where they
go
if ((parameterValues != null) && (parameterValues.Length > 0))
{
//pull the parameters for this stored procedure from the
parameter cache (or discover them & populate the cache)
SqlParameter[] commandParameters =
SqlHelperParameterCache.GetSpParameterSet(transaction.Connection.ConnectionString,
spName);
#endregion ExecuteScalar
#region ExecuteXmlReader
/// <summary>
/// Execute a SqlCommand (that returns a resultset and takes no parameters)
against the provided SqlConnection.
/// </summary>
/// <remarks>
/// e.g.:
/// XmlReader r = ExecuteXmlReader(conn, CommandType.StoredProcedure,
"GetOrders");
/// </remarks>
/// <param name="connection">a valid SqlConnection</param>
/// <param name="commandType">the CommandType (stored procedure, text,
etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL command
using "FOR XML AUTO"</param>
/// <returns>an XmlReader containing the resultset generated by the
command</returns>
public static XmlReader ExecuteXmlReader(SqlConnection connection,
CommandType commandType, string commandText)
{
//pass through the call providing null for the set of SqlParameters
return ExecuteXmlReader(connection, commandType, commandText,
(SqlParameter[])null);
}
/// <summary>
/// Execute a SqlCommand (that returns a resultset) against the specified
SqlConnection
/// using the provided parameters.
/// </summary>
/// <remarks>
/// e.g.:
/// XmlReader r = ExecuteXmlReader(conn, CommandType.StoredProcedure,
"GetOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="connection">a valid SqlConnection</param>
/// <param name="commandType">the CommandType (stored procedure, text,
etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL command
using "FOR XML AUTO"</param>
/// <param name="commandParameters">an array of SqlParamters used to
execute the command</param>
/// <returns>an XmlReader containing the resultset generated by the
command</returns>
public static XmlReader ExecuteXmlReader(SqlConnection connection,
CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
//create a command and prepare it for execution
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, connection, (SqlTransaction)null, commandType,
commandText, commandParameters);
/// <summary>
/// Execute a stored procedure via a SqlCommand (that returns a resultset)
against the specified SqlConnection
/// using the provided parameter values. This method will query the
database to discover the parameters for the
/// stored procedure (the first time each stored procedure is called), and
assign the values based on parameter order.
/// </summary>
/// <remarks>
/// This method provides no access to output parameters or the stored
procedure's return value parameter.
///
/// e.g.:
/// XmlReader r = ExecuteXmlReader(conn, "GetOrders", 24, 36);
/// </remarks>
/// <param name="connection">a valid SqlConnection</param>
/// <param name="spName">the name of the stored procedure using "FOR XML
AUTO"</param>
/// <param name="parameterValues">an array of objects to be assigned as the
input values of the stored procedure</param>
/// <returns>an XmlReader containing the resultset generated by the
command</returns>
public static XmlReader ExecuteXmlReader(SqlConnection connection, string
spName, params object[] parameterValues)
{
//if we receive parameter values, we need to figure out where they
go
if ((parameterValues != null) && (parameterValues.Length > 0))
{
//pull the parameters for this stored procedure from the
parameter cache (or discover them & populate the cache)
SqlParameter[] commandParameters =
SqlHelperParameterCache.GetSpParameterSet(connection.ConnectionString, spName);
/// <summary>
/// Execute a SqlCommand (that returns a resultset and takes no parameters)
against the provided SqlTransaction.
/// </summary>
/// <remarks>
/// e.g.:
/// XmlReader r = ExecuteXmlReader(trans, CommandType.StoredProcedure,
"GetOrders");
/// </remarks>
/// <param name="transaction">a valid SqlTransaction</param>
/// <param name="commandType">the CommandType (stored procedure, text,
etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL command
using "FOR XML AUTO"</param>
/// <returns>an XmlReader containing the resultset generated by the
command</returns>
public static XmlReader ExecuteXmlReader(SqlTransaction transaction,
CommandType commandType, string commandText)
{
//pass through the call providing null for the set of SqlParameters
return ExecuteXmlReader(transaction, commandType, commandText,
(SqlParameter[])null);
}
/// <summary>
/// Execute a SqlCommand (that returns a resultset) against the specified
SqlTransaction
/// using the provided parameters.
/// </summary>
/// <remarks>
/// e.g.:
/// XmlReader r = ExecuteXmlReader(trans, CommandType.StoredProcedure,
"GetOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="transaction">a valid SqlTransaction</param>
/// <param name="commandType">the CommandType (stored procedure, text,
etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL command
using "FOR XML AUTO"</param>
/// <param name="commandParameters">an array of SqlParamters used to
execute the command</param>
/// <returns>an XmlReader containing the resultset generated by the
command</returns>
public static XmlReader ExecuteXmlReader(SqlTransaction transaction,
CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
//create a command and prepare it for execution
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, transaction.Connection, transaction,
commandType, commandText, commandParameters);
/// <summary>
/// Execute a stored procedure via a SqlCommand (that returns a resultset)
against the specified
/// SqlTransaction using the provided parameter values. This method will
query the database to discover the parameters for the
/// stored procedure (the first time each stored procedure is called), and
assign the values based on parameter order.
/// </summary>
/// <remarks>
/// This method provides no access to output parameters or the stored
procedure's return value parameter.
///
/// e.g.:
/// XmlReader r = ExecuteXmlReader(trans, "GetOrders", 24, 36);
/// </remarks>
/// <param name="transaction">a valid SqlTransaction</param>
/// <param name="spName">the name of the stored procedure</param>
/// <param name="parameterValues">an array of objects to be assigned as the
input values of the stored procedure</param>
/// <returns>a dataset containing the resultset generated by the
command</returns>
public static XmlReader ExecuteXmlReader(SqlTransaction transaction, string
spName, params object[] parameterValues)
{
//if we receive parameter values, we need to figure out where they
go
if ((parameterValues != null) && (parameterValues.Length > 0))
{
//pull the parameters for this stored procedure from the
parameter cache (or discover them & populate the cache)
SqlParameter[] commandParameters =
SqlHelperParameterCache.GetSpParameterSet(transaction.Connection.ConnectionString,
spName);
#endregion ExecuteXmlReader
}
/// <summary>
/// SqlHelperParameterCache provides functions to leverage a static cache of
procedure parameters, and the
/// ability to discover parameters for stored procedures at run-time.
/// </summary>
public sealed class SqlHelperParameterCache
{
#region private methods, variables, and constructors
//Since this class provides only static methods, make the default
constructor private to prevent
//instances from being created with "new SqlHelperParameterCache()".
private SqlHelperParameterCache() {}
/// <summary>
/// resolve at run time the appropriate set of SqlParameters for a stored
procedure
/// </summary>
/// <param name="connectionString">a valid connection string for a
SqlConnection</param>
/// <param name="spName">the name of the stored procedure</param>
/// <param name="includeReturnValueParameter">whether or not to include
their return value parameter</param>
/// <returns></returns>
private static SqlParameter[] DiscoverSpParameterSet(string
connectionString, string spName, bool includeReturnValueParameter)
{
using (SqlConnection cn = new SqlConnection(connectionString))
using (SqlCommand cmd = new SqlCommand(spName,cn))
{
cn.Open();
cmd.CommandType = CommandType.StoredProcedure;
SqlCommandBuilder.DeriveParameters(cmd);
if (!includeReturnValueParameter)
{
cmd.Parameters.RemoveAt(0);
}
cmd.Parameters.CopyTo(discoveredParameters, 0);
return discoveredParameters;
}
}
return clonedParameters;
}
/// <summary>
/// add parameter array to the cache
/// </summary>
/// <param name="connectionString">a valid connection string for a
SqlConnection</param>
/// <param name="commandText">the stored procedure name or T-SQL
command</param>
/// <param name="commandParameters">an array of SqlParamters to be
cached</param>
public static void CacheParameterSet(string connectionString, string
commandText, params SqlParameter[] commandParameters)
{
string hashKey = connectionString + ":" + commandText;
paramCache[hashKey] = commandParameters;
}
/// <summary>
/// retrieve a parameter array from the cache
/// </summary>
/// <param name="connectionString">a valid connection string for a
SqlConnection</param>
/// <param name="commandText">the stored procedure name or T-SQL
command</param>
/// <returns>an array of SqlParamters</returns>
public static SqlParameter[] GetCachedParameterSet(string connectionString,
string commandText)
{
string hashKey = connectionString + ":" + commandText;
SqlParameter[] cachedParameters =
(SqlParameter[])paramCache[hashKey];
if (cachedParameters == null)
{
return null;
}
else
{
return CloneParameters(cachedParameters);
}
}
/// <summary>
/// Retrieves the set of SqlParameters appropriate for the stored procedure
/// </summary>
/// <remarks>
/// This method will query the database for this information, and then
store it in a cache for future requests.
/// </remarks>
/// <param name="connectionString">a valid connection string for a
SqlConnection</param>
/// <param name="spName">the name of the stored procedure</param>
/// <returns>an array of SqlParameters</returns>
public static SqlParameter[] GetSpParameterSet(string connectionString,
string spName)
{
return GetSpParameterSet(connectionString, spName, false);
}
/// <summary>
/// Retrieves the set of SqlParameters appropriate for the stored procedure
/// </summary>
/// <remarks>
/// This method will query the database for this information, and then
store it in a cache for future requests.
/// </remarks>
/// <param name="connectionString">a valid connection string for a
SqlConnection</param>
/// <param name="spName">the name of the stored procedure</param>
/// <param name="includeReturnValueParameter">a bool value indicating
whether the return value parameter should be included in the results</param>
/// <returns>an array of SqlParameters</returns>
public static SqlParameter[] GetSpParameterSet(string connectionString,
string spName, bool includeReturnValueParameter)
{
string hashKey = connectionString + ":" + spName +
(includeReturnValueParameter ? ":include ReturnValue Parameter":"");
SqlParameter[] cachedParameters;
cachedParameters = (SqlParameter[])paramCache[hashKey];
if (cachedParameters == null)
{
cachedParameters = (SqlParameter[])(paramCache[hashKey] =
DiscoverSpParameterSet(connectionString, spName, includeReturnValueParameter));
}
return CloneParameters(cachedParameters);
}
}
}
ApplicationStatus.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
using System.Data;
using System.Configuration;
using System.Collections;
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;
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
Page.Validate();
if (Page.IsValid)
{
LoginObj .UserName = Session["UserName"].ToString();
LoginObj.OldPassword = txtOldPassword.Text.Trim();
if (LoginObj.CheckEmpOldPassword() == true)
{
if (txtNewPassword.Text.Trim() == txtOldPassword.Text.Trim())
{
lblError.Text = "New Password should not be same as Old Password";
lblError.Visible = true;
lblSuccess.Visible = false;
}
else
{
LoginObj .Password = txtNewPassword.Text.Trim();
LoginObj.EmpChangeOldPassword ();
lblSuccess.Visible = true;
lblSuccess.Text = "Your Password has been changed successfully!";
lblError.Visible = false;
}
}
else
{
lblError.Text = "Invalid Old Password";
lblError.Visible = true;
lblSuccess.Visible = false;
}
}
}
}
EmployeeHome.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
}
}
EmployeeLogin.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
LoginObj.UserName = txtLoginId.Text.Trim();
LoginObj.Password = txtPassword.Text.Trim();
DataSet dsEmpLoginDetail = LoginObj.GetEmpLoginDetails ();
DataRowCollection drc = dsEmpLoginDetail.Tables[0].Rows;
if (drc.Count > 0)
{
lblError.Visible = false;
DataRow dr = drc[0];
Session["UserName"] = dr["UserName"].ToString();
//Session["EmpLoginId"] = dr["EmpLoginId"];
}
else
{
lblError.Visible = true;
lblError.Text = "Invalid Login ID/Password";
}
}
catch (Exception ex)
{
lblError.Visible = true;
lblError.Text = ex.Message.ToString();
}
Response.Redirect("frmEmployeeHome.aspx");
employeeLogout.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
Leaveapplication.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
if (Session["UserName"] == null)
{
Response.Redirect("frmEmployeeLogin.aspx");
}
else
txtUserName.Text = Session["UserName"].ToString();
if (!IsPostBack)
{
BindLeavetype();
}
GMDStartDate.Attributes.Add("readonly", "readOnly()");
}
void BindLeavetype()
{
DataSet dsLeavetype = objLeave.GetLeaveType();
ddlLeaveType.DataSource = dsLeavetype.Tables[0];
ddlLeaveType.DataTextField = "LeaveTypeName";
ddlLeaveType.DataValueField = "LeaveTypeId";
ddlLeaveType.DataBind();
ddlLeaveType.Items.Insert(0, "Select");
}
updateEmployee.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
if (!IsPostBack)
{
BindDept();
BindCountry();
// BindStatus();
BindState();
BindCity();
BindDesignation();
BindData();
}
}
void BindCountry()
{
DataSet dsCountry = objCommon.GetCountryName();
ddlCountryName.DataSource = dsCountry.Tables[0];
ddlCountryName.DataTextField = "CountryName";
ddlCountryName.DataValueField = "CountryId";
ddlCountryName.DataBind();
ddlCountryName.Items.Insert(0, "Select");
}
void BindState()
{
DataSet dsState = objCommon.GetStateName();
ddlStateName.DataSource = dsState.Tables[0];
ddlStateName.DataTextField = "StateName";
ddlStateName.DataValueField = "StateId";
ddlStateName.DataBind();
ddlStateName.Items.Insert(0, "Select");
}
void BindCity()
{
DataSet dsCity = objCommon.GetCityName();
ddlCityName.DataSource = dsCity.Tables[0];
ddlCityName.DataTextField = "CityName";
ddlCityName.DataValueField = "CityId";
ddlCityName.DataBind();
ddlCityName.Items.Insert(0, "Select");
}
void BindDept()
{
DataSet dsDept = objCommon.GetDeptName();
ddlDept.DataSource = dsDept.Tables[0];
ddlDept.DataTextField = "DeptName";
ddlDept.DataValueField = "DeptId";
ddlDept.DataBind();
ddlDept.Items.Insert(0, "Select");
}
void BindDesignation()
{
DataSet dsDesgn = objCommon.GetDesignation();
ddlDesig.DataSource = dsDesgn.Tables[0];
ddlDesig.DataTextField = "DesigType";
// ddlDesig.DataValueField = "DesigId";
ddlDesig.DataBind();
ddlDesig.Items.Insert(0, "Select");
}
int CountryIndex =
ddlCountryName.Items.IndexOf(ddlCountryName.Items.FindByValue(EmpObj.CountryId.ToString()
));
if (CountryIndex >= 0)
ddlCountryName.Items[CountryIndex].Selected = true;
int StateIndex =
ddlStateName.Items.IndexOf(ddlStateName.Items.FindByValue(EmpObj.StateId.ToString()));
if (StateIndex >= 0)
ddlStateName.Items[StateIndex].Selected = true;
int CityIndex =
ddlCityName.Items.IndexOf(ddlCityName.Items.FindByValue(EmpObj.CityId.ToString()));
if (CityIndex >= 0)
ddlCityName.Items[CityIndex].Selected = true;
int DesigIndex =
ddlDesig.Items.IndexOf(ddlDesig.Items.FindByText(EmpObj.Designation.ToString()));
if (DesigIndex >= 0)
ddlDesig.Items[DesigIndex].Selected = true;
}
Employeemaster
<title>Untitled Page</title>
</head>
<body>
</asp:ScriptManager>
<div>
<tr>
<tr>
<tr>
<tr>
</tr>
</table></td>
<td></td>
</tr>
</table></td>
</tr>
<tr>
<td>
</td>
</tr>
</table></td>
</tr>
<tr>
<div style="height:408px">
</asp:contentplaceholder>
</div>
</td>
</tr>
<tr>
</tr>
</table>
</div>
</form>
</body>
</html>