2009/05/27

C#- Convert string to int with thousand separator

string sText = "111,111,111";
int i;
i = Int32.Parse(sText, NumberStyles.AllowThousands);

C#- Thousand separator

To add thousand separator to your number, you could do this:

Convert.ToDecimal(yourNumber.Text.ToString()).ToString("N2")
or
Convert.ToDecimal(yourNumber.Text.ToString()).ToString("N4")

where N2 = 2 decimal place and N4 = 4 decimal place.

if you don't want any decimal place you can try this:
Convert.ToInt32(yourNumber.Text.ToString()).ToString("N0")

Get windows login name?

To get windows login name, you can type this in dos prompt:
echo %username%

To get windows login name in sql, you can type this:
exec master..xp_cmdshell 'echo %USERNAME%'

2009/05/22

ASP.NET AJAX 1.0

ASP.NET AJAX is a set of technologies to add AJAX (Asynchronous JavaScript And XML) support to ASP.NET. It consists of a client-side script framework, server controls, and more.

Link: http://www.microsoft.com/downloads/details.aspx?FamilyID=ca9d90fa-e8c9-42e3-aa19-08e2c027f5d6&displaylang=en

2009/05/21

Error connecting to undo manager of source file

Problem:
When you compile you see this error msg: "Error connecting to undo manager of source file ..."

Solution:
1) Right click the designer file and select delete.
2) Right click the aspx file and select Convert to Web Application.

Yay!

2009/05/20

DataGrid.DataSource return null?

There will be a time that you want to get the bound datagrid's datasource to work on it. For example: you want to have a button that can add in new row dynamically to the bound datagrid. But when you try to do this: DataTable dt = (DataTable)dg.DataSource, you will then realize that dt returns null.

Solution:
Actually .NET 2.0 would not save the datasource when the datagrid is bound. What you can do is to save the datasource into a view state variable. For example:

When you bind a datagrid, you also need to set viewstate varible to the same source:

dg.DataSource = ds;
ViewState["temp"] = ds;
dg.DataBind();

So, when you want to retrieve the datasource, you can do this:
DataTable dt = (DataTable)ViewState["temp"];

If you want to add a new row, you can do this:
DataTable dt = (DataTable)ViewState["temp"];
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
ViewState["temp"] = dt;
dg.DataSource = dt;
dg.DataBind();

Good Luck!

2009/05/16

C#- Validate before post back?

Recently I am working on SIMS (Safety Information Management System) and encountered a problem. The problem might make me sounds like a newbie but, yes, I am :).

So the problem is:
I have a textbox only accept numbers with no decimal and no letters, and I also set this textbox to auto postback so once the user enter a number and it will calculate the number with a formula.

Solution:
I have a validator with the needed regular expression pointed to that text box, all you have to do is click on the textbox and switch to html view and add in this: CausesValidation = "true", it will then check the textbox value and it must be true before doing the autopostback. Work like a charm ;)