Pages

Friday, 27 September 2013

Programmatically Upload a File to FTP Server in .Net

The following code snippet could be used to upload files to an FTP site. It uses the 'FtpWebRequest' and 'FtpWebResponse' classes from the System.Net namespace.

Using System.Net:
//Also include any other required namespaces here.

FtpWebRequest ftpRequest;
FtpWebResponse ftpResponse;
 
try
{
    //Settings required to establish a connection with the server
    this.ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://ServerIP/FileName"));
    this.ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
    this.ftpRequest.Proxy = null;
    this.ftpRequest.UseBinary = true;
    this.ftpRequest.Credentials = new NetworkCredential("UserName", "Password");
 
    //Selection of file to be uploaded
    FileInfo ff = new FileInfo("File Local Path With File Name"); //e.g.: c:\\Test.txt
    byte[] fileContents = new byte[ff.Length];
 
    //will destroy the object immediately after being used
    using (FileStream fr = ff.OpenRead())
    {
        fr.Read(fileContents, 0, Convert.ToInt32(ff.Length));
    }
 
    using (Stream writer = ftpRequest.GetRequestStream())
    {
        writer.Write(fileContents, 0, fileContents.Length);
    }
    //Gets the FtpWebResponse of the uploading operation
    this.ftpResponse = (FtpWebResponse)this.ftpRequest.GetResponse();
    Response.Write(this.ftpResponse.StatusDescription); //Display response
}
catch (WebException webex)
{
    this.Message = webex.ToString();
}

Friday, 12 July 2013

How to create web service and how to use web service

Introduction:

Here I will explain what webservice is, uses of webservice and how to create webservice and how to consume webservice in asp.net.

Description:
Today I am writing article to explain about webservices. First we will see what is webservice is and uses of webservice and then we will see how to use webservice in our applications.

What is Web Service?

Web Service is an application that is designed to interact directly with other applications over the internet. In simple sense, Web Services are means for interacting with objects over the Internet. The Web serivce consumers are able to invoke method calls on remote objects by using SOAP and HTTP over the Web. WebService is language independent and Web Services communicate by using standard web protocols and data formats, such as
  • HTTP
  • XML
  • SOAP
Advantages of Web Service

Web Service messages are formatted as XML, a standard way for communication between two incompatible system. And this message is sent via HTTP, so that they can reach to any machine on the internet without being blocked by firewall.

Examples for Web Service

Weather Reporting: You can use Weather Reporting web service to display weather information in your personal website.

Stock Quote: You can display latest update of Share market with Stock Quote on your web site.

News Headline: You can display latest news update by using News Headline Web Service in your website.

In summary you can any use any web service which is available to use. You can make your own web service and let others use it. Example you can make Free SMS Sending Service with footer with your advertisement, so whosoever use this service indirectly advertise your company... You can apply your ideas in N no. of ways to take advantage of it.

Frequently used word with web services

What is SOAP?

SOAP (simple object access protocol) is a remote function calls that invokes method and execute them on Remote machine and translate the object communication into XML format. In short, SOAP are way by which method calls are translate into XML format and sent via HTTP.

What is WSDL? 

WSDL stands for Web Service Description Language, a standard by which a web service can tell clients what messages it accepts and which results it will return.
WSDL contains every detail regarding using web service and Method and Properties provided by web service and URLs from which those methods can be accessed and Data Types used.

What is UDDI?

UDDI allows you to find web services by connecting to a directory.


So lets start create web service.

Step 1: create a new website in asp.net .

Step 2:create two text box like-
          textbox 1
          textbox 2
         and insert a submit button .
Step 3:for show addition of both insert a label in asp.net.
          
Step 4:now in root of asp.net website  add webservice by right click in root of asp.net.
  
Step 5:add webservice page and write code for web service 
          
          [WebMethod]
              public int add(int num1,int num2)
              {
              return(num1+num2);
              }
Step 6:on button click at aspx page write a code-
              protected void Button1_Click(object sender, EventArgs e)
                         {
        WebService ws = new WebService();
       Label1.Text= ws.add(Convert.ToInt32(TextBox1.Text),Convert.ToInt32(TextBox2.Text)).ToString();
                           }    
          
Step 7:run the code and insert value in both text box and click submit button and result will                 appear at label.   



for any query please write comment -


With Regards                                                                     
Avinash Sharma          





Friday, 28 June 2013

Image Rotator in asp.net with example

Html code-


<head>
    <title></title>
    <script type="text/javascript" language="javascript">
        window.onload = function () {
            var rotator = document.getElementById("rotator");
            var images = rotator.getElementsByTagName("img");
            for (var i = 0; i < images.length; i++) {
                images[i].style.display = "none";
            }
            images[0].style.display = "block";
            var counter = 0;
            setInterval(function () {
                for (var i = 0; i < images.length; i++) {
                    images[i].style.display = "none";
                }
                images[counter].style.display = "block";
                counter++;
                if (counter == images.length) {
                    counter = 0;
                }
            }, 1000);
        };
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div id="rotator">
      <img src="http://i43.tinypic.com/2di1254.jpg" style = "height:272px; width:333px" /> <img src="http://i39.tinypic.com/343op3p.jpg" style = "height:272px; width:333px" /> <img src="http://i41.tinypic.com/jr9zd1.jpg" style = "height:275px; width:333px" />
    </div>
    </form>
</body>
</html>