Monthly Archives: June 2013

Line breaks in MVC

In MVC #, if you use simple string replacement to replace new line with line break like this

string.replace("\r\n","
");

it may not work. Use the following in that case

@MvcHtmlString.Create(Html.Encode(Model.Description).Replace("\r\n", " 
 "))

Here Model.Description in coming from the database that contains text.

Solution No 2

Use CSS instead to solve the problem

div
{
white-space:pre-line;
}

Or you can also use white-space:pre;
white-space:pre, preserves all white space characters.
white-space:pre-line, does not preserve white spaces at the start of the line.

For tutorial about pre and pre-line, check this w3 link

Remove duplicate files on Mac using Terminal

I tried to use this link to find and remove duplicates files (mp3’s) but was not successful. I spent days to make it work on Mountain Lion. Finally I got something that is partially working

First go to a subdirectory where you want to identify all the duplicates, copy and past this command (make sure there is no line break in this command – complete command should be in one line)

find . size +20 ! -type d -exec cksum {} ";" | sort | tee ./f.tmp | cut -f 1,2 -d ' ' | uniq -d 

This will create f.tmp file in the current folder inside tmp folder. It will contain all the files links and their md5 value. The list is sorted. You can easily guess how many duplicate files you have by checking the values of first two columns.

Next use the following command to identify the duplicate files only. Note if you have [ or ] characters, remove those from the files first!

cut -f 1,2 -d ' ' f.tmp | uniq | grep -v  -f f.tmp > dup.txt

The list of duplicate files will be dumped (copied) into dup.txt files. You can open it with nano editor or with any editor of your choice.

open -a texteditor dup.txt

You can then supply this file to find to delete the duplicate files.

 

Cut – (cut followed by dash) means read from input stream (that is not a file)

Complete Command in one go

This works on Mountain Lion OSX 10.8.3

find . -size +20 \! -type d -exec md5 -r {} \; | sort | tee ./f.tmp | cut -f 1 -d ' ' | uniq -d | grep - -hif ./f.tmp > dup.txt

Mac Terminal Getting Started

How to work with command line on a mac? The following is similar to DOS or command line utilities or commands on Windows PS.

Start Terminal
click search icon (glass magnifier) and Type terminal to get into terminal
Quit Terminal
Command+ q or click on red close button on the window

Run a command
Type an valid command, for example ls command print directory listing, find . ‘string’ search for all files in current and subdirectories searching for ‘string’
Quit a command
Control + Z lets you quit to stop a command. It is similar to break on windows.

Quit a help screen
Press Q alone lets you exit a help screen. For example you want to quit in the middle of help grep. Hit Q when you have found what you were looking for

Working with Nano

Nano is small text editor available on mac. You can edit files directly inside terminal without using an windows program. Here is a quick tutorial.

Start Nano
type “nano” or type “nano filename” to open a particular text file in nano editor.

Close Nano
Control+x, to close nono. This will ask you if you want to save you changes. Hit N if you dont want to save any changes

A simple tutorial using Nano

Goal: Create a text file called, myfile.txt and your name and phone number in it and save it. Then we will load the file to see if the contents are there.

1. Type nano myfile.txt
This will open nano text editor. No file will be created yet.
2. Start writing text. Writing anything you want
3. Control+O to write files to the disk
4. Control+x to quit

Nano Commands

The following are some of the commands display on the main windows. Notice that all commands are preceded by control key. Control + letter will execute that command. The commands changes in each windows, depending on what is displayed on the screen.

Screen Shot 2013-06-27 at 9.51.35 PM

Control + X = Quit
Control + V = Navigate to Next page (contents must exist in next page)
Control + Y = Navigate to previous page
Control + K = Cut text. It will not cut text over multiple lines though.
Control + U = Past the cutted text. Uncut text.
Control + O = Write file to the disk (same as save file).

CSS Immediate Successor Operator ( >)

CSS Immediate Successor Operator (>) can give a little bit an expected (and potentially) buggy result when combined with first-child pseudo class. Consider the following code jsfiddle.net

/* HTML */
<div id=parent>
    <p>first </p>
    <p>second</p>
    <div id=child_div>
        <p>first</p>
        <p>second</p>
    </div>
</div>

/* CSS */
#parent>p:first-child{
color:red;
}

The first

element in parent div is highlighted red, which is correct. Now insert a
as the first element parent div jsfiddle.net and now now the first element is not highlighted?

is still an immediate successor of parent div but?

Removing first-child pseudo class, the immedidate successor works correctly. Example jsfiddle

Find phone number on Samsung Galaxy s3

On Android 4.1.2 that is on Samsung Galaxy S3, finding your phone number can be a little difficult. There is no “About Phone” option in setting.

Procedure

1. Soft Left click, go to setting (bottom option).

2. Go to the very last option in the long menu, that is “About Device”

3. Click on second icon from top, that is “Status”

Your phone number is right in the middle of the list that says, “My Phone Number”.

What is REST?

REST (stands for Representational State Transfer) is a architecture by which data can be transferred between two points.  There are two parts of it

1. Request

2. Response

REST Request consists of HTML GET OR POST URLS. RESET Response can be anything from complete html output to XML output to JSON.

REST is a common way of implementing APIs over web services. The request is asynchronous. You can think of a REST protocol as a virtual browser in the sky. You post something to the browswer (in the form of REST POST or GET). The virtual browser renders the response. You then call another request to collect the output.

The request and response does not need to be one after each other. Once you submit a request, you can assume, the response will be there no matter when you collect the response.

This is just a basic overview of REST for beginner and no means it is a complete description of REST protocol.

 

Bind data to NavigateUrl – ASP.NET

If you are trying to bind NavigateUrl in ASP.NET to a database field using the following syntax

NavigateUrl='<%# Eval("content") %>'

You would notice that it does not work. It should be very straight forward but it isn’t. You may get the following errors.

Error: Databinding expressions are only supported on objects that have a DataBinding event. System.Web.UI.WebControls.HyperLinkField does not have a DataBinding event

Error: Literal content (‘<asp:hyperlinkfield DatatextField=”DBUrl”  HeaderText=”DBUrl”   SortExpression=”DBUrl” NavigateUrl=”‘) is not allowed within a ‘System.Web.UI.WebControls.DataControlFieldCollection’.

Bind data to NavigateUrl

You cannot bind data directly to NavigateUrl, if that is what you are trying to do (I was doing the same thing). You have to use <asp:hyperlinkfieldcontrol> own provided fields to do that + some improvisation. You can accomplish it using this code

<asp:hyperlinkfield 
DataNavigateUrlFields="DataField" #this field catches the database data
DatatextField="DataField"  
HeaderText="Data Field"   
SortExpression="DataField" 
DataNavigateUrlFormatString="{0}" #this field displays it
target="_blank" />

If you want to open the link in a new window, use target=”_blank”, as an additional property of hyperlinkfieldcontrol (this will not show in property window but will work if you past it directly).

Some other useful links that I browsed before finding this solution

1. http://forums.asp.net/t/1469908.aspx/1

2. http://stackoverflow.com/questions/1779481/hyperlink-with-navigateurl-with-eval-where-is-the-mistake

3.http://oscarvalles.wordpress.com/2010/02/05/literal-content-text-is-not-allowed-within-a-system-web-ui-webcontrols-datacontrolfieldcollection/

4. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hyperlinkfield.datanavigateurlformatstring.aspx

5. http://www.sitepoint.com/forums/showthread.php?636986-Gridview-with-Navigate-URL-Databind

 

Grouping and back references in Regular Expressions

You are give a name

Micheal Doughlas   # match only first part of the name Solve the puzzle

Someone ask to search the string but give me only the first name, excluding the last name. How do you do it? Is there a way to write regex for this that matches the first part of the name and ignore the last part?

There isn’t any regex which can match the first part and ignore the last part but there is technique called grouping which can achieve the same purpose. Lets solve the above problem using grouping which ( ) characters. Notice the using grouping below

([a-zA-Z]*)  Test link

Lets make this a little bit more simpler

([a-zA-Z]*) ([a-zA-Z]*) Test link

You can reference these groups in variables $1, $2, … and so on. The first group in this case return first name, the second group returns last name. This may not exactly server your purpose but it might be close.