Keep in touch

you can keep in touch with my all blogs and sites by install this Toolbar...
http://rworldtoolbar.ourtoolbar.com/

Tuesday, February 3, 2009

How to: Send Data Using the WebRequest Class

For C#,
=======
using System;
using System.IO;
using System.Net;
using System.Text;

namespace Examples.System.Net
{
public class WebRequestPostExample
{
public static void Main ()
{
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create ("http://www.rushiraval.co.cc);
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = "This is a test that posts this string to a Web server.";
byte[] byteArray = Encoding.UTF8.GetBytes (postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream ();
// Write the data to the request stream.
dataStream.Write (byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close ();
// Get the response.
WebResponse response = request.GetResponse ();
// Display the status.
Console.WriteLine (((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream ();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd ();
// Display the content.
Console.WriteLine (responseFromServer);
// Clean up the streams.
reader.Close ();
dataStream.Close ();
response.Close ();
}
}

------------------------------------------------------------------------------------

For VB,
==========
Imports System
Imports System.IO
Imports System.Net
Imports System.Text
Namespace Examples.System.Net
Public Class WebRequestPostExample

Public Shared Sub Main()
' Create a request using a URL that can receive a post.
Dim request As WebRequest = WebRequest.Create("http://www.rushiraval.co.cc ")
' Set the Method property of the request to POST.
request.Method = "POST"
' Create POST data and convert it to a byte array.
Dim postData As String = "This is a test that posts this string to a Web server."
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
' Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded"
' Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length
' Get the request stream.
Dim dataStream As Stream = request.GetRequestStream()
' Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length)
' Close the Stream object.
dataStream.Close()
' Get the response.
Dim response As WebResponse = request.GetResponse()
' Display the status.
Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
' Get the stream containing content returned by the server.
dataStream = response.GetResponseStream()
' Open the stream using a StreamReader for easy access.
Dim reader As New StreamReader(dataStream)
' Read the content.
Dim responseFromServer As String = reader.ReadToEnd()
' Display the content.
Console.WriteLine(responseFromServer)
' Clean up the streams.
reader.Close()
dataStream.Close()
response.Close()

End Sub
End Class
End Namespace

}

for detailed, read more

Thursday, October 23, 2008

Javascript validation for input limited text into textbox or textarea in ASP.NET

Javascript validation for input limited text into textbox or textarea
On design page source::
----------------------------



On b/h code source page:
protected void Page_Load(object sender, EventArgs e)
{
txtLocation.Attributes.Add("onkeypress", "return LocationLength()");
txtLocation.Attributes.Add("onkeyup", "return LocationLength()");
}

Happy Coding..!!

Saturday, October 18, 2008

Get And Set The System Date And Time using c#

Have you want to get your current system date and time as well as set the system date and time using c# code.
here it is, C# code snippet that uses unmanaged code to retrieve the current date and time of the Windows operating system, and also sets it to the specified values.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Windows.Forms;
  6. using System.Runtime.InteropServices;
  7. namespace Sample
  8. {
  9. public partial class Form1 : Form
  10. {
  11. public Form1()
  12. {
  13. InitializeComponent();
  14. }
  15. public struct SystemTime
  16. {
  17. public ushort Year;
  18. public ushort Month;
  19. public ushort DayOfWeek;
  20. public ushort Day;
  21. public ushort Hour;
  22. public ushort Minute;
  23. public ushort Second;
  24. public ushort Millisecond;
  25. };
  26. [DllImport("kernel32.dll", EntryPoint = "GetSystemTime", SetLastError = true)]
  27. public extern static void Win32GetSystemTime(ref SystemTime sysTime);
  28. [DllImport("kernel32.dll", EntryPoint = "SetSystemTime", SetLastError = true)]
  29. public extern static bool Win32SetSystemTime(ref SystemTime sysTime);
  30. private void button1_Click(object sender, EventArgs e)
  31. {
  32. // Set system date and time
  33. SystemTime updatedTime = new SystemTime();
  34. updatedTime.Year = (ushort)2008;
  35. updatedTime.Month = (ushort)4;
  36. updatedTime.Day = (ushort)23;
  37. // UTC time; it will be modified according to the regional settings of the target computer so the actual hour might differ
  38. updatedTime.Hour = (ushort)10;
  39. updatedTime.Minute = (ushort)0;
  40. updatedTime.Second = (ushort)0;
  41. // Call the unmanaged function that sets the new date and time instantly
  42. Win32SetSystemTime(ref updatedTime);
  43. // Retrieve the current system date and time
  44. SystemTime currTime = new SystemTime();
  45. Win32GetSystemTime(ref currTime);
  46. // You can now use the struct to retrieve the date and time
  47. MessageBox.Show("It's " + currTime.Hour + " o'clock. Do you know where your C# code is?");
  48. }
  49. }
  50. }
Happy Coding..!!

Delete All Temporary Internet Files Of Internet Explorer

  1. using System.IO;
  2. public static void Main()
  3. {
  4. ClearFolder(new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache))); // Execute ClearFolder() on the IE's cache folder
  5. }
  6. void ClearFolder(DirectoryInfo diPath)
  7. {
  8. foreach (FileInfo fiCurrFile in diPath.GetFiles())
  9. {
  10. fiCurrFile.Delete();
  11. }
  12. foreach (DirectoryInfo diSubFolder in diPath.GetDirectories())
  13. {
  14. ClearFolder(diSubFolder); // Call recursively for all subfolders
  15. }
  16. }
Happy Coding..!!

Thursday, October 16, 2008

How to get IP address of the Current Machine?

public void UseDNS()
{
string hostName = Dns.GetHostName();
Console.WriteLine("Host Name = " + hostName);
IPHostEntry local = Dns.GetHostByName(hostName);
foreach(IPAddress ipaddress in local.AddressList)
{
Console.WriteLine("IPAddress = " + ipaddress.ToString());
}
}

Saturday, October 11, 2008

Resolving w3c validation issues details...

For those who are wondering why should one bother with W3C Validations,
please read Why should we Validate our WebSites?

When validating your website using W3C some weird errors might occur

An Example is


there is no attribute "border".


even if you haven't given the border attribute

The Reason


The ASP.NET engines sees the W3C validator as down-level browser and renders
non-XHTML compliant code. Your code is most likely fine. The problem is with
ASP.NET.



The Solution(Step Wise)


1.Right Click on your Solution Explorer
2.Click on Add ASP.NET Folder ---> App_Browsers
3.Now Click on App_Browsers ---> Add New Item
4.A dialog Box now pops up with some Visual Studio Installed Templates.
Select the Browser File Template from there, change the name as W3CValidations.browser(any other convenient name also) and Click on the Add Button
5.Delete the whole XML MarkUp code inside the W3CValidations.browser
6.Place the following code instead




























7.Now upload this Folder nad File to your Hosting Service
8.Re Validate using W3C Validator
9.Bingo! You got a Clean Validation Certificate.
10. Show off the Validation Certificate to all those who cares [:)]


Happy coding..!!

ref:
http://fun2code.blogspot.com/search?updated-max=2008-08-28T15%3A02%3A00%2B05%3A30&max-results=3

Saturday, October 4, 2008

use Messagebox in asp.net

Declaration:
using System.Windows.Forms;

Code:
MessageBox.Show("message right here!!");