Keep in touch

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

Showing posts with label ASP.NET Tips. Show all posts
Showing posts with label ASP.NET Tips. Show all posts

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 4, 2008

use Messagebox in asp.net

Declaration:
using System.Windows.Forms;

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

Msgbox for ASP.Net

Messsagebox function for ASP.Net.
Call this function from the server side, and a msgbox will appear on the client browser. As a bonus, the msgbox javascript is added after the rest of the form, so the browser displays the html for the page and then the msgbox.


Declaration:
Imports System.Text

Code:
Public Sub UserMsgBox(ByVal sMsg As String)

Dim sb As New StringBuilder()
Dim oFormObject As System.Web.UI.Control

sMsg = sMsg.Replace("'", "\'")
sMsg = sMsg.Replace(Chr(34), "\" & Chr(34))
sMsg = sMsg.Replace(vbCrLf, "\n")
sMsg = ""

sb = New StringBuilder()
sb.Append(sMsg)

For Each oFormObject In Me.Controls
If TypeOf oFormObject Is HtmlForm Then
Exit For
End If
Next

' Add the javascript after the form object so that the
' message doesn't appear on a blank screen.
oFormObject.Controls.AddAt(oFormObject.Controls.Count, New LiteralControl(sb.ToString()))

end sub


Happy coding..!!

Monday, September 29, 2008

Error - GetTypeHashCode() : no suitable method found to override

I found it very useful so I want to it here. I found a need for it while transferring my site from vb.net to C#.

After creating an ASP.NET web form using Microsoft Visual Studio 2005 or Microsoft Visual Studio 2005 Team Suite, I renamed the form from it's default name "Default.aspx" to a more user-friendly name "Order.aspx" within MS VS. After adding more code to the C# code-behind page, I discovered the following line: "public partial class _Default"

Being new to the ASP.NET programming language, I changed the "_Default" to "Order" thinking MS VS had failed to rename items within the code it generates. This caused the following error to display at debug/run time: "GetTypeHashCode() : no suitable method found to override"

There were several other errors displayed as well.

The class names must match between the .aspx and .aspx.cs web pages. Here is what the lines in each file should look like:

In the ASPX source file: %@ Page Language="C#" codefile="FormName.aspx.cs" Inherits="FormName_aspx" %

In the ASPX.CS source file: public partial class FormName_aspx : Page

Once I changed the .ASPX file to match the class name in the .ASPX.CS file, the errors were resolved.

Ref::
http://forums.asp.net/t/1225330.aspx
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/7dbed154-34c6-41e3-b44f-23a594e9ccba/

Saturday, September 27, 2008

GenerateRandomName for images in asp.net 2.0 using c#

public string GenerateRandomName()
{

string allowedChars = "";
allowedChars = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,";
allowedChars += "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,";
allowedChars += "1,2,3,4,5,6,7,8,9,0";
char[] sep ={ ',' };
string[] arr = allowedChars.Split(sep);
string fname = "";
string temp = "";
Random rand = new Random();
for (int i = 0; i < 10; i++)
{
temp = arr[rand.Next(0, arr.Length)];
fname += temp;
}
return fname;
}

just call this like::
string getrandomname = GenerateRandomName;

Happy Coding..!!

Tuesday, August 26, 2008

Tricks with Server.MapPath

The Server.MapPath method can be used to retrieve several interesting properties about the running ASP.NET application, for example:

' the current directory
currDir = Server.MapPath(".")

' the parent directory
parentDir = Server.MapPath("..")

' the application's root directory
rootDir = Server.MapPath("/")

For example, you can store the application's root directory in an Application variable, and use it to dynamically build physical paths to other files and directories.

Happy Coding..!!

Thursday, July 24, 2008

ASP.NET: How To Name A Variable Or Property Using .NET’s “Reserved” Words

Let’s say you want to give your class a boolean property named Error that gets turned on or off if something goes awry in your code. You would think that you can’t do this because the word “Error” is reserved by .NET. However, there is a way. All you have to do is put Square Brackets around the word! For instance:

Public Property [Error]() As Boolean

Do it .! and Enjoying Coding with Reserved Keywords...

Happy Coding..!