Saturday, June 27, 2015

Generic Class in C#

Generic Classes have type parameters. Using Generic Classes a developer can encapsulate the operations which are not of specific data type.

Here is the Example to understand the same:

Example 1:

using System;

namespace GenericClass
{
    class Program
    {
        static void Main(string[] args)
        {
            MyClass<int> mc = null;
            MyClass<string> mc1 = null;
            try
            {
                mc = new MyClass<int>(10);
                Console.WriteLine(mc.PrintThis());
                mc1 =new MyClass<string>("Hello World.");
                Console.WriteLine(mc1.PrintThis());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                mc = null;
                mc1 = null;
            }
            Console.ReadLine();
        }
    }
    class MyClass<T>
    {
        private T _Value;
        public MyClass(T t)
        {
            _Value = t;
        }
        public string PrintThis()
        {
            return "You Passed : " + _Value;
        }
    }

}

Output will be :

Sunday, September 18, 2011

Deleting Only Duplicate/Repeting Records in SQl Server Table.

Here is a Query which will help you to delete the duplicate record from a table.
Ex:
Table Name : Test
Table Content:
id name
===============
1 Ajit
2 Amit
3 Rana
4 Raj
5 Sonu
5 Sonu
==================
Here the last two records are duplicate, i want to delete the duplicate record only not both record.
===================
So here we go type the following Query.
===================

declare @tbl Table(id int,name varchar(50))
insert into @tbl(id,name)
select distinct * from Test
truncate table Test
insert into Test(id,name)
select id,name from @tbl
select * from test
=======================
Press F5 (Execute this query)
Now you can se that the duplicate record is deleted from the table.
==================
Description:
===========
Here I am creating one temporary table called @tbl and in that I am storing the distinct content of table called Test.
after that i am truncating Test table and then again inserting the records from Temporary table called @tbl.
==================
Enjoy.......
Ask me for more.......

Wednesday, March 10, 2010

Getting IP Address of PC in ASP.NET

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Below is the method which will return the IP address of the system as a string.
" strHostName " is a variable as you can see below which contains the Host name (PC name) that also you can get.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public string GetNetInfo()
{
try
{
string retVal = string.Empty;
string strHostName = System.Net.Dns.GetHostName();
string strIp = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
return strIp;
}
catch (Exception ex)
{
throw ex;
}
}
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Enjoy......

Tuesday, February 23, 2010

Reading Contents of a File into a String Variable

We have a file (txt,doc,xml,xslt etc tested) and we wanted to read the content of that file into a string variable.

To do this I have following code section:
=-=-=-=-=-=-=-=-=-=-=-=-=-=
using System.IO;

public string GetContentsOfFile(string URL)
{
if(string.Equals(URL,string.Empty))
throw new ArgumentNullException("URL not given");
StreamReader sr = new StreamReader(URL);
string retStr = string.Empty;
retStr = sr.ReadToEnd();
return retStr;
}
=-=-=-=-=-=-=-=-=-=-=-=-=-=
This method will take URL of file as parameter and return a string with the contents of that file.

Enjoy...

Adding New Node to an Existing XML file

We have an XML file named: TestXML.xml

Structure is as follow:




We wanted to add a new node.

This code block will do the same

public void AddXMLNode(string XMLFilePath,string TitleToAdd, string ArtistToAdd)
{
XmlDocument doc = new XmlDocument();
doc.Load(XMLFilePath);
XmlNode node = doc.CreateNode(XmlNodeType.Element, "CD", null);
XmlNode TitleNode = doc.CreateElement("title");
TitleNode.InnerText = TitleToAdd;
XmlNode ArtistNode = doc.CreateElement("artist");
ArtistNode.InnerText = ArtistToAdd;
node.AppendChild(TitleNode);
node.AppendChild(ArtistNode);
XmlNodeList list = doc.GetElementsByTagName("Catalog");
list[0].AppendChild(node);
doc.Save(XMLFilePath);
}

Now Test This Code:

AddXMLNode(TestXML.xml,”Deewana”,”Sonu Nigam”);

Now the Structure Will Look Like:



Enjoy...

Friday, February 19, 2010

Understanding WITH TIES Option in SQL Server

Table Structure:
=-=-=-=-=-=-=-=-
Book_ID Book_Name Book_Price
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
B001 ABC $10.50
B002 AAA $12.50
B003 BBC $10.50
B004 KCC $16.50
B005 MCC $10.50
B006 DCC $17.50
B007 BBA $13.50
B008 MMC $10.50
B009 BOOK $07.10
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

if you use TOP clause with the SELECT statement it will only gives you the N rows even if there is any record of same value available.

Consider this example:

select top 5 Book_Name,Book_Price from Book order by Book_Price desc

Resule of this query is will be:

Book_Name Book_Price
=-=-=-=-=-=-=-=-=-=-=-=-=-
DCC $17.50
KCC $16.50
BBA $13.50
AAA $12.50
MCC $10.50
=================
But there is also some books with the same price ($10.50). So those records will be ignored by TOP Clause.

But If we think as book seller's mind they also wanted to list the name of those books which comes in that top 5 price range because they have to sell those boks also because those are also coming in TOP 5 price range.

So, to solve this issue we will use WITH TIES Clause.

Consider this Example:

select top 5 with ties Book_Name,Book_Price from Book order by Book_Price desc

Result of this query will be:

Book_Name Book_Price
=-=-=-=-=-=-=-=-=-=-=-=-=-
DCC $17.50
KCC $16.50
BBA $13.50
AAA $12.50
BBC $10.50
MCC $10.50
MMC $10.50
ABC $10.50
=======================

Now we are getting the expected output; here we can see all the book name of top 5 price range.

===============================
Enjoy...   

Wednesday, February 10, 2010

Calling Controller Page Methods using JavaScript in MVC

First Check whether the Browser supports the Ajax or Not.
================

function GetXmlHttpObject() {
var xmlHttp = null;
try {
// Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
}
catch (e) {
// Internet Explorer
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
====
Then Write The following Code.
=============
function ConfirmMsg() {
if (confirm("put the confirmation message, clicking cancel of this will not cause a post back")) {
xmlHttp = GetXmlHttpObject();
if (xmlHttp == null) {
alert("Your browser does not support AJAX!");
return;
}
var url = "/Folder or ControllerName/MethodName";
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState==4){
document.getElementById('objDiv').value = xmlHttp.responseText;
}
}
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
return false;
}
}
=====
Enjoy...