Friday, May 29, 2009

C# String vs StringBuilder

When we have to loop through a huge number of recordset to concatenate the results, we should use a stringbuilder as opposed to a string. The reason being, in C#, the String object is being truncated and recreated elsewhere in memory as opposed to the StringBuilder which stays in memory and does not gets recreated.

If the recordset is huge enough (say maybe more than 10 records), then the usage of a StringBuilder rather than plain old concatenation will result in a faster response time.

I had a text file that I was reading the results from (the text file had 7000 lines of records) and I suspected the textfile was the bottleneck to the application as it was taking over 60 seconds to execute. Apparently after some usage of:


DateTime currentSystemTime = DateTime.Now;
Debug.WriteLine("DB START: " + currentSystemTime);

I realised that the program was really slow when it was trying to concatenate the files to form an sql query string. So rather than doing this:

String insertionQuery = "";
insertionQuery = insertionQuery + String.Format("INSERT INTO [log] (mac, type, yearmth, dte, tme) VALUES ('{0}','{1}','{2}', '{3}', '{4}');",
logMac, logType, logIndex, logDate, logTime);

I did this:

StringBuilder insertionQuery = new StringBuilder();
insertionQuery = insertionQuery.Append(String.Format("INSERT INTO [log] (mac, type, yearmth, dte, tme) VALUES ('{0}','{1}','{2}', '{3}', '{4}');",
logMac, logType, logIndex, logDate, logTime));

Solved

Tuesday, May 26, 2009

ASP.NET Date Programming

//String to DateTime
String MyString;
MyString = "1999-09-01 21:34 PM";
DateTime MyDateTime;
MyDateTime = new DateTime();
MyDateTime = DateTime.ParseExact(MyString, "yyyy-MM-dd HH:mm tt", null);

//DateTime to String
MyDateTime = new DateTime(1999, 09, 01, 21, 34, 00);
String MyString; MyString = MyDateTime.ToString("yyyy-MM-dd HH:mm tt");

// FORMATS
d - Numeric day of the month without a leading zero.dd - Numeric day of the month with a leading zero.ddd - Abbreviated name of the day of the week.dddd -
f,ff,fff,ffff,fffff,ffffff,fffffff - Fraction of a second. The more Fs the higher the precision.
h - 12 Hour clock, no leading zero.hh - 12 Hour clock with leading zero.H - 24 Hour clock, no leading zero.HH - 24 Hour clock with leading zero.
m - Minutes with no leading zero.mm - Minutes with leading zero.
M - Numeric month with no leading zero.MM - Numeric month with a leading zero.MMM - Abbreviated name of month.MMMM - Full month name.
s - Seconds with no leading zero.ss - Seconds with leading zero.
t - AM/PM but only the first letter. tt - AM/PM ( a.m. / p.m.)
y - Year with out century and leading zero.yy - Year with out century, with leading zero.yyyy - Year with century.
zz - Time zone off set with +/-.


http://www.codeproject.com/KB/cs/String2DateTime.aspx

ASP NET DEBUGGING

For ASP.NET, Console.WriteLine does not work, so we have to use System.Diagnostics.Debug.WriteLine("output"); instead. We can view the output log by viewing the program in debug mode (pressing F5 instead of CTRL+F5).

Monday, May 25, 2009

WF GF

then this guy bring his gf
wah the GF SUPER IRRITATING think she damn smart
then keep saying other ppl's english bad
the waiter came and take orders
she tried to act 'sophisticated'
act like she don't understand the waiter's broken singlish
then she said: "can you slower down? AR"
hahaha.. i almost laugh till i peng

Monday, May 11, 2009

Could not find stored procedure

When you cannot find the stored procedure and you are sure that the connectionString works and the stored procedure is inside the programmability folder. Example connection String is:
csb.ConnectionString = "Server=SERVER\WHATEVER;Initial Catalog=mw;Database=mw;User ID=aaron;Password=aaron";

Restore failed for Server

When you restore a database from another device and run into an error such as: System.Data.SqlClient.SqlError: File 'c:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\salon.mdf' is claimed by 'salon_1'(1) and 'salon_Data'(2). The WITH MOVE clause can be used to relocate one or more files. (Microsoft.SqlServer.Smo)

You need to look under options and make sure that the SQL server is not trying to output to the same file name. The problem was due to the rows under the column "Restore As" (under options tab) having the same file name. So in other words, simply change the value of the column name for the "Restore As" to unique MDF values, and the restore should be successful.

Sunday, May 10, 2009

Import Data to SQL Server

1. On the existing server
Go to Microsoft SQL Server Studio Express
Task > Backup >
* take note to create the backup to only a single media file, unless you have to split it up.

2. On the new server
Go to Microsoft SQL Server Studio Express
Task > Restore
* there is no need to create a new database with the same name if this is a completely new restore. once we set the database file device, the new database name will appear to be restored.

Wednesday, May 6, 2009

Macintosh and FileReference Type in Flex

In Mac, the Type attribute of the filereference object returns null by default and this is a huge problem, considering that the type attribute works by default for the PC.


private function getMacExtension(myExtension:String,myName:String):String {
if (myExtension == null) {
if (myName.lastIndexOf(".") > 0) {
myExtension = myName.substr(myName.lastIndexOf("."), myName.length);
myExtension = myExtension.toLowerCase();
}
}
return myExtension;
}

Saturday, May 2, 2009

Use Zune as Hard Disk

Make sure your Zune is not plugged in and your Zune software isn't running
open up regedit by going to the start menu and selecting "run". Type regedt32 and hit "OK"
Browse to HKEY_LOCAL_MACHINE\System\ControlSet001\Enum\USB\
Search for "PortableDeviceNameSpace". This should be contained in the Vid_####&Pid_####\########_-_########_-_########_-_########\Device Parameters within the above ...\USB\ The ##'s listed here will be numbers and letters specific to your Zune
Change the following values:
EnableLegacySupport to 1
PortableDeviceNameSpaceExcludeFromShell to 0
ShowInShell to 1
Plug in your Zune, and make sure the Zune Software starts up.
Hopefully at this point you can open up "My Computer" and browse your device, though it does NOT show up as a drive letter.