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...

Wednesday, February 3, 2010

Creating Hit Counter in ASP.NET WebSite

Create a Website (website1)
Add a Class file and edit like:
==================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

///
/// Summary description for Global
///

public class Global:HttpApplication
{
public static int Users = 0;
public Global()
{
//
// TODO: Add constructor logic here
//
}
}
====================
Add a Global Application Class (Global.asax) file and edit like:
in session_start Event Increment the number of users, means on every session the number of users will be incremented.
====================
void Session_Start(object sender, EventArgs e)
{
Global.Users++;
}
=============================
Then goto your Default.aspx.cs page source and edit like:
=======================
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("No Of Users:"+Global.Users);
}
}

================
Execute this Application First time you will see No of Users: 1
break this application execute next time you will see No of users: 2
and the same will be keep going for next Consecutive page requests.
========
Enjoy...