Tuesday, November 24, 2009

How to add HTML, XML code into blog post

There is very easy method.

Replace < with & l t ;
> with & g t ;

Remove the spaces and use.

ASP.NET: How to add Membership and Roles Databases to SQL Server

Go to the following location path where you have installed .NET Framework.

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727

Note: This path may vary according to your system installation folder. If so search for aspnet_regsql.exe and find out the exe file and double click on it to run.

There you get following window.



Click next and follow the guidelines to insert the Membership Tables and Roles Tables to an existing database or to a new database according to your need.

Refer the MSDN to find out how to configure Web.Config in your ASP.NET Web Site to access those databases.

Following is how I added those credentials to Web.config.

<connectionStrings>
<add name="CONNECTION_NAME" connectionString="Data Source=SERVER_NAME;Initial Catalog=DATABASE_NAME; User ID=USER_NAME;Password=PASSWORD" providerName="System.Data.SqlClient" />
</connectionStrings>

<membership defaultProvider="TESTMembershipProvider">
<providers>
<add name="eBuyMembershipProvider"
type="System.Web.Security.SqlMembershipProvider"
connectionStringName="CONNECTION_NAME"
applicationName="APPLICATION_NAME"
minRequiredPasswordLength="5"
minRequiredNonalphanumericCharacters="0" />
</providers>
</membership>

<roleManager enabled="true"
defaultProvider="TESTSqlRoleProvider"
cacheRolesInCookie="true"
createPersistentCookie="false"
cookieProtection="All">

<providers>
<add name="TESTSqlRoleProvider"
type="System.Web.Security.SqlRoleProvider"
applicationName="APPLICATION_NAME"
connectionStringName="CONNECTION_NAME" />
</providers>
</roleManager>

Replace what indicated in CAPITAL to your database and application names.

Now you can access the Membership and Roles providers using your application code directly.

Example:

Membership.GetAllUsers();
Roles.CreateRole("Administartor");

Experience the powers of ASP.NET Security.

Refer MSDN for detailed tutorials on providing SQL Membership Authentication and Role base authorization.

C#.NET: How to convert ArrayList to String Array or Array of Defined type

There is a method called ToArray() which can be used for this purpose. There is one overload for this method too with Type as a parameter. Refer to following example.
Which converts an object ArrayList into string Array.

ArrayList list = new ArrayList();
String[] arr = new String[list.Count];
arr = (String[])list.ToArray(typeof(string));

For more information please refer following MSDN.

Tuesday, November 10, 2009

How to get System Date from SQL Server

Use the GETDATE() function as follows.

SELECT GETDATE() AS Today

Result is as follows. Note that function will return both date and time.

Today
-----------------------
2009-11-10 17:48:00.310

According to what date type you need you have to convert the date returns from the GETDATE() function. For an example if you need date only then use the CONVERT function as follows.

SELECT convert(varchar,GETDATE(), 101) AS Today

Result is as follows. Note that follow code will converts the result of GETDATE() to a VARCHAR and sets the style to 101(MM/DD/YYYY).

Today
----------
11/10/2009

There are various date CONVERT formats you can used according to your need.

CONVERT(varchar,GETDATE(),108) 'hh:mm:ss'
CONVERT(varchar,GETDATE(),114) 'hh:mm:ss:mmm'
CONVERT(varchar,GETDATE(),113) 'dd Mmm yyyy hh:mm:ss:mmm'
CONVERT(VarChar(50), GETDATE(), 104)
CONVERT(VarChar(50), GETDATE(), 103)
CONVERT(VarChar(50), GETDATE(), 100)
CONVERT(VarChar(50), GETDATE(), 101)

How to dynamically number rows in a SELECT statement

I found a good article on above topic which helped me a lot to do operations in MS SQL Server (2005 or 2008) using rank() and row_number() methods. So I thought of adding this post with a link to that article for my and your further references.


http://support.microsoft.com/kb/q186133/



Following is a sample query I wrote to fetch data from database table called MiniCategory and rank is returned as RowID.

SELECT rank() OVER (ORDER BY m.MiniCatId) AS RowID, m.MiniCatId, m.MiniCatName
FROM [MiniCategory] m WHERE m.SubCatId =1 ORDER BY RowID


Following is a sample query I wrote to fetch data from database table called MiniCategory which outputs the records of the table according to the descending order of HitCount and the row numbers will adjust in according to.

SELECT row_number() OVER (ORDER BY HitCount DESC)
AS RowId, s1.MiniCatId,s1.HitCount,s1.MiniCatName,s1.SubCatId
FROM (SELECT MiniCatId, MiniCatName, SubCatId, HitCount=MAX(HitCount)
FROM [MiniCategory]
GROUP BY MiniCatId, MiniCatName,SubCatId) AS s1
WHERE SubCatId = 1

Thursday, November 5, 2009

How to add tab icons to your web site

It is very simple. What you have to do is add the following code to the <head> of your html file.

<head>
<link rel="shortcut icon" href="images/logo.gif">
</head>

Here replace the path of your image with "images/logo.gif" and save and refresh the web page.
Here you go... you will find the icon on the tab and address bar. Isn't it very simple?

Note: I have checked this with only Mozilla FireFox browser.

Tuesday, November 3, 2009

How to reset primary key indent to 1 after deletion of records in SQL Server

Do you need to reset auto incremental primary key column index to 1 after deleting some records in the table in Microsoft SQL Server. Therefore what you have to do it run the following code as a query in the relevant database.

DBCC CHECKIDENT (‘Table_Name’, RESEED, 0)

Replace “Table_Name” with your Table name and the above query will change the primary key column index to 0 and when you insert next record column will start from index 1.