CodeCloudy

Azure | .Net | JQuery | Javascript | Umbraco

Sort a list of strings numerically using LINQ

Lets take a case where you need to sort an array of numbers that are strings.

And the list need be sorted numerically.

One thing to note is that this is useful where you cannot convert them to int list.

 
typical way:
yourStringList.OrderBy(x => x.yourSortAttribute)
this will not sort it numerically. because they are strings.

so you need to give a comparer called SemiNumericComparer.
yourStringList.OrderBy(x => x.yourSortAttribute, new SemiNumericComparer())
 
Your have write a custom IComparer<T> class to overcome this issue. Add this class to you class library:

/// <summary>
/// Semi Numeric Comparer
/// </summary>
public class SemiNumericComparer : IComparer<string>
{
/// <summary>
/// Compares the specified s1.
/// </summary>
/// <param name=”s1″>The s1.</param>
/// <param name=”s2″>The s2.</param>
/// <returns>the specified s1.</returns>
public int Compare(string s1, string s2)
{
if (Convert.ToInt32(s1, CultureInfo.InvariantCulture) > Convert.ToInt32(s2, CultureInfo.InvariantCulture))
{
return 1;
}

if (Convert.ToInt32(s1, CultureInfo.InvariantCulture) < Convert.ToInt32(s2, CultureInfo.InvariantCulture))
{
return -1;
}

if (Convert.ToInt32(s1, CultureInfo.InvariantCulture) == Convert.ToInt32(s2, CultureInfo.InvariantCulture))
{
return 0;
}

return string.Compare(s1, s2, true, CultureInfo.InvariantCulture);
}
}

this should work now. Cheers! :)
for more information about the IComparer: http://msdn.microsoft.com/en-us/library/8ehhxeaf.aspx
Advertisements
Leave a comment »

SQL Azure migration: Built-in function ‘TEXTPTR’ is not supported in this version of SQL Server.

Not only ‘TEXTPTR’  also ‘READTEXT’ is not supported in SQL Azure.

You will find this issue when you try to execute an old version of AspState script in sql azure.

You will wonder why sql azure does not support most of the new features. it’s because the sql azure was built without the deprecated features of SQL SERVER 2008 R2.

executing the new version will solve the issue.

you can get the new version here: http://azuresqlsession.codeplex.com/

 

SQL Azure Migration Wizard (SQLAzureMW) is a cool tool that will help us analyze the issues on SQL AZURE migrations.

you can download it from here: http://sqlazuremw.codeplex.com/

6 Comments »

SQL Azure Error: Reference to database and/or server name in ‘dbName.dbo.tableName’ is not supported in this version of SQL Server.

Error you will get is:

Reference to database and/or server name in ‘dbName.dbo.tableName’ is not supported in this version of SQL Server.

 

Solution:

simply remove the db name and execute it.

‘dbName.dbo.tableName’ -> ‘dbo.tableName’

Leave a comment »

Sql azure blindly adding bulk clustered indexes

In a previous post i mentioned when migrating to SQL Azure you have to create clustered indexes for each of the table that doesn’t have a clustered index (heap tables).

the error you will get is:

Tables without a clustered index are not supported in this version of SQL Server. Please create a clustered index and try again.

Lets say you have a huge database with huge amount of heap tables. And you need to do a blind test by adding a clustered index for the first column of each table.

Step 1:

Check the heap tables you have:

— List all heap tables
SELECT SCH.name + ‘.’ + TBL.name AS TableName
FROM sys.tables AS TBL
INNER JOIN sys.schemas AS SCH
ON TBL.schema_id = SCH.schema_id
INNER JOIN sys.indexes AS IDX
ON TBL.object_id = IDX.object_id
AND IDX.type = 0 — = Heap
ORDER BY TableName

Step 2:

declare @SQL nvarchar(max)
SELECT @SQL = STUFF((
SELECT ‘; CREATE CLUSTERED INDEX NewPK_’ + TBL.name + ‘ ON ‘ + ‘[‘+TBL.name+’]’ + ‘ (‘+(
sELECT top 1 ‘[‘+column_name+’]’ FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = TBL.name
)+ ‘)’

— List all heap tables
FROM sys.tables AS TBL
INNER JOIN sys.schemas AS SCH
ON TBL.schema_id = SCH.schema_id
INNER JOIN sys.indexes AS IDX
ON TBL.object_id = IDX.object_id
AND IDX.type = 0 — = Heap
ORDER BY TBL.name

FOR XML PATH(”)),1,2,”)
SET @SQL = ‘ ‘ + @SQL
PRINT @SQL — execute until this statement to see only the script to be executed.

EXECUTE (@SQL) — execute until this statement to actually execute the script

NOTE: this is only good for a blind test. but if you are going to move forward. you need to consider the tables case by case and create the clustered indexes for the suitable columns or else it will bring huge performance issues.

Leave a comment »

Automatically select the culture of the browser

In order to automatically select the culture of the browser,

add the following globalization element to the web.config file:

<configuration>

 <system.web>

    <globalization culture=”auto” uiCulture=”auto” />

We can chage it at page level but I dont know how to access to asp.net page of XtraReport (if there is page behind).

   <%@ Page UICulture=”auto” Culture=”auto” %>

     or in code.

Leave a comment »

.Net Error Handling

  • Try/catch – (for page level error)
  • Page_Error: Occurs when an error occurs within the Web page. This event is in the Web form. –  (for page level error)

private void Page_Error(object sender, EventArgs e)
{}

  • Global_Error: Occurs when an error occurs within the application. This event is in the Gloabl.asax file.

private void Page_Error(object sender, EventArgs e)
{}

  • Application_Error: Occurs when an error occurs within the application. This event is in the Gloabl.asax file.

void Application_Error(object sender, EventArgs e)
{}

  • CustomError

<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<compilation debug=”true” />

<!– Turn on Custom Errors –>
<customErrors mode=”On”
defaultRedirect=”DefaultRedirectErrorPage.aspx”>
<error statusCode=”404″ redirect=”Http404ErrorPage.aspx”/>
</customErrors>

</system.web>
</configuration>

  • HandleErrorAttribute  – Represents an attribute that is used to handle an exception that is thrown by an action method. (MVC specific)  –  (for page level error) http://msdn.microsoft.com/en-us/library/system.web.mvc.handleerrorattribute.aspx
  • I would use Application_Error for those cases where the page isn’t handling the error for some reason, as a last resort to log the error. If a page can handle its own errors and gracefully deal with them, it should do so in Page_Error.
  • in simple words, if the page can handle it the page will use Page_Error to do it. or else it will be handled by the Application_Error.
  • Methods in the Server object are used to handle the exception in the error events.
    • GetLastError: Gets the last exception that occurred on the server.
    • ClearError: Use this method to handle the exception and stop the error to trigger the subsequent error event or display the error to the user.

	
Leave a comment »

ASP.NET MVC Which layer should I choose for testing?

For Verifying security issues and for Verifying effectiveness of input corruption you need to design a testing methodology to test against controllers.

For Centralized business rules and directly validate the accuracy of business rules you need to design a testing methodology to test against the domain model.

Leave a comment »

Man-in-the-middle attack (MITM)

the attacker makes independent connections with the victims and relays messages between them, making them believe that they are talking directly to each other over a private connection, when in fact the entire conversation is controlled by the attacker. The attacker must be able to intercept all messages going between the two victims and inject new ones.

Main the middle.JPGThe attack can succeed only when the attacker can impersonate each endpoint to the satisfaction of the other; it is an attack on (or lack of) mutual authentication. Most cryptographic protocols include some form of endpoint authentication specifically to prevent MITM attacks. For example, SSL can authenticate one or both parties using a mutually trusted certification authority.

Solution:

Use SSL authentication; You need to install a certificate on the web server. and need to force the log-in form to use SSL.

Leave a comment »

CSS Style to Make you text unselectable

.userSelectNone
{
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
-o-user-select: none;
user-select: none;
}

Leave a comment »