0
Posted on 2:46 PM by prajeesh and filed under ,
Suppose you have a web service that returns a value in XML format and you want to show the returned value in a webpage or a widget.
For example :

<?xml version="1.0" encoding="utf-8" ?>
<int xmlns="http://tempuri.org/">1</int>

You can use following code snippet to call a web service from javascript:
<script language="text/javascript">
var returned_data;
var request = new XMLHttpRequest();
//function to initialize web service
function initializeWebservice()
{
var url =
http://Localhost/Testwebservice.asmx/GetLeadCounts; //Web service url
request.onreadystatechange = webStatusProc;
request.open( "GET", url, true );
request.send();
}
//function to process webservice response
function webStatusProc()
{
if (request.readyState == 4) // Request completed?
{
response = request.responseXML.toXML();
XML = XMLDOM.parse( response);
returned_data= XML.evaluate('string(/int)');
alert(returned-data);
//do anything with returned data
}
}
</script>



Shout it kick it on DotNetKicks.com
0
Posted on 4:25 AM by prajeesh and filed under ,
If you find "The test form is only available for requests from the local machine" message in your webservice page after hosted in your server, dont worry just add following tags in your web.config file just before </system.web>
tag
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices> Shout it kick it on DotNetKicks.com
0
Posted on 12:22 AM by prajeesh and filed under ,
In some sites you may have seen visitors counter/Total hits at the bottom of the website, in asp.net it is very easy to implement this feature, here i will show you a simple example that stores visitors count in a Text file.
First you have to add a Global.asax file into the solution you are working, you can do this by Right clicking your project name in the solution explorer ->Add New Item -> Global Application Class.
create a Hits.txt file in the root folder of your application and just add following code in the Session_Start event in the Global.asax file.

void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
Application.Lock();
if (System.IO.File.Exists(Server.MapPath(Request.ApplicationPath + "/Hits.txt")))
{
System.IO.StreamReader sr = new System.IO.StreamReader(Server.MapPath(Request.ApplicationPath + "/Hits.txt"));
string count = sr.ReadToEnd();
sr.Dispose();
Application["Hits"] = int.Parse(count) + 1;
System.IO.StreamWriter sw = new System.IO.StreamWriter(Server.MapPath(Request.ApplicationPath + "/Hits.txt"));
sw.Write(Application["Hits"].ToString());
sw.Dispose();
}
//Application["Hits"] = int.Parse(Application["Hits"].ToString()) + 1;
Application["OnlineUsers"] = (int)Application["OnlineUsers"] + 1;
Application.UnLock();
}

Reading Data from Hits.text file:
if (System.IO.File.Exists(Server.MapPath(Request.ApplicationPath + "/TestFile.txt"))){
{
System.IO.StreamReader sr = new System.IO.StreamReader(Server.MapPath(Request.ApplicationPath + "/Hits.txt"));
string strdata = sr.ReadToEnd();
sr.Dispose();
}
Here total hits will be available in "strdata" variable.
we have to create a StreamReader object to read data from a text file, follow this MSDN article to know more about StreamReader class. Shout it kick it on DotNetKicks.com
0
Posted on 11:22 PM by prajeesh and filed under ,
It is a good idea to create log files to identify exceptions occured in your webapplication in a live environment.
In ASP.net we can achieve this by creating a Text file in the application and write exceptions into the text file with time of exception, here is the sample code:
First you have to create a Log.txt file in the root folder of your application
try
{
//lines of code that may generate exceptions.
}
catch(Exception ex)
{
if (System.IO.File.Exists(HttpContext.Current.Request.ApplicationPath + "/Log.txt"));
{
System.IO.StreamWriter Sr = new System.IO.StreamWriter(HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath+"/Log.txt"),true);
Sr.WriteLine(
DateTime.Now.ToString()+":"+ex.ToString());
}
}

so all exceptions generated within the try block will be recorded into Log.txt file.


Shout it kick it on DotNetKicks.com
0
Posted on 10:12 PM by prajeesh and filed under ,
File manipulation is a very easy task in asp.net, i this article i will show you a simple example that writes and and read data from a text file.
suppose we have a Text file named "TestFile.txt" in the root folder of our web application.
Writing Data to a text file :
if (System.IO.File.Exists(Server.MapPath(Request.ApplicationPath + "/TestFile.txt")))
{
System.IO.StreamWriter sw = new System.IO.StreamWriter(Server.MapPath(Request.ApplicationPath + "/TestFile.txt"));
sw.Write("Hello world");
sw.Dispose();
}

if you want to Append data in the Text file then you have to set append parameter true in StramWriter object constructor.
Eg:-
System.IO.StreamWriter sw = new System.IO.StreamWriter(Server.MapPath(Request.ApplicationPath + "/TestFile.txt"),true);
we need to create a streamwriter object to write lines of data into a text file, follow this MSDN article to understand more about StreamWriter class.

Reading Data from a Text file:
if (System.IO.File.Exists(Server.MapPath(Request.ApplicationPath + "/TestFile.txt")))
{
System.IO.StreamReader sr = new System.IO.StreamReader(Server.MapPath(Request.ApplicationPath + "/TestFile.txt"));
string strdata = sr.ReadToEnd();
sr.Dispose();

}
we have to create a StreamReader object to read data from a text file, follow this MSDN article to know more about StreamReader class.
Data will be available in strdata variable.

Shout it kick it on DotNetKicks.com