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

14 December 2012

moved blog content to new site http://dotnetmirror.com

Hi,

We are happy to inform that we had launched our own site http://dotnetmirror.com

we are moving all of these blog content. please visit the site.

Thanks,
Webmaster

02 November 2012

GET/Remove the last character from a string in SQL Server

In some of cases we need remove last character from the string.In this tip we will see how to get and remove last character in string.

Sample string: 'DotNetMirror,'

Now we will remove , from the string.

Get Last Char in string:


Declare @STRING varchar(100)
set @STRING='DotNetMirror,'

select RIGHT(@String, 1)
Remove the last char(comma) in string :

IF(LEN(@String)> 0 AND RIGHT(@String, 1) = ',')
BEGIN
select substring(@String, 1, (len(@String) - 1)) --or
SET @String = LEFT(@String, LEN(@String) - 1)
END
select @String


 


 

15 September 2012

Iterating/Looping through rows without using cursors in Sql server


In this snippet we will see how to  iterating the table rows data with out using cursor in SQL server.
 
set nocount on
go

declare @tempEmpTable table(EmpID int , EmpName nvarchar(100))
declare  @varEmpID int, @varEmpName nvarchar(100)

insert into @tempEmpTable
select 1, '1st Employee' union all
select 2, '2nd Employee' union all
select 3, '3rd Employee' union all
select 4, '4th Employee' union all
select 5, '5th Employee' union all
select 6, '6th Employee' union all
select 7, '7th Employee' union all
select 8, '8th Employee' union all
select 9, '9th Employee'

select top 1 @varEmpID = EmpID, @varEmpName = EmpName  from @tempEmpTable  

while @varEmpID is not null
begin
                print @varEmpName
                delete  @tempEmpTable   where @varEmpID = EmpID
                set @varEmpID = null         
                select top 1 @varEmpID = EmpID, @varEmpName = EmpName  from @tempEmpTable  
end
Output:
1st Employee
2nd Employee
3rd Employee
4th Employee
5th Employee
6th Employee
7th Employee
8th Employee
9th Employee

12 September 2012

javascript not setting the hidden field value in ASP.NET


Today when i tried to change the value of hidden filed in java script, its not setting the value. I am using master page for an ASPX page.



<asp:HiddenField ID="hdnTest" runat="server" Value="Before JS Call" />



In java script method, when I try to assign the value it’s not getting replaced with new value.




document.getElementById('<%=hdnTest.ClientID%>').value = 'After JS Call';

then i replaced with below code

document.getElementById('ContentPlaceHolder1_
hdnTest').value = 'After JS Call';


and it worked out.

Not sure with the reason but its resolved!

06 September 2012

Rollback Transaction for table variable in sql server


Table variable is special kind of data type which will store result set and the data will returned for processing. Table variable can be used in store procedures and functions.
Table variables have limited scope and data will be stored in temporary storage so they are not affected with transaction roll backs.
In the below script we will see practically how rollback behaves for table variable.
DECLARE @student AS Table (id INT,Name VARCHAR(50) NOT NULL)
 
BEGIN TRY
    BEGIN TRANSACTION       
    INSERT INTO @student Values(1,'Ram')
    INSERT INTO @student Values(2,'Rahim')
    INSERT INTO @student Values(3,NULL)
    INSERT INTO @student Values(4,'Rohan')
    COMMIT TRANSACTION
    SELECT *,Name FROM @student -- will not return data due to error at id=3
END TRY
BEGIN CATCH
    ROLLBACK TRANSACTION
    SELECT *,id FROM @student
END CATCH
Output:
 

 In summary, we can say that Rollback Transaction does not work for table variables.

03 September 2012

convert IP address to binary format using IPAddress Class in C#


In this snippet we will see how to convert an IP address to binary format using System.Net.IPAddress class.

static List<string> ConvertIPAddressToBinary(string input)
        {
           IEnumerable<string> binaries= IPAddress.Parse(input).GetAddressBytes().Select( x=> Convert.ToString(Convert.ToInt32(x), 2).PadLeft(8, '0'));
           return binaries.ToList();
        }
 
  static void Main()
        {
            List<string> listBinaries = ConvertIPAddressToBinary(“192.168.0.1”)
            foreach (string binary in listBinaries)
            {
                Console.Write(binary + " ");
            }
        }
Output : 11000000 10101000 00000000 00000001

IPAddress class contains the address of a computer on an IP network and supports IPv4 or IPv6.