Developers Archive for April, 2007

File Upload Using FtpWebRequest

File Upload Using FtpWebRequest Wednesday, April 25th, 2007

namespaces to be added:
using System.Net
using System.IO

1.Create an FtpWebRequest object over an ftp server Uri
2.Set the ftp method to execute (upload)
3.Set options(ssl support, transfer as binary/not etc.) for the ftp webrequest.
4.Set the login credentials(username, password)
5.Execute the request.
6.Close the FTP Request, and the file streams.

In the following code the file to upload, the uri and the network credentials are provided as an example.
        protected void Submit1_ServerClick(object sender, EventArgs e)
       {
        string filename = “C:\\test.txt”;
        Upload(filename);
       }
      
        private void Upload(string filename)
        {
            FileInfo fileInf = new FileInfo(filename);
            FtpWebRequest reqFTP;
   
            // Create FtpWebRequest object from the Uri provided
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(”ftp://ftp.xyz.com/” + fileInf.Name));
      
            // Provide the WebPermission Credintials
            reqFTP.Credentials = new NetworkCredential(”user1″, “pass”);
           
            //use proxy
            reqFTP.Proxy = null;
   
            // By default KeepAlive is true, where the control connection is
            // not closed after a command is executed.
            reqFTP.KeepAlive = false;

            // Specify the command to be executed.
            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
   
            // Specify the data transfer type.
            reqFTP.UseBinary = true;

            // Notify the server about the size of the uploaded file
            reqFTP.ContentLength = fileInf.Length;

            // The buffer size is set to 2kb
            int buffLength = 2048;
            byte[] buff = new byte[buffLength];
            int contentLen;
   
            // Opens a file stream (System.IO.FileStream) to read
            //the file to be uploaded
            FileStream fs = fileInf.OpenRead();
  
            try
            {
            // Stream to which the file to be upload is written
            Stream strm = reqFTP.GetRequestStream();
       
            // Read from the file stream 2kb at a time
            contentLen = fs.Read(buff, 0, buffLength);
       
            // Till Stream content ends
            while (contentLen != 0)
            {
            // Write Content from the file stream to the
            // FTP Upload Stream
            strm.Write(buff, 0, contentLen);
            contentLen = fs.Read(buff, 0, buffLength);
            }
       
            // Close the file stream and the Request Stream
            strm.Close();
            fs.Close();
           
            }
            catch(Exception ex)
            {
               MessageBox.Show(ex.Message, “Upload Error”);     
               return;
            }
        }

Disabling Right Click in JavaScript

Disabling Right Click in JavaScript Wednesday, April 25th, 2007

Disabling Right Click in JavaScript
————————————
 If you have quality content on your webpage and want to prevent people stealing your resources, you can add this script to disable the right-mouse click.

The Javascript function that is listed below.

<html>
<head>
<script>
var isNS = (navigator.appName == “Netscape”) ? 1 : 0;
if(navigator.appName == “Netscape”)

document.captureEvents(Event.MOUSEDOWN||Event.MOUSEUP);
}
  function mischandler(){
   return false;
 }
  function mousehandler(e){

  var myevent = (isNS) ? e : event;
  var eventbutton = (isNS) ? myevent.which : myevent.button;
    if((eventbutton==2)||(eventbutton==3)) return false;
 }
  document.oncontextmenu = mischandler;
  document.onmousedown = mousehandler;
  document.onmouseup = mousehandler;
  </script>
</head>
<body>
<img src=’sample.jpg’  alt='’>
</body>
</html>

The Above script works on Internet Explorer, Firefox etc.,

 

Functions in Postgres SQL vs Functions in MySQL

Functions in Postgres SQL  vs Functions in MySQL Wednesday, April 25th, 2007

Postgres SQL - Functions:
In SQL Server and MySQL use a proprietary extension of SQL to provide stored procedure functionality (amongst other features). Microsoft calls it Transact SQL, or “T-SQL”, and as far as I know, MySQL just calls it “MySQL”. Both variants are different enough to occasionally trip up a developer but are written around the common SQL-92/2003 standards. The net result is that the syntax used inside stored procedures resembles SQL code used without sprocs; the same “SELECTs”, “INSERTs”, etc. but with a few extra commands added for control flow and the like. Postgres stored procedures can also be written with a SQL-like language called “PL/pgSQL”.
But, as an added twist, a developer could instead chose to write it in Perl, Python, or Tcl using PL/Perl, PL/Python, or PL/Tcl. Perl/Python/Tcl hackers may rejoice at this thought and it does open up a great many possibilities.

CREATE FUNCTION Orders_details(order_id int) RETURNS int AS $$
DECLARE
qty int;
BEGIN
SELECT COUNT(*) INTO ord
FROM Orders
WHERE oid= order_id;
RETURN ord;
END;
$$ LANGUAGE plpgsql;

The mentionable differences (besides the self-explanatory LANGUAGE command):
Microsoft SQL Server has distinct roles for both user-defined functions (UDFs) and stored procedures. A UDF is typically used within the context of a query, while a sproc takes input and/or output from/to an application. MySQL also has both procedures and functions, but they are defined almost identically and work almost identically. Postgres dispenses with the difference and everything is simply defined as a “function”. As with MySQL, we also see the double dollar-sign (”$$”). However, here we’re using it for a different kind of work-around. With Postgres, the function definition is just one long string and if single quotes are contained somewhere within it,
they’d have to be escaped using a pair of single quotes: ”. If there were already escaped quotes within the function, they’d then need to be escaped again, giving us a total of four quotes.
To make life easier (and our code more legible), we can surround the definition with double dollar signs which negates the need for escaping.

Calling the function is also a bit different:

SELECT Orders_details(17);

By the same way we can create trigger procedures in postgres sql by return as trigger.
In this we call this function, each time that particular trigger is executing.
Like mysql, we can create the triggers on before/after insert or delete or update a table.
we can create trigger as follows:

CREATE TRIGGER emp_stamp BEFORE INSERT OR UPDATE ON emp
FOR EACH ROW EXECUTE PROCEDURE emp_check();

And function is as follows:
CREATE FUNCTION emp_check() RETURNS trigger AS $emp_stamp$
BEGIN
IF NEW.empname IS NULL THEN
RAISE EXCEPTION ‘empname cannot be null’;
END IF;
END;
RETURN NEW;
$emp_stamp$ LANGUAGE plpgsql;


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.