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

<*Library>
<*Book id="1">
<*Name>Programming
<*Author>L.L Yee
<*Book id="2">
<*Name>Mathematics
<*Author>Jason Lee
<*/Library>
You need this code:
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!

<*/?\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.

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
The tag contains an invalid value for the 'culture' attribute.
if there is an error please refer to 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.

Saturday, March 15, 2008

How to customize community server 2007?

Ever think of customize community server 2007? Yes, you can. However to teach you the whole thing will be long story, therefore keep thing simple. Just follow this guideline and try it yourself. Here the steps:

1. Download the SDK cs2007.1 from communityserver.org

2. Refer to short and tested step from doc.communityserver.org/wiki/page.aspx/399/how-to-create-new-themes/ (please follow the step from 1,2 and 3 only)

3. Install your cs2007.1 database

4. Compile and Run the SDK source code from VS 2005, note you have to do some setting in the property page such as running it from dynamic port rather than localhost ISS

5 .Copy the created theme from step 2 and paste the dir into the cs theme directory.

6. refresh and include the dir. Edit the pasted dir only.

7. try to edit the master.master such as remove the logo and run it and feel the different.

Good luck, i have tested it and it works!

Sunday, March 2, 2008

good design determine programmer productivity that create reliable system

An enterprise system is complex and it has to be able to handle day to day business processes and activities. The reliability of the system is so much dependent on how the system was designed and developed by the programmer. Programmer need to code well and able to create a system with the most reliable code. However, in practical to be able to achieve a highly reliable system is very costly to the organization. The reason is because a lot of testing need to be done to ensure that every source code is validated. Microsoft particuallary has been trying to improve the reliability of Windows by patches and patches after the final release. Why need to do in that way? Why not to produce a system which is 100% free of bugs? The answer of this question is too simple to say that it is very time consuming and costly to examine every possiblility of the failure in the system. We have no choice but keep complaining about the reliability of the system and luckly there are software developers who are keeps fixing it. This is how the software industry works. If some one were to change it, it is the last breakthrough of information technology.

To start to design a stable and reliable system, 3 things need to keep in mind at first. 3 rules: guidelines need to be followed, always keep things simple and last but not lease always keeps design a good database structure by having 3rd NF.

Guidelines are the findings and experinces of the system designer in which makes the best to design the system. It also covers the rules that draw the requirements. Guidelines are important because the things to be followed and the things to be avoided during the system development cycle, guideline are very depending on each organization, therefore you set your own rules.

KISS, keep it simple, the secret that always work in every situation in our life. Keep it simple does not means to ignore requirements but to avoid doing things in a hard way. We need to be smart in finding alternative which is elegant and simple enough to our self to accomplish thing. For example, i can save time by having alble to find 5Min's solution using Google. Smart isn't it?

Database design is very very important. We hardly see an enterprise system without using database. A large system particularly are very used to having a database server to serve everyday data intensive client. Moreover, an efficient and high performance database system is critical to the success of the system as a whole. Therefore database design is extremely critical to determine the reliability of the system and the productivity of the programmers. Why? The reasons are, programmer need to code less and perhaps it is easier to code if the database design is in properly designed. I once experience to do develop a system in which the database design is not in the proper way. In this case i had to write additional code which is not necessary to make it work and i had to spend much my time to check the data to ensure it was consistent across the system. Why we need to check by us since a relational database system is so good in enforcing constrain in every database manipulation that keep data integrity. Therefore a good database design must have this 3 things, 3rd NF, one primary key for each table and have relational constrain.

A lot of problem will happen during the system development if the initial system design is not good enough. The worst thing is that the database design is in bad shape, i wonder how many debugging time has been wasted during the system development.