Important


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DemoBAL;

namespace DemoPresentation
{
public partial class _Default : System.Web.UI.Page
{
#region “Page Events”
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid(“0”);
Session[“dtCategory”] = null;
lblMessage.Text = string.Empty;
}
lblMessage.Text = string.Empty;
}
#endregion

#region “Button Events”
protected void btnPrevious_Click(object sender, EventArgs e)
{
BindGrid(false);
}

protected void btnNext_Click(object sender, EventArgs e)
{
BindGrid(true);
}

protected void btnSave_Click(object sender, EventArgs e)
{
if (Session[“dtCategory”] != null)
{
int count = 0;
DataTable dtCategory = (DataTable)Session[“dtCategory”];
foreach (GridViewRow row in grdProducts.Rows)
{
dtCategory.Rows[count][“CategoryName”] = ((TextBox)row.FindControl(“txtCategoryName”)).Text;
count += 1;
}
Session[“dtCategory”] = null;
count = ProductBAL.Update(dtCategory, Convert.ToInt32(lblCurrentPage.Text) – 1, grdProducts.PageSize);
BindGrid(lblCurrentPage.Text);
lblMessage.Text = “[ ” + count + ” ]Record Update!”;
}
}
#endregion

#region “Gridview Events”
protected void grdProducts_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == “cmdExpand”)
{
int id = Convert.ToInt32(e.CommandArgument);
if (id >= Convert.ToInt32(grdProducts.PageSize))
{
id = id – (Convert.ToInt32(grdProducts.PageSize) * Convert.ToInt32(grdProducts.PageIndex));
}

decimal CategoryId = Convert.ToDecimal(grdProducts.DataKeys[id][“CategoryId”]);
BindSubGrid(((GridView)grdProducts.Rows[id].FindControl(“grdSubProducts”)), CategoryId, id);
}
}

#endregion

#region “Private Methods”
private void BindGrid(string CurrentPage)
{
lblCurrentPage.Text = CurrentPage != “0” ? (Convert.ToInt32(CurrentPage) – 1).ToString() : CurrentPage;
DataTable dtCategory = CategoryBAL.Get(0, Convert.ToInt32(lblCurrentPage.Text), grdProducts.PageSize);
if (dtCategory != null && dtCategory.Rows.Count > 0)
{
grdProducts.DataSource = dtCategory;
grdProducts.DataBind();
int RecordCount = CategoryBAL.Get();
lblPageCount.Text = ” Of ” + ((RecordCount / grdProducts.PageSize) + (RecordCount % 2 == 0 ? 1 : 0)).ToString();
lblCurrentPage.Text = (Convert.ToInt32(lblCurrentPage.Text) + 1).ToString();
Session[“dtCategory”] = dtCategory;
}
else
{
lblPageCount.Text = “0”;
lblCurrentPage.Text = “0”;
}
setButtonVisibility();
}

/// <summary>
/// Bind Page Data as per supplied parameters.
/// </summary>
/// <param name=”IsNext”>Pass true if Next page button and false incase of previous page button click.</param>
private void BindGrid(bool IsNext)
{

DataTable dtCategory = CategoryBAL.Get(0, IsNext == true ? Convert.ToInt32(lblCurrentPage.Text) : Convert.ToInt32(lblCurrentPage.Text) – 2, grdProducts.PageSize);
if (dtCategory != null && dtCategory.Rows.Count > 0)
{
grdProducts.DataSource = dtCategory;
//                grdProducts.PageIndex = (IsNext == true ? grdProducts.PageIndex + 1 : grdProducts.PageIndex – 1);
grdProducts.DataBind();
int RecordCount = CategoryBAL.Get();
lblPageCount.Text = ” Of ” + ((RecordCount / grdProducts.PageSize) + (RecordCount % 2 == 0 ? 1 : 0)).ToString();
lblCurrentPage.Text = IsNext == true ? (Convert.ToInt32(lblCurrentPage.Text) + 1).ToString() : (Convert.ToInt32(lblCurrentPage.Text) – 1).ToString();
Session[“dtCategory”] = dtCategory;
}
else
{
lblPageCount.Text = “0”;
lblCurrentPage.Text = “0”;
}
setButtonVisibility();
}

private void BindSubGrid(GridView grdSubCategory, decimal CategoryId, int rownum)
{
DataTable dtSubCategory = ProductBAL.Get(CategoryId);
if (dtSubCategory != null && dtSubCategory.Rows.Count > 0)
{
grdSubCategory.DataSource = dtSubCategory;
grdSubCategory.DataBind();
((Label)grdProducts.Rows[rownum].FindControl(“lblNodataFound”)).Text = string.Empty;
}
else
{
((Label)grdProducts.Rows[rownum].FindControl(“lblNodataFound”)).Text = “No sub category found!”;
}
}

private void setButtonVisibility()
{
if (Convert.ToInt32(lblCurrentPage.Text) == 1)
{
btnPrevious.Enabled = false;
}
else
{
btnPrevious.Enabled = true;
}

int RecordCount = CategoryBAL.Get();
if (Convert.ToInt32(lblCurrentPage.Text) == (RecordCount / grdProducts.PageSize) + (RecordCount % 2 == 0 ? 1 : 0))
{
btnNext.Enabled = false;
}
else
{
btnNext.Enabled = true;
}
}
#endregion
}
}

Reverse String


public static String Reverse(string input)
{
var length = input.Length;
var buffer = new char[length];
for (var i = 0; i < input.Length; i++)
{
buffer[i] = input[(length – i) – 1];
}
return new String(buffer);
}

private string ReverseWordsLogic2(string str)
{
int count = 0;
char[] charArray = str.ToCharArray();
int len = str.Length – 1;
for (int i = 0; i <= len; i++)
{
if (charArray[i].ToString() == ” ” || charArray[i] == ‘ ‘)
count += 2;
}

string[] words = new string[count + 1];
string word = “”;
int icount = 0;
count = 0;
for (int i = 0; i <= len; i++)
{
if (charArray[i] != ‘ ‘)
{
word += charArray[i];
}
else
{
words[icount] = word;
words[icount + 1] = ” “;
icount += 2;
word = “”;
}
}
words[icount] = word;
word = “”;

int wordlen = words.Length – 1;

for (int i = 0; i <= wordlen; i++)
words[i] = words[wordlen – i];

for (int i = 0; i <= wordlen; i++)
word += word[i].ToString();
return word;
}

Good One


Treeview query

SELECT  P1.emp
FROM Personnel AS P1, Personnel AS P2
WHERE P1.lft BETWEEN P2.lft AND P2.rgt
GROUP BY P1.emp

Update multiple

public static int Update(DataTable dtNewCategory, int PageIndex, int PageSize)
{
DataTable dtOldCategory = new DataTable();
int count = 0;

dtOldCategory = CategoryBAL.Get(0, PageIndex, PageSize);
CategoryInformation objCategoryInformation = new CategoryInformation();
for (int i = 0; i < dtOldCategory.Rows.Count; i++)
{
if (dtOldCategory.Rows[i][“CategoryName”].ToString() != dtNewCategory.Rows[i][“CategoryName”].ToString())
{
objCategoryInformation = Connection.objDemoDataContext.CategoryInformations.Single(m => m.CategoryId == Convert.ToDecimal(dtOldCategory.Rows[i][“CategoryId”]));
objCategoryInformation.CategoryName = dtNewCategory.Rows[i][“CategoryName”].ToString();
count += 1;
}
}
Connection.objDemoDataContext.SubmitChanges();
return count;
}
}

convert datatable class


using System;
using System.Data;
using System.Reflection;

namespace DemoBAL
{
public static class ConvertToDataTable
{
#region “Converting ObjectArray to Datatable”

/// <summary>
/// Method to Convert Datatable from object Array.
/// </summary>
/// <param name=”array”></param>
/// <returns></returns>
public static DataTable ConvertToDatatable(Object[] array)
{

PropertyInfo[] properties = array.GetType().GetElementType().GetProperties();
DataTable dt = CreateDataTable(properties);
if (array.Length != 0)
{
foreach (object o in array)
FillData(properties, dt, o);
}
return dt;
}

/// <summary>
/// Method To Create total column of datatable.
/// </summary>
/// <param name=”properties”></param>
/// <returns></returns>
private static DataTable CreateDataTable(PropertyInfo[] properties)
{
DataTable dt = new DataTable();
DataColumn dc = null;
foreach (PropertyInfo pi in properties)
{
dc = new DataColumn();
dc.ColumnName = pi.Name;
//dc.DataType = pi.PropertyType;
dt.Columns.Add(dc);
}
return dt;
}

/// <summary>
/// Method for Fill data in DataTable.
/// </summary>
/// <param name=”properties”></param>
/// <param name=”dt”></param>
private static void FillData(PropertyInfo[] properties, DataTable dt, Object o)
{
DataRow dr = dt.NewRow();
foreach (PropertyInfo pi in properties)
{
dr[pi.Name] = pi.GetValue(o, null);
}
dt.Rows.Add(dr);
}

#endregion

}
}

Hello world!


Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!