Tag Archive for Net

.NET Connection String

<connectionStrings>
<clear/>
<add name="NorthwindConnectionString"
connectionString="Data Source=SERVERNAME;Initial Catalog=Northwind;Persist Security Info=True;User ID=username;Password=password"
providerName="System.Data.SqlClient"/>
</connectionStrings>

source

URLRewriter.NET Regex

<rewriter>

<!-- EXCEPT FOR PREDEFINED DIRECTORIES -->

<rewrite url="~/(.+)/((js|img|css|op|mtn)/(.+))" to="~/$2" processing="stop"></rewrite>

<!-- EXCEPT FOR COMMONLY IMPORTED FILES -->

<rewrite url="^(/.+(.gif|.png|.jpg|.ico|.pdf|.css|.swf|.zip|.js)(?.+)?)$" to="$1" processing="stop" />

<!-- ALL URL WILL BE REWRITTEN  -->

<rewrite url="~/(.+)/(.+).aspx(?(.+))?$" to="~/$2.aspx?site=$1&amp;$4" processing="stop"></rewrite>

</rewriter>

source

XmlDocument fluent interface

XmlDocument fluent interface

source

Playing With Extension Methods

using System;

namespace ExtensionMethodTest
{
static class Program
{

public delegate void MyDelegate();

static void Main(string[] args)
{

5.Times(delegate()
{
Console.WriteLine("Hello World");
});

Console.Read();
}

public static void Times(this int count, MyDelegate del)
{
for (int i = 0; i < count; i++)
{
del();
}
}

}
}

source

SendMail

// Make mail easy to send.
public static string SendMail(string from, string to, string subject, string body, string smtp)
{
// Create new MailMessage Object
MailMessage message = new MailMessage(from, to, subject, body);

//message.Bcc.Add("someone@tobcc.com");

message.IsBodyHtml = true;

// Create new SmtpClient Object
SmtpClient mailClient = new SmtpClient(smtp, 25);

try
{
// Send eMail and return 1 if successful
// otherwise return error message
mailClient.Send(message);
return "1";
}
catch (System.Net.Mail.SmtpException e)
{
// create or append errors to a log file
CreateLogFiles.CreateLogFiles Err = new CreateLogFiles.CreateLogFiles();
Err.ErrorLog(System.Web.HttpContext.Current.Server.MapPath("/logs/ErrorLog.log"), e.InnerException.Message);
return e.InnerException.Message;
}
}

source

ASP .NET 301 Redirect

<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://www.domain.com");
}
</script>

source

Python – getFile Internet

package get.file.example;

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

public class GetFileExample
{
public GetFileExample()
{
try
{
byte[] bite = new byte[2048];
int read;
URL url = new URL("http://switch.dl.sourceforge.net/sourceforge/pys60miniapps/FlickrS60_src_v0.1b.tar.bz2");

BufferedInputStream bis = new BufferedInputStream(url.openStream());
FileOutputStream fos = new FileOutputStream("/tmp/file.tar.bz2");

while((read = bis.read(bite))>0)
{
fos.write(bite, 0, read);
System.out.println("--");
}

fos.close();
bis.close();

System.out.println("FINE");

}
catch(MalformedURLException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}

public static void main(String[] args)
{
new GetFileExample();
}
}

source

nethttp post

require 'net/http'

url = URI.parse('http://api.search.yahoo.com/ContentAnalysisService/V1/termExtraction')
appid = 'YahooDemo'

context = 'Italian sculptors and painters of the renaissance favored
the Virgin Mary for inspiration'
query = 'madonna'

post_args = {
'appid' => appid,
'context' => context,
'query' => query
}

resp, data = Net::HTTP.post_form(url, post_args)

source

Oracle Stored Proc to Return a Recordset (.Net Side)

Oracle.DataAccess.Client.OracleConnection OraConn = new Oracle.DataAccess.Client.OracleConnection();
OraConn.ConnectionString = "Data Source=DBName;User Id=MyUser;Password=MyPW;";
OraConn.Open();
Oracle.DataAccess.Client.OracleCommand OraComm = new Oracle.DataAccess.Client.OracleCommand("RobCursorTestProc", OraConn);
OraComm.CommandType = CommandType.StoredProcedure;
OraComm.Parameters.Add("Cursor", Oracle.DataAccess.Client.OracleDbType.RefCursor, ParameterDirection.Output);
Oracle.DataAccess.Client.OracleDataReader OraDR = OraComm.ExecuteReader();
if (OraDR.Read())
{
MessageBox.Show(OraDR.GetName(0));
MessageBox.Show(OraDR.GetValue(0).ToString());
}

source

Net::FTP to ftp a file

use Carp;
use Net::FTP;

my $ip  = ...; # Target machine

my $local= ...; # local filename
croak "$local file is missing !!
" unless -f $local;

my $remote= ...; # Remote filename

my $ftp= Net::FTP->new( $ip
,Port    => 21  # Standard port number
,Timeout => 15  # Timeout in seconds
)
or croak "Failed to connect to $ip.";

my $user= ...;  # login user
my $pw  = ...;  # login password
$ftp->login( $user, $pw )
or croak "Failed to login.";

$ftp->put( $local, $remote)
or croak "Failed to <a href="http://ftp.";" >ftp.";</a>

$ftp->quit();

source