Create dbBroker Class which contain database connection string (dbBroker.cs).
using System;
using MVCLibDefination;
using MVCLibFramwork;
namespace MVCLibBusinessObjects
{
/// <summary>
/// Summary description for dbBroker.
/// </summary>
public class dbBroker
{
public String getConnectionString()
{
String strConn="";
strConn="server=pol005;user Id=haritha;password=haritha;database=haritha;Pooling=yes;Min Pool Size=0;Max Pool Size=25;Connect Timeout=90";
return strConn;
}
}
}
4. Create CityMaster Entity class (file name CityMaster.cs).
using System;
using MVCLibBusinessObjects;
using MVCLibDefination;
using MVCLibFramwork;
namespace MVCLibBusinessObjects
{
/// <summary>
/// Summary description for CityMaster.
/// </summary>
public class CityMaster : BusinessEntity
{
private int vCityCode=0;
private String vCityName="";
public int CityCode
{
get
{
return vCityCode;
}
set
{
vCityCode=value;
}
}
public String CityName
{
get
{
return vCityName;
}
set
{
vCityName=value;
}
}
}
}
5. Create CityMaster Business Class (CityMasterBO.cs).
Note: We are making use of "Microsoft.ApplicationBlocks.Data" namespace in below class
using System;
using MVCLibDefination;
using MVCLibFramwork;
using Microsoft.ApplicationBlocks.Data;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Web;
using System.Web.UI;
namespace MVCLibBusinessObjects
{
/// <summary>
/// Summary description for CityMasterBO.
/// </summary>
public class CityMasterBO : BusinessLogic, IbusinessLogic
{
String strConn="";
String strQuery="";
CityMaster varCityMaster=new CityMaster();
ArrayList IBusiness.GetData(object vCityMaster)
{
ArrayList arylist=new ArrayList();
varCityMaster=(CityMaster) vCityMaster;
dbBroker dbb=new dbBroker();
strConn= dbb.getConnectionString(); //dbb.getConnectionStr.ToString();
try
{
if (varCityMaster.CityCode==0)
{
strQuery="select citycode,cityname from citymaster";
}
else
{
strQuery="select citycode,cityname from citymaster where
citycode=" + varCityMaster.CityCode ;
}
SqlDataReader rdrSql;
rdrSql=SqlHelper.ExecuteReader(strConn,CommandType.Text,strQuery);
while (rdrSql.Read())
{
int intCityCode=rdrSql.GetInt32(0);
String strCityName=rdrSql.GetString(1);
CityMaster cm=new CityMaster();
cm.CityCode=intCityCode;
cm.CityName=strCityName;
arylist.Add(cm);
}
rdrSql.Close();
}
catch(Exception exp)
{
HttpContext.Current.Response.Write("Error : " + exp.ToString());
}
return arylist;
}
bool IBusiness.DataExists(object vCityMaster)
{
bool blnflag=false;
try
{
dbBroker dbb=new dbBroker();
strConn= dbb.getConnectionString(); //dbb.getConnectionStr.ToString();
//HttpContext.Current.Response.Write("Conn : " + strConn);
varCityMaster=(CityMaster) vCityMaster;
strQuery="select count(*) from CityMaster where cityname='" + varCityMaster.CityName.ToUpper().Trim() + "'";
int intCnt=0;
//HttpContext.Current.Response.Write("Query : " + strQuery);
intCnt= (int) SqlHelper.ExecuteScalar(strConn,CommandType.Text,strQuery);
if (intCnt>0)
{
blnflag=true;
}
}
catch(Exception exp)
{
HttpContext.Current.Response.Write("Error : " + exp.ToString());
}
return blnflag;
}
bool IBusiness.Insert(object vCityMaster)
{
bool blnflag=false;
dbBroker dbb=new dbBroker();
strConn= dbb.getConnectionString(); //dbb.getConnectionStr.ToString();
SqlConnection cnnCon=new SqlConnection();
cnnCon.ConnectionString=strConn;
if (cnnCon.State==ConnectionState.Closed)
{
cnnCon.Open();
}
SqlTransaction sqlTrans=cnnCon.BeginTransaction();
try
{
varCityMaster=(CityMaster) vCityMaster;
strQuery="insert into CityMaster(cityname) values('" + varCityMaster.CityName.ToUpper().ToString() + "')";
SqlHelper.ExecuteNonQuery(sqlTrans,CommandType.Text,strQuery);
sqlTrans.Commit();
blnflag=true;
}
catch(Exception exp)
{
HttpContext.Current.Response.Write("Error : " + exp.ToString());
sqlTrans.Rollback();
}
return blnflag;
}
bool IBusiness.Update(object vCityMaster)
{
bool blnflag=false;
dbBroker dbb=new dbBroker();
strConn= dbb.getConnectionString(); //dbb.getConnectionStr.ToString();
SqlConnection cnnCon=new SqlConnection();
cnnCon.ConnectionString=strConn;
if (cnnCon.State==ConnectionState.Closed)
{
cnnCon.Open();
}
SqlTransaction sqlTrans=cnnCon.BeginTransaction();
try
{
varCityMaster=(CityMaster) vCityMaster;
strQuery="update CityMaster set cityname='"+ varCityMaster.CityName.ToUpper().ToString() + "' where citycode="+ varCityMaster.CityCode;
SqlHelper.ExecuteNonQuery(sqlTrans,CommandType.Text,strQuery);
sqlTrans.Commit();
blnflag=true;
}
catch(Exception exp)
{
HttpContext.Current.Response.Write("Error : " + exp.ToString());
sqlTrans.Rollback();
}
return blnflag;
}
bool IBusiness.Delete(object vCityMaster)
{
bool blnflag=false;
dbBroker dbb=new dbBroker();
strConn= dbb.getConnectionString(); //dbb.getConnectionStr.ToString();
SqlConnection cnnCon=new SqlConnection();
cnnCon.ConnectionString=strConn;
if (cnnCon.State==ConnectionState.Closed)
{
cnnCon.Open();
}
SqlTransaction sqlTrans=cnnCon.BeginTransaction();
try
{
varCityMaster=(CityMaster) vCityMaster;
strQuery="delete from CityMaster where citycode="+ varCityMaster.CityCode;
SqlHelper.ExecuteNonQuery(sqlTrans,CommandType.Text,strQuery);
sqlTrans.Commit();
blnflag=true;
}
catch(Exception exp)
{
HttpContext.Current.Response.Write("Error : " + exp.ToString());
sqlTrans.Rollback();
}
return blnflag;
}
}
}