this.Page.ClientScript.RegisterStartupScript(this.GetType(), "ScriptName", "<script language=JavaScript>window.print();</script>");
Friday, May 4, 2012
Run Process in background / hidden
// Run Process in background / hidden
//txtRebuildFolderBrowser.Text stands for a specific folder ex: C:\thedirectory
//rebuildFileName stands for a specific file eX: the.exe or the.bat
System.Diagnostics.ProcessStartInfo i = new
System.Diagnostics.ProcessStartInfo(txtRebuildFolderBrowser.Text + "\\" + rebuildFileName);
i.CreateNoWindow = true;
i.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
System.Diagnostics.Process p = System.Diagnostics.Process.Start(i); // inicia o processo do rebuild
p.WaitForExit(); //aguarda o processo terminar
MessageBox.Show("Process Complete","Process Info:");
Converting from string to type
//Converting from string to type
//Generic code for converting a string to a specific type
public static T FromString<T>(string text)
{
try
{
return (T)Convert.ChangeType(text, typeof(T), CultureInfo.InvariantCulture);
}
catch
{
return default(T);
}
}
public static T FromXmlAttribute<T>(XmlNode node, string attributeName)
{
if(node == null)
throw new ArgumentNullException("node");
if(String.IsNullOrEmpty(attributeName))
throw new ArgumentException("Cannot be null or empty", "attributeName");
XmlAttribute attribute = node.Attributes[attributeName];
if(attribute == null)
return default(T);
return FromString<T>(attribute.Value);
}
public static T FromXAttribute<T>(XElement element, string attributeName)
{
if (element == null)
throw new ArgumentNullException("element");
if (String.IsNullOrEmpty(attributeName))
throw new ArgumentException("Cannot be null or empty", "attributeName");
XAttribute attribute = element.Attribute(attributeName);
if (attribute == null)
return default(T);
return FromString<T>(attribute.Value);
}
Call a command line function from C#
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "cmd";
p.StartInfo.Arguments = "/C ipconfig /all";
p.Start();
StreamReader sr = p.StandardOutput;
while ((sr.ReadLine()) != null)
{
Console.WriteLine(sr.ReadLine());
}
Monday, April 2, 2012
Convert a generic list to DataTable in C#
The below extension method will help us to convert a generic list to DataTable
public static class DataTableExtension
{
public static DataTable ToDataTable(this List list)
{
Type type = typeof(T);
DataTable dt = new DataTable(type.Name);
var propertyInfos = type.GetProperties().ToList();
//For each property of generic List (T), add a column to table
propertyInfos.ForEach(propertyInfo =>
{
Type columnType = Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType;
dt.Columns.Add(propertyInfo.Name, columnType);
});
//Visit every property of generic List (T) and add each value to the data table
list.ForEach(item =>
{
DataRow row = dt.NewRow();
propertyInfos.ForEach(
propertyInfo =>
row[propertyInfo.Name] = propertyInfo.GetValue(item, null) ?? DBNull.Value
);
dt.Rows.Add(row);
});
//Return the datatable
return dt;
}
}
How to use this?
class Program
{
static void Main(string[] args)
{
List lstString = new List();
Enumerable.Range(1, 10).ToList().ForEach(i => lstString.Add(
new Person { PersonId = i
, PersonName = string.Concat("Name", i)
, Sex = i%2 ==0 ? "M":"F"
, Salary = 100 + i
}));
var res = lstString.ToDataTable();
}
}
public class Person
{
public int PersonId { get; set; }
public string PersonName { get; set; }
public string Sex { get; set; }
public decimal Salary { get; set; }
}
public static class DataTableExtension
{
public static DataTable ToDataTable
{
Type type = typeof(T);
DataTable dt = new DataTable(type.Name);
var propertyInfos = type.GetProperties().ToList();
//For each property of generic List (T), add a column to table
propertyInfos.ForEach(propertyInfo =>
{
Type columnType = Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType;
dt.Columns.Add(propertyInfo.Name, columnType);
});
//Visit every property of generic List (T) and add each value to the data table
list.ForEach(item =>
{
DataRow row = dt.NewRow();
propertyInfos.ForEach(
propertyInfo =>
row[propertyInfo.Name] = propertyInfo.GetValue(item, null) ?? DBNull.Value
);
dt.Rows.Add(row);
});
//Return the datatable
return dt;
}
}
How to use this?
class Program
{
static void Main(string[] args)
{
List
Enumerable.Range(1, 10).ToList().ForEach(i => lstString.Add(
new Person { PersonId = i
, PersonName = string.Concat("Name", i)
, Sex = i%2 ==0 ? "M":"F"
, Salary = 100 + i
}));
var res = lstString.ToDataTable();
}
}
public class Person
{
public int PersonId { get; set; }
public string PersonName { get; set; }
public string Sex { get; set; }
public decimal Salary { get; set; }
}
Wednesday, March 28, 2012
SMTP E-Mail Sender in C#
Either a call to Dispose on your SmtpClient after you're done using it:
smtp.Dispose();
or use a using:
using (SmtpClient smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
Credentials = new NetworkCredential("user@gmail.com", "password"),
EnableSsl = true
})
{
smtp.Send(mail);
}
The using will take care of calling Dispose for you.
Also, don't forget that there's a convenience method on the SmtpClient, if you don't need anything fancy on the message like HTML formatting or attachments.
smtp.Send(from, to, subject, body);
smtp.Dispose();
or use a using:
using (SmtpClient smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
Credentials = new NetworkCredential("user@gmail.com", "password"),
EnableSsl = true
})
{
smtp.Send(mail);
}
The using will take care of calling Dispose for you.
Also, don't forget that there's a convenience method on the SmtpClient, if you don't need anything fancy on the message like HTML formatting or attachments.
smtp.Send(from, to, subject, body);
Tuesday, February 28, 2012
Cookie: How to read and write a cookie in Asp.net
Cookies are very popular in Web development, and used to store visitors data. They Provide another way to store information of web page for later use. Mainly it is used to remember users between visits. You can retain the values of web page during the postbacks and can use the same value in any page of your web application. It is a very small text file and stored in the client machine, but is easily traceable. It has so many limitations #1. User can not store data more than 4096 KB #2. User can easily stops/disable cookies by browser We can use cookies in asp.net by using collections.
Sample Code
//How to Add
void Cookies_Add()
{
HttpCookie LanguageCookies = new HttpCookie("LanguageCookies");
LanguageCookies.Value = "English";
LanguageCookies.Expires = DateTime.Now.AddMinutes(20);
Response.Cookies.Add(LanguageCookies);
}
//How to Read
void Cookies_Read()
{
string str = Request.Cookies["LanguageCookies"].Value;
}
//How to delete
void Cookies_Delete()
{
if(Request.Cookies["LanguageCookies"]!=null)
{
// cookies will be expiry immediatly
Response.Cookies["LanguageCookies"].Expires =
DateTime.Now.AddMinutes(-1);
}
}
Sample Code
//How to Add
void Cookies_Add()
{
HttpCookie LanguageCookies = new HttpCookie("LanguageCookies");
LanguageCookies.Value = "English";
LanguageCookies.Expires = DateTime.Now.AddMinutes(20);
Response.Cookies.Add(LanguageCookies);
}
//How to Read
void Cookies_Read()
{
string str = Request.Cookies["LanguageCookies"].Value;
}
//How to delete
void Cookies_Delete()
{
if(Request.Cookies["LanguageCookies"]!=null)
{
// cookies will be expiry immediatly
Response.Cookies["LanguageCookies"].Expires =
DateTime.Now.AddMinutes(-1);
}
}
Subscribe to:
Posts (Atom)