Objects in this mirror are closer to Microsoft Technologies. DNM objective is to help users on Microsoft technologies by providing Articles, snippets, Interview Questions.

31 August 2012

Remove special characters (!@#$%.etc) with empty string from a string using JavaScript


Using java script if you want to remove special characters from a string, we can achieve using regular expression.

The below code will allow alphabets (lower and upper case) and numbers in a string.
var str = '@This is sample sting from <.NET Mirror> Ready! 1#2%3*Go';
var rStr = str.replace(/[^a-zA-Z0-9]+/g, '');
alert(rStr);
Output : ThisissamplestingfromNETMirrorReady123Go

If you want allow some of special characters then add them to expression. In the below code we will try to add space and dot.  so now the expression allows alphabets, numbers space and dot.

var rStr1 = str.replace(/[^a-zA-Z0-9 .]+/g, '');
alert(rStr1);
Output: This is sample sting from .NET Mirror Ready 123Go

SET IDENTITY_INSERT for Table Variable in SQL Server


SET IDENTITY_INSERT - Allows to insert explicit values to identity column of the table.

SET IDENTITY_INSERT ON/OFF works for talbes and temporary table.
we can not use for table variables.

If we run a sample query below
DECLARE @student TABLE (ID INT IDENTITY,Name VARCHAR(50))
 
INSERT INTO @student(Name) Values('A')
INSERT INTO @student(Name) Values('B')
INSERT INTO @student(Name) Values('C')
INSERT INTO @student(Name) Values('D')
 
DELETE FROM @student WHERE ID = 3
 
SET IDENTITY_INSERT @student ON
 
INSERT INTO @student(ID,Name) Values(2,'E')

we will get error as
Msg 102, Level 15, State 1, Line 10
Incorrect syntax near '@student'



28 August 2012

Color themes with Visual studio 2012 and How to Change theme


Visual studio 2012 has 2 types of themes (Dark and Light). You can see the sample screenshots below.

If you want to change/switch the theme, you can do from Tools  Options Environment General Color theme select the dropdown items which has(Dark/Light).

Dark Theme look:



Dark Color theme VS2012
Light Theme look
Light Color theme VS2012
 

27 August 2012

Page.RouteData is null or Page not found in URL routing


If we are using ASP.NET URL rewriting concept, you might Registered routes correctly in Global.aspx like below


  protected void Application_Start(object sender, EventArgs e)
    {
        RegisterRoutes(RouteTable.Routes);
    }

  public static void RegisterRoutes(RouteCollection routeCollection)
    {
        routeCollection.MapPageRoute("RouteArticle", "Articles/{Id}", "~/Resources/ViewArticle.aspx");
}

 ViewArticle.aspx.cs:


int resourceContentID = int.Parse(Page.RouteData.Values["Id"].ToString());
 
From the browser if you try to access the page(http://localhost/website/articles/13) ,sometimes we will get run time error as Page.RouteData as null or HTTP page not found in browser.

In order to avoid the error add below section to web.config file. 

<configuration>
       <system.webServer>
              <modules runAllManagedModulesForAllRequests="true"/>
       </system.webServer>
</configuration>

24 August 2012

Microsoft changed the logo after 25 years

After 25 years, Microsoft has changed the logo on 23-Aug-2012. The new look you can find from microsoft sites.

The new logo looks like










For more info : http://blogs.technet.com/b/microsoft_blog/archive/2012/08/23/microsoft-unveils-a-new-look.aspx

22 August 2012

What is the meaning of # in C#


The name "C sharp" was inspired by musical notation where a sharp indicates that the written note should be made a semitone higher in pitch. This is similar to the language name of C++, where "++" indicates that a variable should be incremented by 1.
Due to technical limitations of display (standard fonts, browsers, etc.)    and the fact that the sharp symbol (♯ music sharp sign) is not present on the standard keyboard, the number sign (# number sign) was chosen to represent the sharp symbol in the written name of the programming language. This convention is reflected in C# Language Specification.

Microsoft has used this # symbol in other languages like F#, J#.
Source : wiki

21 August 2012

First time click on fileupload not working with ajax updatepanel


If we are using fileupload  control inside an AJAX updatepanel, sometimes we face an issue like file upload will not work for first time. In that case add below code in your page_load event.


protected void Page_Load(object sender, EventArgs e)
    {
        Page.Form.Attributes.Add("enctype", "multipart/form-data");
    }


Difference between count(*) and count(column_name)


We might assume that count(*) and count(column_name) will return same result count. But NO, in case of column holds any null values.
Count (*) – returns all values (including nulls and duplicates)
Count (Column_Name) – return all Non-NULL values
In the below script we will see how it works. So that it will be easy for us to understand.

create table #tempTable(Name char(1))



insert into #tempTable values('A')

insert into #tempTable values('B')

insert into #tempTable values(Null)

insert into #tempTable values('C')

insert into #tempTable values(Null)

insert into #tempTable values('C')



select COUNT(*) from #tempTable

select COUNT(Name) from #tempTable



drop table #tempTable

Output:

6 and 4

The table #temptable has total 6 rows. Count(*) retruns all the rows including null/duplicates but where as count(name) returns only 4 rows which includes duplicates(‘C’) but not null values.

If you want to remove duplicates from count(Name) use COUNT(distinct Name).
select COUNT(distinct Name) from #tempTable –returns 3.

Accessing and Changing TD text at runtime


In this article we will see how to access and change the TD text at runtime.
The TD tag belongs to table (HTML element) which will not be accessible in code behind page. So we will mark the TD tag with runat=”server” and provide an ID to access in code page. To set/get the td text we can use Inner Text property of td element.
 
In the below example we will see how to read/write text of td element.
 
Desing Page(.aspx)
<form id="form1" runat="server">
    <div>
    <h1>
        <table>
            <tr>
                <td runat="server" id="tdAccess">
                Design time td text
                </td>
            </tr>
        </table></h1>
        <asp:Label ID="lblTDText" runat="server"></asp:Label><br />
        <asp:Button ID="btnChangeTD" runat="server" Text="Change TD Text"
            onclick="btnChangeTD_Click" />
    </div>
    </form>
Code behind(.cs)
protected void Page_Load(object sender, EventArgs e)
    {
        lblTDText.Text = tdAccess.InnerText; // get td element text
    }
    protected void btnChangeTD_Click(object sender, EventArgs e)
    {
        tdAccess.InnerText = "Runtime td text from button click"; //// set td element text
    }
Explanation: we have TD tag inside table element which is marked with runat=”server” and id as tdAccess. On page load we are accessing td text (design time text) and assigning it to label control and from the button click, we are changing the td text at runtime.