Posted by Ajay Matharu in .Net, ASP.Net, Development, Visual StudioOct 7th, 2009 | 1 Comment
Following code helps you to import grid values to excel,
//Import System.IO in your application for StreamWriter Object
//note: excel_file represents the complete physical address of excel file eg C:/myexcel.xls
public void export_datagridview_to_excel(DataGridView dgv, string excel_file)
{
int cols;
//open file
StreamWriter wr = new StreamWriter(excel_file);
//determine the number of columns and write columns to file
cols = dgv.Columns.Count;
for (int i = 0; i < cols; i++)
{
wr.Write(dgv.Columns[i].Name.ToString().ToUpper() + “\t”);
}
wr.WriteLine();
//write rows to excel file
for...
Posted by Ajay Matharu in .Net, Visual StudioJul 25th, 2009 | No Comments
Standard numeric format strings are used to return string equivalents of numeric types in predefined patterns. When used with ToString, a standard numeric format string parameter takes the form.
object.ToString ( "Fn" );
where F is a single alphabetic character called the format specifier, and n can be one or more digits called the precision specifier.
The following table describes the standard numeric format strings. Note that the patterns produced by these format specifiers are influenced by the settings in the Regional Options control panel. Computers using different cultures or different currency...
Posted by Ajay Matharu in .Net, ASP.Net, Visual StudioJul 22nd, 2009 | No Comments
Another one of those problems that can stump you for a while. I often make backups of files as I progress and sometimes leave them in the website folder, this was fine with classic ASP but as I have found with ASP.Net can cause all sorts of problems.One issue I was having was trying to reference a component in my aspx page from the codebehind page and seeing the following error when trying to reference a label control:
“Label2 does not exist in the current context”
So I found that the problem was even though I renamed one of my old files it still contained the class that my codebehind was...
Posted by Ajay Matharu in .Net, PHP, Visual StudioJun 12th, 2009 | No Comments
To get MD5 hash in C# use this method. It returns same value AS MD5() function in PHP
public string GetMD5Hash(string input)
{
System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);
bs = x.ComputeHash(bs);
System.Text.StringBuilder s = new System.Text.StringBuilder();
foreach (byte b in bs)
{
s.Append(b.ToString("x2").ToLower());
}
string password =...
Posted by Ajay Matharu in .Net, ASP.Net, SharepointJun 10th, 2009 | No Comments
Here is the little code that you can use to impersonate any user in sharepoint. This will code will pretend that you are logged in as the user who is impersonated and will be allowed access to all the features and inherits all the rights for that person. This is mainly used to do something for which the impersonating person has rights and you other user do not have rights.
SPWeb web = SPContext.Current.Web.Site.OpenWeb();
SPUser user = web.AllUsers[username];
SPUserToken userToken = user.UserToken;
SPSite site = new SPSite(SPContext.Current.Web.Site.Url.ToString(), userToken);
web = site.OpenWeb();
Here...