Friday, August 24, 2007

Get the last modified date of the url using HttpWebRequest and HttpWebResponse

Get the last modified date of the url using HttpWebRequest and HttpWebResponse


private void getLastModifiedDateOfUrl(){
HttpWebRequest req = (HttpWebRequest) WebRequest.Create("
http://computerbapu.blogspot.com");
HttpWebResponse resp = (HttpWebResponse) req.GetResponse();
Response.Write ("Last modified: " + resp.LastModified);
resp.Close();}

//output: Last modified: 24/08/2007 04:29:23

Use of Uri class in asp.net 2.0.

Use of Uri class in asp.net 2.0.
=======================

//namespaceusing System.Net;

private void showUrlParts()
{
Uri sample = new Uri("http://yourpath/bass_aei/login.aspx?param=22");
Response.Write("Host: " + sample.Host + "<br/>");
Response.Write("Port: " + sample.Port + "<br/>");
Response.Write("Scheme: " + sample.Scheme + "<br/>");
Response.Write("Local Path: " + sample.LocalPath + "<br/>");
Response.Write("Query: " + sample.Query + "<br/>");
Response.Write("Path and query: " + sample.PathAndQuery + "<br/>");
}


//Host: localhost
//Port: 80
//Scheme: http
//Local Path: /yourpath/login.aspx
//Query: ?param=22
//Path and query: /yourpath/login.aspx?param=22

Download a resource using WebClient in C#.NET

Download a resource using WebClient in C#.NET

using System.net;
//download a resource from the internet.
private void downloadResource() {

WebClient wc= new WebClient();
string uri = "http://computerbapu.blogspot.com";
string fname = "myResource.txt";
try {
//this function does the work
wc.DownloadFile(uri, fname);
}
catch (WebException exc)
{
Console.WriteLine(exc);
}
catch (UriFormatException exc)
{
Console.WriteLine(exc); }
Console.WriteLine("Download is completed.");
}

Wednesday, August 22, 2007

google map javascript

Javascript to show a google map
==========================


<html>
<body>
<script src="http://maps.google.com/maps?file=api&v=1&key=yourKey"type="text/javascript"></script>
<div id="mapDiv" style="width: 700px; height: 600px"></div>
<script type="text/javascript">
//<![CDATA[ var myMap = new GMap(document.getElementById("mapDiv"));
myMap.addControl(new GSmallMapControl());
myMap.addControl(new GMapTypeControl());
myMap.centerAndZoom(new GPoint(-112.1419, 40.4419), 4);
//]]>
</script>
</body>
</html>

Simple XPath query with dot net

Simple XPath query with dot net
==========================


XML Path Language (XPath) is the capability to query and locate the tree's content in a xml file.
In dot net XPath evaluation is exposed through the XPathNavigator abstract class. The navigator is an XPath processor that works on top of any XML data source that exposes the IXPathNavigable interface.The most important member of this interface is the CreateNavigator method, which returns an XPathNavigator object.
Today we will try out some xpath usin dot net
Format of users.xml



<?xml version="1.0" encoding="utf-8" ?> <users> <user> <name rollno="1">Bobby</name> <password>pass1</password> <role>Manager</role> </user> <user> <name rollno="2">Ravi</name> <password>pass2</password> <role>Data Entry Operator</role> </user></users>


/// using xpath to retrieve names
private void showXMLName() {

XPathDocument xpd = new XPathDocument("c:\\users.xml");
XPathNavigator xpn = xpd.CreateNavigator();
XPathNodeIterator xpi = xpn.Select("//users/user/name");

while (xpi.MoveNext()) {

MessageBox.Show(xpi.Current.Name + ":" + xpi.Current.Value);
}
}



// using normal xml
private void showXMLName() {

string xmlFile = "c:\\users.xml";
XmlDocument doc = new XmlDocument();
doc.Load(xmlFile);
XmlNodeList nodes = doc.GetElementsByTagName("name");

foreach (XmlNode node in nodes) {
MessageBox.Show(node.ChildNodes[0].Value);
}
}


//you might want to go for attributes and other details alltogether
private void showXMLDetails()
{

string xmlFile = "c:\\users.xml";

XmlDocument doc = new XmlDocument();
doc.Load(xmlFile);
// Retrieve the title of every science-fiction movie.
XmlNodeList nodes = doc.SelectNodes("//users/user");

foreach (XmlNode node in nodes)

{
//MessageBox.Show(node.ChildNodes[0].Attributes.Count.ToString());

//MessageBox.Show(node.ChildNodes[0].ChildNodes[0].Value);
//MessageBox.Show(node.ChildNodes[0].LastChild.Value);
//get roll no - check the attribute at name node

MessageBox.Show(node.ChildNodes[0].Attributes["rollno"].Value );
// show name - in case of windows.net
MessageBox.Show(node.ChildNodes[0].FirstChild.Value ); //show password
MessageBox.Show(node.ChildNodes[1].FirstChild.Value); //show roles
MessageBox.Show(node.ChildNodes[2].FirstChild.Value);
}

}


//you may specify filters in xpath query.. starts-with, contains, position etc...
//get all records which starts with a particular letter

XPathNodeIterator xpi = xpn.Select("//users/user[starts-with(role,'S')]/name");
//get all records where user role is Sales

XPathNodeIterator xpi = xpn.Select("//users/user[role='Sales']/name");
//get all records which contains the string les

XPathNodeIterator xpi = xpn.Select("//users/user[contains(role,'les')]/name");
//get the record at first position.

XPathNodeIterator xpi = xpn.Select("//users/user[position()=1]/name");
//get all records where role = sales and name is bob.. you may specify and condition XPathNodeIterator xpi = xpn.Select("//users/user[role='Sales' or name='Bob']/name");

//get all records where substring for the node role is S...this demonstrate the use of substring

XPathNodeIterator xpi = xpn.Select("//users/user[substring(role,1,1)='S']/name");

Tuesday, August 21, 2007

CREATE TABLE FROM ANOTHER TABLE IN MS SQL SERVER USING QUERY

CREATE TABLE FROM ANOTHER TABLE IN MS SQL SERVER USING QUERY

select * into newtable from oldtable

you will notice the newtable created... you may try out with various combination of joins etc.

The above is valid in ms sql server

RETRIEVE FILES FROM DIRECTORY IN C#

RETRIEVE FILES FROM DIRECTORY IN C#


//string yourdirectory=Server.MapPath("yourfoldername");
string yourdirectory="C://";

string []dir=Directory.GetFiles(yourdirectory);

for (int i = 0; i < dir.Length; i++)
{

//get individual file names
string fileName = dir[i].Substring(dir[i].ToString().LastIndexOf("\\") +1);
string entirePath = dir[i].toString();
}