Search This Blog

Friday, October 31, 2008

Put your DataTable value to ViewState such that you can rearrange the ViewState

ViewState["userDataSource"] = dtTemp;


public SortDirection GridViewSortDirection
{
get
{
if (ViewState["sortDirection"] == null)
ViewState["sortDirection"] = SortDirection.Ascending;
return (SortDirection)ViewState["sortDirection"];
}
set
{
ViewState["sortDirection"] = value;
}
}


private void SortGridView(string sortExpression, string direction)
{
DataTable dt = ((DataTable)ViewState["userDataSource"]);
DataView dv = new DataView(dt);
dv.Sort = sortExpression + " " + direction;

this.gvUser.DataSource = dv;
gvUser.DataBind();
}

protected void gvUser_Sorting(object sender, GridViewSortEventArgs e)
{

string sortExpression = e.SortExpression;
GridView dd = ((GridView)(object)sender);

if (GridViewSortDirection == SortDirection.Ascending)
{
GridViewSortDirection = SortDirection.Descending;
SortGridView(sortExpression, "DESC");
}
else
{
GridViewSortDirection = SortDirection.Ascending;
SortGridView(sortExpression, "ASC");
}

}

Pagination in DataList

Generally DataList Control does not support pagination by its default properties..But if you need it you have to do it by mannual coding.

Add
PagedDataSource pds = new PagedDataSource();
at the top of your Page_Load(object sender, EventArgs e) event

Now Add these methods in your code behind page


public void BindDatalist() //Get Data and Set Page Size ,Page Index
{

//dlstFeatureAlbum.DataSource = FeatureAlbum.GetAlbums();
// dlstFeatureAlbum.DataBind();

DataTable dt = new DataTable();

dt = FeatureAlbum.GetAlbums();
pds.DataSource = dt.DefaultView;
pds.AllowPaging = true;
pds.PageSize = 24;//Convert.ToInt16(ddlPageSize.SelectedValue);
if (dt.Rows.Count <= 24)
{
divPageIndex.Visible = false;
}
if (dt.Rows.Count == 0)
{
lblEmptyList.Visible = true;
}
pds.CurrentPageIndex = CurrentPage;
lnkbtnNext.Enabled = !pds.IsLastPage;
lnkbtnPrevious.Enabled = !pds.IsFirstPage;

dlstFeatureAlbum.DataSource = pds;
dlstFeatureAlbum.DataBind();

doPaging();
}


//Code for Pagination

public int CurrentPage //this method is for storing the current page index
//to view state
{

get
{
if (this.ViewState["CurrentPage"] == null)
return 0;
else
return Convert.ToInt16(this.ViewState["CurrentPage"].ToString());
}

set
{
this.ViewState["CurrentPage"] = value;
}

}

private void doPaging() //Calculating total results and divide them into pages
{
DataTable dt = new DataTable();
dt.Columns.Add("PageIndex");
dt.Columns.Add("PageText");
for (int i = 0; i < pds.PageCount; i++)
{
DataRow dr = dt.NewRow();
dr[0] = i;
dr[1] = i + 1;
dt.Rows.Add(dr);
}

dlPaging.DataSource = dt;
dlPaging.DataBind();
}

protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName.Equals("lnkbtnPaging"))
{
CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
BindDatalist();
}
}

protected void lnkbtnPrevious_Click(object sender, EventArgs e) //navigation
{
CurrentPage -= 1;
BindDatalist();
}

protected void lnkbtnNext_Click(object sender, EventArgs e) //navigation
{
CurrentPage += 1;
BindDatalist();
}

protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
{
LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
{
lnkbtnPage.Enabled = false;
lnkbtnPage.Font.Bold = true;
}
}

protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
{
CurrentPage = 0;
BindDatalist();
}

//End

Write XML from code behind

public void CreateXMLFile(string FileName)
{
DataTable dt = new DataTable();
dt = FeatureAlbum.GetAlbums();
string path;
path = System.AppDomain.CurrentDomain.BaseDirectory.ToString() + FileName + ".xml";
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
System.IO.FileInfo fileName = new System.IO.FileInfo(path);
try
{
if (!fileName.Exists)
{
StreamWriter sw = new StreamWriter(path, true);
sw.WriteLine("");
sw.WriteLine("
");
sw.Flush();
sw.Close();
}
doc.Load(path);
XmlElement newElem;
XmlElement newChElem;
XmlElement newSuperChElem;
XmlNode XNode;

for (int i = 0; i < dt.Rows.Count; i++)
{
newElem = doc.CreateElement("image");
doc.DocumentElement.AppendChild(newElem);
// newElem.SetAttribute("","");

newChElem = doc.CreateElement("filename");
newChElem.InnerText = dt.Rows[i]["Name"].ToString();
newElem.AppendChild(newChElem);

newChElem = doc.CreateElement("caption");
newChElem.InnerText = dt.Rows[i]["Description"].ToString();
newElem.AppendChild(newChElem);
}
//newChElem = doc.CreateElement("Time");
//newChElem.InnerText = "Time";
//newElem.AppendChild(newChElem);

XmlTextWriter writer = new XmlTextWriter(path, System.Text.Encoding.ASCII);
writer.Formatting = Formatting.Indented;
doc.Save(writer);
writer.Close();
}
catch (Exception ex)
{
//Your Catch block here...
}
}

Wednesday, August 6, 2008

Friend Function

A friend function is not a class member and yet it can have access to private and protected members of a class. Friends are not called by using member selection operators such as “ . or ->” unless they are members of another class. Friend functions can be defined inside a class declaration. Friend functions declared within a class are inline just like other inline member functions. These functions act as though they were defined immediately after all class members have been seen but before the end of the class declaration. Even though friend functions are declared inside class declarations, they are not considered in the scope of the enclosing class. They are normally considered in the file scope. An entire class can be declared as a friend of another class.

To declare a function as a friend of a class, put the word “friend” before the function prototype in the class definition. For example, you want to show that ClassTwo is a friend of ClassOne, simply put the word “friend” in front of ClassTwo like:

friend class ClassTwo;

This line should be inside the definition of class ClassOne and declared explicitly. The friendship between ClassOne and ClassTwo is neither symmetric nor transitive. This means that ClassTwo is a friend of ClassOne but ClassOne cannot be assumed to be a friend of ClassTwo. Same concept applies when you have multiple classes connected by multiple friendships. Friendship can only go one way.

A partial example of friends accessing private members of a class goes something like this:

class ClassOne{
// friend declaration should appear before the declaration of public and private member function
friend void ClassTwo ( ClassOne &, int); // friend declaration
public:
Count( ) { x = 0; } // constructor
Void print( ) const { cout << x << endl;} // output
Private:
Int x; // data member
};

//ClassTwo is declared as a friend function of ClassOne
void ClassTwo( ClassOne &c, int val)
{
c.x = val;
}
int main( )
{
ClassOne counter;
counter.print ( );
ClassTwo( counter,10); // set x with a friend
Counter.print( );
Return 0;
}

The purpose of using friend function is to improve performance. Sometimes when a member function cannot be used in a certain operation, using friends would be able to solve that problem.

Tuesday, August 5, 2008

Devide Tree Menu in Two Diff Column

public void GetCategoryTree()
{

BpBL.ProjectCategory objCat = new BpBL.ProjectCategory();

// PopulateTreeViewControl(categoryList);
DataTable dtTemp= new DataTable();
DataTable dtSubCat = new DataTable();
DataTable dtDemoTable1;
DataTable dtDemoTable2;

dtTemp=objCat.SelectMainCategory();
dtDemoTable1 = dtTemp.Clone();
dtDemoTable2 = dtTemp.Clone();
int count = Convert.ToInt32(dtTemp.Rows.Count);
int i=0;

foreach(DataRow drMain in dtTemp.Rows)
{

if (i++ % 2 == 0)
{
dtDemoTable1.LoadDataRow(drMain.ItemArray, true);
}
else
{
dtDemoTable2.LoadDataRow(drMain.ItemArray, true);
}
}
dtTemp.Clear();
TreeNode parentNode = null;
//Binding First Tree View
foreach (DataRow dr in dtDemoTable1.Rows)
{
parentNode = new TreeNode(dr["CategoryName"].ToString(), dr["Id"].ToString());
dtSubCat = objCat.SelectSubCategory(dr["Id"].ToString());
foreach (DataRow drSubcat in dtSubCat.Rows)
{
// TreeNode childNode = new TreeNode(product.ProductName, product.ProductID.ToString());
TreeNode childNode = new TreeNode(drSubcat["CategoryName"].ToString(), drSubcat["Id"].ToString());

parentNode.ChildNodes.Add(childNode);
}

parentNode.Collapse();

treeViewProjCat.ShowCheckBoxes = TreeNodeTypes.All;
treeViewProjCat.Nodes.Add(parentNode);

}

//Binding 2nd Tree View

foreach (DataRow dr in dtDemoTable2.Rows)
{
parentNode = new TreeNode(dr["CategoryName"].ToString(), dr["Id"].ToString());
dtSubCat = objCat.SelectSubCategory(dr["Id"].ToString());
foreach (DataRow drSubcat in dtSubCat.Rows)
{
// TreeNode childNode = new TreeNode(product.ProductName, product.ProductID.ToString());
TreeNode childNode = new TreeNode(drSubcat["CategoryName"].ToString(), drSubcat["Id"].ToString());

parentNode.ChildNodes.Add(childNode);
}

parentNode.Collapse();

treeViewProjCat2.ShowCheckBoxes = TreeNodeTypes.All;
treeViewProjCat2.Nodes.Add(parentNode);

}
}

Wednesday, July 23, 2008

execute parameterized stored procedure from code behind

Add This to your Web.config file








SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConStr"]);
SqlCommand cmd = new SqlCommand("spCheckProjectStatus", conn);
cmd.CommandType = CommandType.StoredProcedure;

SqlParameter sqlParam = new SqlParameter();
sqlParam = cmd.Parameters.Add("@ProjectId",SqlDbType.VarChar);
sqlParam.Direction = ParameterDirection.Input;
sqlParam.Value = dtTemp.Rows[i]["Pid"].ToString();

SqlParameter sqlParam2 = new SqlParameter();
sqlParam2 = cmd.Parameters.Add("@BiderId",SqlDbType.VarChar);
sqlParam2.Direction = ParameterDirection.Input;
sqlParam2.Value = dtTemp.Rows[i]["Bid"].ToString();

conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
conn.Close();

hope this will help you....

Tuesday, July 22, 2008

Generate Random Key with integer and character

use
System.Security;
System.Security.Cryptography;

in the include section

private string GetUniqueKey()
{

int maxSize = 8;
int minSize = 5;
char[] chars = new char[62];
string a;
a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
chars = a.ToCharArray();
int size = maxSize;
byte[] data = new byte[1];
RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
crypto.GetNonZeroBytes(data);
size = maxSize;
data = new byte[size];
crypto.GetNonZeroBytes(data);
StringBuilder result = new StringBuilder(size);
foreach (byte b in data)
{
result.Append(chars[b % (chars.Length - 1)]);
}
return result.ToString();
}

hope it will help you