Sometimes I need to download the html content from the internet, therefore this method is very useful.
public static void GetWebContent(string url, string saveToPath)
{
if (IsValidUrl(url))
{
HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse httpRes = (HttpWebResponse)httpReq.GetResponse();
Stream fs = httpRes.GetResponseStream();
FileStream fss = new FileStream(saveToPath, FileMode.Create);
const int size = 4096;
byte[] bytes = new byte[4096];
int numBytes;
while ((numBytes = fs.Read(bytes, 0, size)) > 0)
fss.Write(bytes, 0, numBytes);
fss.Close();
}
}
Thursday, April 3, 2008
Find the image height and width using .NET
See as simple as this:
System.Drawing.Imaging.BitmapData bit = new
System.Drawing.Imaging.BitmapData();
int height = bit.Height;
Wednesday, March 26, 2008
ConnectionStrings for SQL Server 2005
ConnectionString for sql server 2005 look like this. Please remember dude...
<*connectionStrings>
<*add name="lala"
connectionString="server=user\sqlexpress;integrated
security=true;database=test"/>
<*/connectionStrings>
How to create xml document from database ado.net
I hate to code with xml because i always forgotten the methods, therefore i blog it here so that i could help someone like me. :)
The 0utput lala.xml
The 0utput lala.xml
<*Library>You need this code:
<*Book id="1">
<*Name>Programming
<*Author>L.L Yee
<*Book id="2">
<*Name>Mathematics
<*Author>Jason Lee
<*/Library>
using System;
using System.Collections.Generic;
using
System.Text;
using System.Data.SqlClient;
using
System.Configuration;
using System.Xml;
namespace TobyXML
{
class
Program
{
static void Main(string[]
args)
{
ReadCustomerFromDB();
}
public static void
ReadCustomerFromDB()
{
string sql = "Select * from library";
string
output = "";
using (SqlConnection myConnection =
new
SqlConnection(ConfigurationManager.ConnectionStrings["lala"].ConnectionString))
{
myConnection.Open();
SqlCommand
mySqlCommand = new SqlCommand(sql, myConnection);
SqlDataReader datareader =
mySqlCommand.ExecuteReader();
XmlDocument xmlDoc = new
XmlDocument();
xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
XmlNode
nodeLibrary =
xmlDoc.CreateElement("Library");
xmlDoc.AppendChild(nodeLibrary);
while(datareader.Read())
{
XmlNode
nodeBook = xmlDoc.CreateElement("Book");
XmlAttribute attId =
xmlDoc.CreateAttribute("id");
attId.Value =
datareader["bookid"].ToString();
nodeBook.Attributes.Append(attId);
XmlNode
nodeName =
xmlDoc.CreateElement("Name");
nodeName.AppendChild(xmlDoc.CreateTextNode(datareader["bookName"].ToString()));
nodeBook.AppendChild(nodeName);
XmlNode
nodeAuthor =
xmlDoc.CreateElement("Author");
nodeBook.AppendChild(nodeAuthor);
nodeAuthor.AppendChild(xmlDoc.CreateTextNode(datareader["author"].ToString()));
nodeLibrary.AppendChild(nodeBook);
}
datareader.Close();
xmlDoc.Save(@"c:\lala.xml");
}
}
}
}
Tuesday, March 18, 2008
How to get HTML tag with regex?
Regex this is the best one!
please ignore the * at the <*
?\W+((\S+\W+(\S*=\S*(?:".*?"'.*?'[^'"><*/?\w+((\s+\w+(\s*=\s*(?:".*?"'.*?'[^'">\s]+))?)+\s*\s*)/?>
please ignore the * at the <*
Get data from the database stright away! .NET
Sometimes you want the most strightforward way to get data from the database without going through the N-tiers stuffs.
This technique have advantages and disadvantages,
Advantage:
The fastest data access beside store procedures.
Simple and strighforward.
Disadvantage:
Not strongly typed,
Possible of memory leak if not code propertly
Things get complicated if involve insert record or large table which have many columns.
This technique have advantages and disadvantages,
Advantage:
The fastest data access beside store procedures.
Simple and strighforward.
Disadvantage:
Not strongly typed,
Possible of memory leak if not code propertly
Things get complicated if involve insert record or large table which have many columns.
public string ReadCustomerFromDB()
{
string sql = "Select
name, phone, age from Customers where customerid =
@customerId";
string output = "";
using (SqlConnection myConnection = new
SqlConnection(ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString))
{
myConnection.Open();
SqlCommand mySqlCommand = new SqlCommand(sql,
myConnection);
mySqlCommand.Parameters.AddWithValue("@customerId", 123456);
SqlDataReader datareader =
mySqlCommand.ExecuteReader();
while
(datareader.Read())
{
Output +=
datareader["name"];
Output +=
datareader["age"];
}
datareader.Close();
}
return output;
}
Format currency using Globalization .NET
There are some elegant way to format the currency such as
String.Format(“{0:c}”,100000”)
The output will be RM100,000.00 if your web.config file set as
<*system.web*>
<*globalization uiCulture="en" culture="en-MY" /*>
However this will not throw you an error if you are using windows vista. such as
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemGlobalizationCultureInfoClassTopic.asp to find out your own culture code.
in this case i should change it to
<*globalization uiCulture="ms" culture="ms-MY" /*>
....
If you set the culture to “en-US” then the output will changed to
$100,000.00
You can check your currency symbol by using this code
System.Globalization.RegionInfo myRI2 = new System.Globalization.RegionInfo(new CultureInfo("en-my", false).LCID);
Console.Write(myRI2.CurrencySymbol);
For more information on setting the culture for specific to your country, please refer the code from this website:
http://msdn2.microsoft.com/en-us/library/system.globalization.regioninfo.aspx
Alternatively, you can specify use string. Format (“{0:$#,##0.##}”,50000), you can replace the ‘$’ whaterver you like. however this is not a good practice.
String.Format(“{0:c}”,100000”)
The output will be RM100,000.00 if your web.config file set as
<*globalization uiCulture="en" culture="en-MY" /*>
However this will not throw you an error if you are using windows vista. such as
Theif there is an error please refer totag contains an invalid value for the 'culture' attribute.
in this case i should change it to
<*globalization uiCulture="ms" culture="ms-MY" /*>
....
If you set the culture to “en-US” then the output will changed to
$100,000.00
You can check your currency symbol by using this code
System.Globalization.RegionInfo myRI2 = new System.Globalization.RegionInfo(new CultureInfo("en-my", false).LCID);
Console.Write(myRI2.CurrencySymbol);
For more information on setting the culture for specific to your country, please refer the code from this website:
http://msdn2.microsoft.com/en-us/library/system.globalization.regioninfo.aspx
Alternatively, you can specify use string. Format (“{0:$#,##0.##}”,50000), you can replace the ‘$’ whaterver you like. however this is not a good practice.
Subscribe to:
Posts (Atom)