SqlHelper Class
-----------------------
public static SqlConnection GetConnection()
{
ConnectionStringSettings settings =
ConfigurationManager. ConnectionStrings[" dbConnString"];
return new System.Data.SqlClient. SqlConnection(settings. ConnectionString);
}
public class CategoryBL
{
CategoryDL categoryDL = new CategoryDL();
public CategoryBL()
{
//
// TODO: Add constructor logic here
//
}
public int CategoryID
{
get;
set;
}
public String CategoryName
{
get;
set;
}
public String CategoryIDs
{
get;
set;
}
public String Search
{
get;
set;
}
public int CategoryMasterInsert()
{
return categoryDL. CategoryMasterInsert( CategoryName);
}
public int CategoryMasterUpdate()
{
return categoryDL. CategoryMasterUpdate( CategoryID, CategoryName);
}
public int CategoryMasterDelete()
{
return categoryDL. CategoryMasterDelete( CategoryIDs);
}
public DataSet CategoryMasterSelectAll()
{
return categoryDL. CategoryMasterSelectAll( Search);
}
public DataSet CategoryMasterSelectByID()
{
return categoryDL. CategoryMasterSelectByID( CategoryID);
}
}
public class CategoryDL
{
public CategoryDL()
{
//
// TODO: Add constructor logic here
//
}
public int CategoryMasterInsert(string CategoryName)
{
int result = 0;
SqlConnection sqlConnection = SQLHelper.GetConnection();
SqlCommand sqlCommand = new SqlCommand(" CategoryMasterInsert", sqlConnection);
sqlCommand.CommandType = CommandType.StoredProcedure;
SqlParameter spCategoryName = new SqlParameter("@CategoryName", SqlDbType.VarChar, 50);
spCategoryName.Value = CategoryName;
sqlCommand.Parameters.Add( spCategoryName);
try
{
sqlConnection.Open();
result = sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
}
catch
{
}
return result;
}
public int CategoryMasterUpdate(int CategoryID, string CategoryName)
{
int result = 0;
SqlConnection sqlConnection = SQLHelper.GetConnection();
SqlCommand sqlCommand = new SqlCommand(" CategoryMasterUpdate", sqlConnection);
sqlCommand.CommandType = CommandType.StoredProcedure;
SqlParameter spCategoryID = new SqlParameter("@CategoryID", SqlDbType.Int);
spCategoryID.Value = CategoryID;
sqlCommand.Parameters.Add( spCategoryID);
SqlParameter spCategoryName = new SqlParameter("@CategoryName", SqlDbType.VarChar, 50);
spCategoryName.Value = CategoryName;
sqlCommand.Parameters.Add( spCategoryName);
try
{
sqlConnection.Open();
result = sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
}
catch
{
}
return result;
}
public int CategoryMasterDelete(string CategoryIDs)
{
int result = 0;
SqlConnection sqlConnection = SQLHelper.GetConnection();
SqlCommand sqlCommand = new SqlCommand(" CategoryMasterDelete", sqlConnection);
sqlCommand.CommandType = CommandType.StoredProcedure;
SqlParameter spCategoryIDs = new SqlParameter("@CategoryIDs", SqlDbType.NChar, 512);
spCategoryIDs.Value = CategoryIDs;
sqlCommand.Parameters.Add( spCategoryIDs);
try
{
sqlConnection.Open();
result = sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
}
catch
{
}
return result;
}
public DataSet CategoryMasterSelectByID(int CategoryID)
{
SqlConnection sqlConnection = SQLHelper.GetConnection();
SqlCommand sqlCommand = new SqlCommand(" CategoryMasterSelectByID", sqlConnection);
sqlCommand.CommandType = CommandType.StoredProcedure;
SqlParameter spCategoryID = new SqlParameter("@CategoryID", SqlDbType.Int);
spCategoryID.Value = CategoryID;
sqlCommand.Parameters.Add( spCategoryID);
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
DataSet dataset = new DataSet();
try
{
sqlDataAdapter.Fill(dataset);
}
catch
{
}
return dataset;
}
public DataSet CategoryMasterSelectAll(string Search)
{
SqlConnection sqlConnection = SQLHelper.GetConnection();
SqlCommand sqlCommand = new SqlCommand(" CategoryMasterSelectAll", sqlConnection);
sqlCommand.CommandType = CommandType.StoredProcedure;
SqlParameter spSearch = new SqlParameter("@Search", SqlDbType.VarChar, 50);
spSearch.Value = Search;
sqlCommand.Parameters.Add( spSearch);
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
DataSet dataset = new DataSet();
try
{
sqlDataAdapter.Fill(dataset);
}
catch
{
}
return dataset;
}
}
public partial class Category : System.Web.UI.Page
{
CategoryBL categoryBL = new CategoryBL();
DataSet dsGrid = new DataSet();
DataSet dsFV = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
void BindGrid()
{
categoryBL.Search = txtSearch.Text.Trim();
dsGrid = categoryBL. CategoryMasterSelectAll();
gvData.DataSource = dsGrid;
gvData.DataBind();
txtSearch.Focus();
}
protected void btnAdd_Click(object sender, EventArgs e)
{
pnlSearch.Visible = false;
pnlAddEdit.Visible = true;
fvAddEdit.ChangeMode( FormViewMode.Insert);
fvAddEdit.DataBind();
((TextBox)fvAddEdit. FindControl("txtCategoryName") ).Focus();
}
protected void btnDelete_Click(object sender, EventArgs e)
{
string categoryIDs = "";
//foreach (GridDataItem selectedItem in rgData.MasterTableView.Items)
//{
// if (selectedItem.Selected)
// {
// categoryIDs += ((HiddenField)selectedItem. FindControl("hfCategoryID")). Value + ",";
// }
//}
categoryIDs = ((Button)sender). CommandArgument.ToString() + ",";
if (categoryIDs.Length > 0)
{
categoryBL.CategoryIDs = categoryIDs;
int a = categoryBL. CategoryMasterDelete();
if (a > 0)
{
BindGrid();
}
}
}
protected void btnSearch_Click(object sender, EventArgs e)
{
BindGrid();
txtSearch.Focus();
}
protected void btnEdit_Click(object sender, EventArgs e)
{
pnlSearch.Visible = false;
pnlAddEdit.Visible = true;
fvAddEdit.ChangeMode( FormViewMode.Edit);
categoryBL.CategoryID = Convert.ToInt32(((Button) sender).CommandArgument. ToString());
dsFV = categoryBL. CategoryMasterSelectByID();
fvAddEdit.DataSource = dsFV;
fvAddEdit.DataBind();
((TextBox)fvAddEdit. FindControl("txtCategoryName") ).Focus();
}
protected void btnSave_Click(object sender, EventArgs e)
{
categoryBL.CategoryName = ((TextBox)fvAddEdit. FindControl("txtCategoryName") ).Text;
int a = categoryBL. CategoryMasterInsert();
if (a > 0)
{
pnlSearch.Visible = true;
pnlAddEdit.Visible = false;
BindGrid();
}
}
protected void btnCancel_Click(object sender, EventArgs e)
{
pnlSearch.Visible = true;
pnlAddEdit.Visible = false;
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
categoryBL.CategoryID = Convert.ToInt32(((HiddenField) fvAddEdit.FindControl(" hfCategoryID")).Value. ToString());
categoryBL.CategoryName = ((TextBox)fvAddEdit. FindControl("txtCategoryName") ).Text;
int a = categoryBL. CategoryMasterUpdate();
if (a > 0)
{
pnlSearch.Visible = true;
pnlAddEdit.Visible = false;
BindGrid();
}
}
protected void gvData_Sorting(object sender, GridViewSortEventArgs e)
{
BindGrid();
DataSet dataTable = gvData.DataSource as DataSet;
if (dataTable != null)
{
DataView dataView = new DataView(dataTable.Tables[0]);
if (ViewState["sortExpression"] == null)
{
ViewState["sortExpression"] = "DESC";
}
else
{
if (ViewState["sortExpression"]. ToString() == "ASC")
{
ViewState["sortExpression"] = "DESC";
}
else
{
ViewState["sortExpression"] = "ASC";
}
}
dataView.Sort = e.SortExpression + " " + Convert.ToString(ViewState[" sortExpression"]);
gvData.DataSource = dataView;
gvData.DataBind();
}
}
protected void gvData_PageIndexChanging( object sender, GridViewPageEventArgs e)
{
gvData.PageIndex = e.NewPageIndex;
BindGrid();
}
}
No comments:
Post a Comment