10.24.05
Posted in Programming at by chenty
I created this blog to talk about technology, business and life. It would be shameful to write about business since I don’t have any success yet, but why don’t I write about my life?? Well, it’s just too simple to write.
void my_life_for_a_day()
{
wakeup();
isLate = true;
if (!isLate)
{
breakfast(); //dead code, never happen
}
if (isCarBroken)
{
panic("omg");
call_boss();
//well still have to do something
play_final_fantasy_11();
}
else
{
drive_to_work();
work();
//lunch now
lunch();
work();
drive_to_home();
buy_food();
dinner();
//play some game
play_final_fantasy_11();
}
sleep();
if (isFeelLonely)
{
dream_a_beautiful_wife();
}
}
-
void my_life_for_a_day()
-
{
-
wakeup();
-
isLate = true;
-
-
if (!isLate)
-
{
-
breakfast(); //dead code, never happen
-
}
-
-
if (isCarBroken)
-
{
-
panic("omg");
-
call_boss();
-
-
//well still have to do something
-
play_final_fantasy_11();
-
}
-
else
-
{
-
drive_to_work();
-
-
work();
-
-
//lunch now
-
lunch();
-
-
work();
-
-
drive_to_home();
-
-
buy_food();
-
dinner();
-
-
//play some game
-
play_final_fantasy_11();
-
}
-
-
sleep();
-
-
if (isFeelLonely)
-
{
-
dream_a_beautiful_wife();
-
}
-
}
This is my simple but busy life. =)
Permalink
Posted in Programming at by chenty
There are 3 very useful parameters I use all the time while wring stored procedure:
@@ERROR
@@ROWCOUNT
@@IDENTITY
There are many other @@ you can use, but I don’t use the other very often, but these 3 I listed above are extremely useful:
1. @@ERROR
@@ERROR returns the error code of the last statement. You can use If statement to check error code of previous statement. The most common case I use this is after every insert or update statement:
If @@ERROR <> 0
Return (1)
-
IF @@ERROR <> 0
-
RETURN (1)
When I use this in a program, whenever I received return value 1, the program will know that there is an error occured.
2. @@ROWCOUNT
@@ROWCOUNT returns how many rows have been affected in the previous statement. I used it a lot after Update or Select.
For example, in a stored procedure, you want to find if password and user name match:
SELECT * FROM Accounts WHERE userid = @userid AND password = @password
If @@ROWCOUNT > 0
RETURN (1)
Else
RETURN (0)
-
SELECT * FROM Accounts WHERE userid = @userid AND password = @password
-
IF @@ROWCOUNT > 0
-
RETURN (1)
-
Else
-
RETURN (0)
If the program get return value 1, then grant access; otherwise, display “user id and password do not match”.
3. @@IDENTITY
@@IDENTITY returns what’s the last INSERT statement’s autonumber identity. I use this a lot in a senario when I need to create a new record, but do a series of initialization on other tables (When use multiple tables to store same item).
e.g.
INSERT employees (name, address) VALUES (@name, @address) /* this will generate a new employeeid for the record inserted)
INSERT salaries (eid, salary) VALUES (@@INDENTITY, 0) /* initialize salary for the employee salary record */
-
INSERT employees (name, address) VALUES (@name, @address) /* this will generate a new employeeid for the record inserted)
-
INSERT salaries (eid, salary) VALUES (@@INDENTITY, 0) /* initialize salary for the employee salary record */
The above code will make sure every newly created employee will have a matching salary record, even though the amount has not yet been decided.
Permalink
10.17.05
Posted in Miscellaneous at by chenty
Since I created my blog, I have not modified the original Wordpress program. The original wordpress has big problem with posting source code, and the screen is too narrow which makes the code not easy to read. Therefore, I decided to hack the Wordpress to fix these problem. The good thing about Wordpress is that there are many plug-ins I can use. I found a good syntax highlight plugin that uses of Geshi. You can find this plug-in at
http://codex.wordpress.org/Plugins/Syntax_Highlighting
It works very well. I formatted my last post, and the code looks much more readable then before.
The second task is to widen the page, which cannot be done by plug-ins. Wordpress provides “themes” for me to do it. I want to totally change the looking, but I figured I would not have time to do it now. Thus, I just modified “default” theme. Just play around with images, css, and actual header, footer and index files etc. It doesn’t take long, but it might require some trial and error. In all, now I believe the programming blogs are much easier to read.
There are many more plug-ins to explore, and I am really interested in creating my own theme someday. Moreover, it would be nice to do some Search Engine Optimization as well. =)
Permalink
Comments off
Posted in Programming at by chenty
I need to implement an image validation code to verify the users on my website are real people instead of some bots. Therefore, I need to implement an image validation system similar to the one you will see when you register a new user account for Yahoo or Hotmail. There are many articles and source code for it. I refer to 2 websites (Chinese):
http://dev.csdn.net/article/73/73816.shtm
http://www.cnblogs.com/index/archive/2004/10/20/54692.aspx
The harder party is to generate the validation image. Here is my code for validation-image.aspx:
private void Page_Load(object sender, System.EventArgs e)
{
string checkCode = CreateRandomCode(4);
Session["ValidationCode"] = checkCode; //checkCode;
CreateImage(checkCode);
}
private string CreateRandomCode(int codeCount)
{
string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H"
+",I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z";
string[] allCharArray = allChar.Split(',');
string randomCode = "";
int temp = -1;
Random rand = new Random();
for(int i = 0; i < codeCount; i++)
{
if(temp != -1)
{
rand = new Random(i*temp*((int)DateTime.Now.Ticks));
}
int t = rand.Next(35);
if(temp == t)
{
return CreateRandomCode(codeCount);
}
temp = t;
randomCode += allCharArray[t];
}
return randomCode;
}
private void CreateImage(string checkCode)
{
if(checkCode == null || checkCode.Trim() == String.Empty)
return;
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 14)), 22);
Graphics g = Graphics.FromImage(image);
try
{
Random random = new Random();
g.Clear(Color.White);
for(int i=0; i<25; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(checkCode, font, brush, 2, 2);
for(int i=0; i<100; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
-
private void Page_Load(object sender, System.EventArgs e)
-
{
-
string checkCode = CreateRandomCode(4);
-
Session["ValidationCode"] = checkCode; //checkCode;
-
CreateImage(checkCode);
-
}
-
-
private string CreateRandomCode(int codeCount)
-
{
-
string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H"
-
+",I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z";
-
string[] allCharArray = allChar.Split(‘,’);
-
string randomCode = "";
-
int temp = -1;
-
-
Random rand =
new Random
();
-
for(int i = 0; i < codeCount; i++)
-
{
-
if(temp != -1)
-
{
-
rand =
new Random
(i*temp*
((int)DateTime.
Now.
Ticks));
-
}
-
int t = rand.Next(35);
-
if(temp == t)
-
{
-
return CreateRandomCode(codeCount);
-
}
-
temp = t;
-
randomCode += allCharArray[t];
-
}
-
return randomCode;
-
}
-
-
private void CreateImage(string checkCode)
-
{
-
if(checkCode == null || checkCode.Trim() == String.Empty)
-
return;
-
-
System.
Drawing.
Bitmap image =
new System.
Drawing.
Bitmap((int)Math.
Ceiling((checkCode.
Length *
14)),
22);
-
Graphics g = Graphics.FromImage(image);
-
-
try
-
{
-
Random random =
new Random
();
-
-
g.Clear(Color.White);
-
-
for(int i=0; i<25; i++)
-
{
-
int x1 = random.Next(image.Width);
-
int x2 = random.Next(image.Width);
-
int y1 = random.Next(image.Height);
-
int y2 = random.Next(image.Height);
-
-
g.
DrawLine(new Pen
(Color.
Silver), x1, y1, x2, y2
);
-
}
-
-
Font font =
new System.
Drawing.
Font("Arial",
12,
(System.
Drawing.
FontStyle.
Bold |
System.
Drawing.
FontStyle.
Italic));
-
System.
Drawing.
Drawing2D.
LinearGradientBrush brush =
new System.
Drawing.
Drawing2D.
LinearGradientBrush(new Rectangle
(0,
0, image.
Width, image.
Height), Color.
Blue, Color.
DarkRed,
1.2f,
true);
-
g.DrawString(checkCode, font, brush, 2, 2);
-
-
for(int i=0; i<100; i++)
-
{
-
int x = random.Next(image.Width);
-
int y = random.Next(image.Height);
-
-
image.SetPixel(x, y, Color.FromArgb(random.Next()));
-
}
-
-
g.
DrawRectangle(new Pen
(Color.
Silver),
0,
0, image.
Width -
1, image.
Height -
1);
-
-
System.
IO.
MemoryStream ms =
new System.
IO.
MemoryStream();
-
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
-
Response.ClearContent();
-
Response.ContentType = "image/Gif";
-
Response.BinaryWrite(ms.ToArray());
-
}
-
finally
-
{
-
g.Dispose();
-
image.Dispose();
-
}
-
}
What this code does is to generate an image present a “check code”, which is stored in Session[”ValidationCode”], which can be used to compare with user input later.
How to use it:
In your webpage, use:
to load the picture.
Then compare the user input with Session[”ValidationCode”]. If they match, then access granted.
Permalink
10.04.05
Posted in Programming at by chenty
The past whole month, I was working on SRS for the current project. Therefore, I did not have time to write anything. Moreover, there is really nothing new to write about. However, last night, right before I am ready to take off, the server went down. It is very strange, it just went down and it seems that boot sector is damage. I am sure that is not caused by virus, since I am always very careful with that. This is exactly the same case as last time the server went down. I think it might be something with motherboard southbridge.
Unfortunately, I have no emergency disk since I thought “floppy drives are history”. I was wrong. I couldn’t recover the operating system, and end up no choice but reinstalling everything. When I finally finished everything and restored the database, it was 10pm… I did not have time to fully test everything before I went home.
This morning, I got reported that the exporting program is not working. I went to Query Analyzer, and typed in several testing queries, and it seems that it does not recoganize any table name. Interestingly, all stored procedure still work. After hours playing around with premissions etc, I finally realized that it might be matter with “case”. And yes, it is case-sensitive now. I checked the database, and the collation become “_CS_”. Therefore, I decide to change it. Here is what I do:
1. In order to use ALTER DATABASE … COLLATE …, database must be in “single user” mode. Right click on database, and select “properties”, go to “options” tab, click on “restrict access” and then choose “single user”.
2. Now, close all enterprise manager window and SQL analyzer window. Restart your SQL server.
3. Open enterprise manager and open sql analyzer, type: ALTER DATABASE [table name] COLLATE [sql collation name], in my case I use “Latin_General_CI_AS”.
4. when it finishes, close all window, restart SQL Server, and then disable “restrict access”.
Run some tests, work perfectly!! <3
Permalink