Sunday, June 19, 2011
Dù bạn đến với lập trình (Programming) như thế nào chúng tôi cũng hy vọng với hiểu biết giới hạn cùng cố gắng nhỏ nhoi của mình sẽ giúp bạn một phần nào đó.
Tất cả những gì ở đây là sự chia sẻ. Chúng ta sẽ cùng nhau đi vào thế giới lập trình theo một cách đơn giản nhất.
Mỗi người trong chúng ta đến với lập trình (Programming) theo những cách khác nhau. Người thì vì sự mê say , người thì tò mò, người lại muốn tìm một nghề nghiệp...Mặc dù vậy, theo chúng tôi, chúng ta cần xác định một điều:
Nếu thật sự mong muốn học được một ngôn ngữ lập trình, đừng bao giờ chạy theo "phong trào"
Nếu bạn quyết định chọn C thì cố gắng theo đến cùng, hay ít nhất cũng nắm vững nền tảng cơ bản của ngôn ngữ C. Nếu bạn quyết theo ASP thì nên tìm hiểu đến nơi đến chốn.
Không có ngôn ngữ nào hơn ngôn ngữ nào cả nếu bạn không nắm rõ.
Khi bạn thật sự thuần thục một ngôn ngữ, việc chuyển qua tìm hiểu một ngôn ngữ khác sẽ rất dễ dàng với bạn.
Chúng ta sẽ cũng nhau thảo luận về ASP.NET. ASP.NET là Active Server Pages .NET (.NET ở đây là .NET framework). Nói ngắn gọn thì ASP.NET là một công nghệ dùng để phát triển các ứng dụng về mạng. Bạn lưu ý ở chổ ASP.NET là một phương pháp tổ chức hay framework để thiết lập các ứng dụng cho mạng dựa trên CLR (Common Language Runtime) chứ không phải là một ngôn ngữ lập trình. Ngôn ngữ lập trình được dùng để diển đạt ASP.NET thông thường là Visual Basic .NET, C# (C sharp), C++, JScript ...Bạn có thể viết ASP.NET bằng VB.NET hay C# chẳng hạn và chúng sẽ được compiled at run time, chứ không phải là Script như trong ASP
Tuy mang tên gần giống như ASP cổ điển nhưng ASP.NET không phải là ASP.ASP (Active Server Pages) là ngôn ngữ lập trình web được viết bởi hãng Microsoft. Chính xác hơn thì ASP không hẳn là một ngôn ngữ lập trình, nó là môi trường kịch bản trên máy chủ (Server-side Scripting Environment) dùng để tạo và chạy các ứng dụng Web động và có tương tác. Các trang web viết bằng ngôn ngữ này có phần mở rộng là .asp (ví dụ HelloWorld.asp) thay vì .htm hay .html. Nội dung file ASP về cơ bản rất giống file Html bình thường, nó bao gồm các cú pháp html trộn lẫn các mã lập trình ASP (còn gọi là các script, được viết bằng VBScript hay JavaScript). Các Script trong ASP thực thi trên server.
còn tiếp ...
Labels: học lập trình
Saturday, June 4, 2011
I get asked this question a lot as I go around demonstrating the benefits of using URL Rewriting in ASP.NET, how can I process wildcard extensions and just directories in IIS 6.0 on Windows 2003 and ASP.NET? It is very simple, but not obvious at all. This entry will walk you through configuring IIS to process all request through the ASP.NET engine.
The first thing you must have is access to the IIS Management console and Administrator privileges to the web server. Once you have these things in place you need to open the properties dialog for the Web site you will be configuring.
IIS Web Site Properties Dialog
Select the 'Home Directory' tab. Once on this tab select the 'Configuration' Button. This will display a three tab dialog, the first tab, Mappings is what we are interested in. Notice the lower portion of the tab list 'Wildcard Application Maps', here we will want to add a reference to the ASP.NET engine. The easy way to do this is to double click on one of the ISAPI extensions in the top portion and copying the path to the ASP.NET .dll file. You will need to select one of the ASP.NET file extensions, like .ascx or .asax to do this. The path should look something like this: DRIVE:\%windows%\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll.
IIS Web Site Configuration Dialog
Now that we have copied the path to the .NET engine we need to add it to the Wildcard mappings. Select the Insert button to the right of the mappings list. Now you will have an Add/Edit Application Extension Mapping dialog displayed. There are two pieces of information you need to supply, the ASP.NET engine path and uncheck 'Verify File Exists'. Now select 'OK' until all the property windows are closed.
IIS Wildcard Application Extension Mapping Dialog
Now all URL requests to the web site will be processed by ASP.NET no matter what the extension or lack of extension. Pay attention for my next entry on how to leverage this feature to map URLs without extensions to actual dynamic pages on your Web site.
Reference - Installing Wildcard Mappings in IIS 6.0
http://professionalaspnet.com/archive/2007/07/27/Configure-IIS-for-Wildcard-Extensions-in-ASP.NET.aspx
Labels: C#
Clean Code by "Uncle Bob" Martin
A must for any developer with an interest in producing better code. Read Reviews or Buy Now
URL Rewriting with URLRewriter.Net
URL Rewriting has lots of benefits, listing its main benefits
- SEO Friendly URL
- Secured URL
- No need to change bookmark with change in site structure.
Before URL Rewriting my URL looks like
http://localhost:2661/URLRewrite2/DynamicPage.aspx?MyTitleId=1
After URL Rewriting URL is changed to
http://localhost:2661/URLRewrite2/Article/Asp-Net-website-paths-1.aspx
Lets Understand URL Rewriting with Simple Example
A Website displaying articles list in a gridview on clicking the article link, it will display dynamically generated article content.
Before URL Rewriting when you mouse-over 1st Article Link, "Asp.net Website Path" it uses query string to display the article content.
Dynamic page display Querysting, before URL Rewriting.
After URL Rewriting we will achieve how SEO Friendly URL is used to display article content.
Now, lets understand how we can achieve it.
For URL Rewriting we are using URLRewriter.Net which is available free. Download URLRewriter.Net
Step-by-Step Explanation
Step 1: Download Binary Files for URLRewriter.Net
Step 2: Add Reference to Binary Files, Right click project "Add Reference" and add binary files.
Step 3: Update Web.Config File to make URLRewriter.Net works.
<configuration>
<configSections>
<section name="rewriter"
requirePermission="false"
type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" />
configSections>
<system.web>
<httpModules>
<add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter" />
httpModules>
system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule" />
modules>
<validation validateIntegratedModeConfiguration="false" />
system.webServer>
<rewriter>
<rewrite url="~/Article/(.+)-(.+).aspx" to="~/DynamicPage.aspx?MyTitleId=$2"/>
rewriter>
configuration>
Step 4: Adding Function to Generate SEO Friendly URL from given Title
public static string GenerateURL(object Title, object strId)
{
string strTitle = Title.ToString();
#region Generate SEO Friendly URL based on Title
//Trim Start and End Spaces.
strTitle = strTitle.Trim();
//Trim "-" Hyphen
strTitle = strTitle.Trim('-');
strTitle = strTitle.ToLower();
char[] chars = @"$%#@!*?;:~`+=()[]{}|\'<>,/^&"".".ToCharArray();
strTitle = strTitle.Replace("c#", "C-Sharp");
strTitle = strTitle.Replace("vb.net", "VB-Net");
strTitle = strTitle.Replace("asp.net", "Asp-Net");
//Replace . with - hyphen
strTitle = strTitle.Replace(".", "-");
//Replace Special-Characters
for (int i = 0; i < chars.Length; i++)
{
string strChar = chars.GetValue(i).ToString();
if (strTitle.Contains(strChar))
{
strTitle = strTitle.Replace(strChar, string.Empty);
}
}
//Replace all spaces with one "-" hyphen
strTitle = strTitle.Replace(" ", "-");
//Replace multiple "-" hyphen with single "-" hyphen.
strTitle = strTitle.Replace("--", "-");
strTitle = strTitle.Replace("---", "-");
strTitle = strTitle.Replace("----", "-");
strTitle = strTitle.Replace("-----", "-");
strTitle = strTitle.Replace("----", "-");
strTitle = strTitle.Replace("---", "-");
strTitle = strTitle.Replace("--", "-");
//Run the code again...
//Trim Start and End Spaces.
strTitle = strTitle.Trim();
//Trim "-" Hyphen
strTitle = strTitle.Trim('-');
#endregion
//Append ID at the end of SEO Friendly URL
strTitle = "~/Article/" + strTitle + "-" + strId + ".aspx";
return strTitle;
}
Step 5: Changing DataBinder.Eval Function in .Aspx Page to reflect changes in URL of Grid.
Note: Learn more about DataBinder.Eval Function
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" Width="788px">
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<Columns>
<asp:TemplateField HeaderText="Title">
<ItemTemplate>
<asp:HyperLink ID="hlTitle" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Title")%>' NavigateUrl='<%#GenerateURL(DataBinder.Eval(Container.DataItem,"Title"),DataBinder.Eval(Container.DataItem,"Id"))%>'>asp:HyperLink>
ItemTemplate>
asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:Label ID="lblDesc" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Description")%>'>asp:Label>
ItemTemplate>
asp:TemplateField>
Columns>
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
asp:GridView>
Now, Lets Check the stuff so far developed.
Assigning SEO Friendly URL in to grid.
On clicking URL inside grid it will point to Dynamically Generated Page with SEO Friendly URL, rather than QueryString.
Things to consider while URL Rewriting.
Problem 1: Page Postback, will turns User Friendly URL into Original URL.
Problem 2: CSS, Image Files pointing to URL Rewriting Page won't work, as they might be pointing with absolute path.
Problem 1: Page Postback for Page displaying URL Rewritten URL
Page Postback of Page displaying User friendly URL will turns into original state when same page postback occurs. In our example, I am adding one button and trying to make Page Postback. You will notice that Page Postback will turns the User Friendly URL into original URL containing QueryString.
For Resolving Page PostBack problem for Page displaying URL Rewritten URL
This article is inspired from Scott's URL Rewritten article. Adding two files as mentioned by scott. If you are developing code in VB download files from Scott's article, else for C# download files with Sourcecode at the end of this article.
Now, lets test the Page Postback by clicking on Button, you will notice this time, URL remains the same.
Problem 2: Image Display Problem
Now, lets display image on for this page and lets observe what problem we may run into. I have added following line to display image, but it won't works.
<img src="Images/article.gif" />
Resolve Problem, by refrencing file from root.
<img src="../Images/article.gif" />
Download Source-Code for URL Rewriting
http://dotnetguts.blogspot.com/2008/07/url-rewriting-with-urlrewriternet.html
Labels: C#
Sunday, May 22, 2011
One problem with HTML forms is it is hard to style the elements to fit into your design. The tutorial will show you how to style the hardest of them all, the select box.
The Plan
Unfortunately browsers allow limited skinning of select boxes. So the plan is to use jQuery to change a select box into a text box and a unorded list for the dropdown. When a user clicks on the text box it will display the list below the textbox, just like a regular select box, but it’s easier to stylesee more and download: http://www.devirtuoso.com/2009/08/styling-drop-down-boxes-with-jquery/
Labels: css
Tuesday, May 17, 2011
Adding a column to a table in SQL Server is done using the ALTER TABLE tablename ADD command. When adding columns you can specify all the same settings available when creating a table.
In the example below, we will create a small sample table, then add columns using the ALTER TABLE command.
Multiple columns can be specificied by using the syntax below. First issue an alter table command then add the column name, data type, nullable, and default value.
CREATE TABLE dbo.Employees |
( |
EmployeeID int IDENTITY (1,1) NOT NULL PRIMARY KEY NONCLUSTERED |
) |
GO |
ALTER TABLE dbo.Employees |
ADD |
FirstName varchar (50) NULL |
,LastName varchar (50) NULL |
,SSN varchar (9) NULL CONSTRAINT ssn_unique UNIQUE |
,IsTerminated bit NOT NULL DEFAULT 0 |
,DateAdded datetime DEFAULT GETDATE () |
,Comments
varchar
(255) SPARSE
NULL
-- SQL Server 2008 sparse column
Labels: SQL Server 2005
The Alter Column statement can modify the data type and the Nullable attribute of a column. The syntax is the same for SQL Server 2005 and SQL Server 2008 except 2008 allows the sparse attribute to be changed.
For the example below, we will begin by creating a sample table, then we will modify the columns.
CREATE TABLE dbo.Employee |
( |
EmployeeID INT IDENTITY (1,1) NOT NULL |
,FirstName VARCHAR (50) NULL |
,MiddleName VARCHAR (50) NULL |
,LastName VARCHAR (50) NULL |
,DateHired datetime NOT NULL |
) |
-- Change the datatype to support 100 characters and make NOT NULL |
ALTER TABLE dbo.Employee |
ALTER COLUMN FirstName VARCHAR (100) NOT NULL |
-- Change datatype and allow NULLs for DateHired |
ALTER TABLE dbo.Employee |
ALTER COLUMN DateHired SMALLDATETIME NULL |
-- Set SPARSE columns for Middle Name (sql server 2008 only) |
ALTER TABLE dbo.Employee |
ALTER
COLUMN
MiddleName
VARCHAR
(100) SPARSE
NULL
Labels: SQL Server 2005
Tuesday, May 10, 2011
How to use ASP & MySQL Add/Insert Multiple Rows Record This is tutorial asp developers how to using ASP add insert multiple record to MySQL table.
ShotDev Focus:
- ASP & MySQL add insert multiple record.
Example
see more here
Labels: ASP
Thursday, May 5, 2011
DECLARE mycur CURSOR FOR select O.type_desc,schema_id,O.name
from
sys.objects O LEFT OUTER JOIN
sys.extended_properties E ON O.object_id = E.major_id
WHERE
O.name IS NOT NULL
AND ISNULL(O.is_ms_shipped, 0) = 0
AND ISNULL(E.name, '') <> 'microsoft_database_tools_support'
AND ( O.type_desc = 'SQL_STORED_PROCEDURE' OR O.type_desc = 'SQL_SCALAR_FUNCTION' )
ORDER BY O.type_desc,O.name;
OPEN mycur;
DECLARE @schema_id int;
DECLARE @fname varchar(256);
DECLARE @sname varchar(256);
DECLARE @ftype varchar(256);
FETCH NEXT FROM mycur INTO @ftype, @schema_id, @fname;
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sname = SCHEMA_NAME( @schema_id );
IF @ftype = 'SQL_STORED_PROCEDURE'
EXEC( 'DROP PROCEDURE "' + @sname + '"."' + @fname + '"' );
IF @ftype = 'SQL_SCALAR_FUNCTION'
EXEC( 'DROP FUNCTION "' + @sname + '"."' + @fname + '"' );
FETCH NEXT FROM mycur INTO @ftype, @schema_id, @fname;
END
CLOSE mycur
DEALLOCATE mycur
GO
Labels: SQL Server 2005
DECLARE @Sql NVARCHAR(500) DECLARE @Cursor CURSOR
SET @Cursor = CURSOR FAST_FORWARD FOR
SELECT DISTINCT sql = 'ALTER TABLE [' + tc2.TABLE_NAME + '] DROP [' + rc1.CONSTRAINT_NAME + ']'
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc1
LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc2 ON tc2.CONSTRAINT_NAME =rc1.CONSTRAINT_NAME
OPEN @Cursor FETCH NEXT FROM @Cursor INTO @Sql
WHILE (@@FETCH_STATUS = 0)
BEGIN
Exec SP_EXECUTESQL @Sql
FETCH NEXT FROM @Cursor INTO @Sql
END
CLOSE @Cursor DEALLOCATE @Cursor
GO
EXEC sp_MSForEachTable 'DROP TABLE ?'
GO
Labels: SQL Server 2005
Monday, May 2, 2011
--**********************
DECLARE @FirstDay smalldatetime, @NumberOfMonths int
SELECT @FirstDay = '2008-02-01', @NumberOfMonths = 1
;WITH Days AS (
SELECT @FirstDay as CalendarDay
UNION ALL
SELECT DATEADD(d, 1, CalendarDay) as CalendarDay
FROM Days
WHERE DATEADD(d, 1, CalendarDay) < DATEADD(m, @NumberOfMonths, @FirstDay)
)
SELECT CONVERT(char(8), CalendarDay, 112) FROM Days
--**********************
Labels: SQL Server 2005