blog.devbroker.org author="Patrick Rabian" about="c#, sharepoint, biztalk, team system resources" more="news, samples, tips for .NET world's developers !"

A shortcut to build a web proxy

Wednesday, 30 July 2008 18:07 by prabian

For reusing : 

public static WebProxy BuildProxy(string proxyServer, int proxyPort, string login, string password, string commaSeparatedProxyExclusions)
{
    WebProxy proxy = new WebProxy(proxyServer, proxyPort);     if (!String.IsNullOrEmpty(login))
    {
        NetworkCredential proxyCredential = new NetworkCredential(login, password);
        CredentialCache proxyCredentials = new CredentialCache();

        proxyCredentials.Add(proxy.Address,
"Basic", proxyCredential);
        proxyCredentials.Add(proxy.Address,
"Digest", proxyCredential);
        proxy.Credentials = proxyCredentials;
    }

    foreach (string exclude in commaSeparatedProxyExclusions.Split(new char[] { ',' }))
    if (!String.IsNullOrEmpty(exclude)) proxy.BypassArrayList.Add(exclude.Trim());

    proxy.BypassProxyOnLocal =
true;

    return proxy;
}

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:  
Categories:   C# sample code
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

.NET StockTrader Sample Application

Monday, 28 July 2008 15:24 by prabian

Part of the Microsoft resume :
This application is an end-to-end sample application for .NET Enterprise Application Server technologies.

  • .NET StockTrader 2.0 composite Web application and middle tier services.
  • New modes for Advanced Web Service (WS-*) message-level security and interoperability with a variety of non-Microsoft platforms via the SOA architecture.
  • Configuration Service 2.0 with technical guides and samples.
  • Capacity planning tool for running multi-agent benchmarks against the .NET StockTrader services.
  • WSTest 1.5 Web services benchmark.

Download here : http://www.microsoft.com/downloads/details.aspx?FamilyID=9A60F0C0-007E-4D49-B061-FBE26C28302E&displaylang=en

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

How to read an event log

Tuesday, 2 January 2007 17:57 by prabian

Sample console application that read the content of an event log…

——–

using System;
using 
System.Collections.Generic;
using 
System.Text;
using 
System.Diagnostics;

namespace EventLogReader{
  
public class 
Program {
    
static void Main (string
[] args){
      EventLog logger=
new EventLog("Application"
);
      
int maxLineLength=30
;
      
string logFilter="*";     
//*, I, W, E, ?            
      
string 
message;
      
string 
type;
      
int counter=0
;
      DateTime start, end;
      TimeSpan executionTime;
      start=DateTime.Now;
      
foreach (EventLogEntry entry in 
logger.Entries){
        message=entry.Message.Replace(
"\n"" "
);
        
if (message.Length>maxLineLength) message=message.Substring(0, maxLineLength)+"…"
;
        
switch 
(entry.EntryType){
          
case EventLogEntryType.Information: type="I"
;
          
break
;
          
case EventLogEntryType.Warning: type="W"
;
          
break
;
          
case EventLogEntryType.Error: type="E"
;
          
break
;
          
default: type="?"
;
          
break
;
        }
        
if (logFilter=="*" 
|| logFilter==type) {
          Console.WriteLine(
"[{4}>{0}-{1}({2})] {3}", type, entry.Source, entry.InstanceId, message, entry.TimeWritten.ToString("dd/MM/yyyy HH:mm:ss"
));
          Console.Out.Flush();
          counter++;
        }
      }
      end=DateTime.Now;
      executionTime=end-start;
      Console.WriteLine(
"{0} row(s) displayed in {1} ms. Press a key to quit…"
, counter, executionTime.TotalMilliseconds);
      Console.Read();
    }

  }

}

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   ,
Categories:   C# sample code
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Export a DataTable to CSV format (Excel compatible)

Monday, 18 December 2006 14:41 by prabian

You can use ExportDataTable method to export a generic datatable content to a CSV format.
The following sample is used in ASP.NET context to export the result of a gridview. This code-sample should be place in the handler method of a click button event.

protected void LinkButtonExportAsExcel_Click(object sender, EventArgs e)
{
try

{
DataView dataView
= (DataView)Cache["DataSource"
];
if (dataView == null) dataView =
GetDataView();
char[] bufferedExport = ExportDataTable(dataView.Table, "CSV"
);
if (bufferedExport == null) return
;
Response.Clear();
Response.ContentType
= "text/csv"
;
Response.ContentEncoding
=
Encoding.Default;
Response.Charset
=
Encoding.Default.EncodingName;
Response.AddHeader(
"Content-Disposition", "attachment;filename=Export.csv"
);
Response.AddHeader(
"Content-Length"
,
Encoding.Default.GetByteCount(bufferedExport).ToString());
Response.BinaryWrite(Encoding.Default.GetBytes(bufferedExport));
Response.Flush();
//
Instead of ‘Response.End’
//(cf :
http://support.microsoft.com/kb/312629/EN-US/)

HttpContext.Current.ApplicationInstance.CompleteRequest();
}
catch
(Exception ex)
{
//
}
}


public static char[] ExportDataTable(DataTable table, string format)
{
string
fieldSeparator;
string
containerSeparator, containerSeparatorReplace;
string
carriageReturn;
switch
(format.ToUpper())
{
case "CSV"
:
fieldSeparator
= ","
;
containerSeparator
= "\""
;
containerSeparatorReplace
= containerSeparator +
containerSeparator;
carriageReturn
= "\r\n"
;
return
ExportDataTable(table, fieldSeparator,
containerSeparator, containerSeparatorReplace, carriageReturn);
default
:
return null
;
}
}

public static char[] ExportDataTable(
DataTable table,
string
fieldSeparator,
string containerSeparator, string
containerSeparatorReplace,
string
carriageReturn)
{
string
temp;
StringBuilder contentBuffer
= new
StringBuilder();
StringBuilder headerBuffer
= new
StringBuilder();
int rowIndex = 0
;
foreach (DataRow row in
table.Rows)
{
int columnIndex = 0
;
foreach (DataColumn column in
table.Columns)
{
if (row[column.ColumnName] !=
DBNull.Value)
{
if (column.DataType == typeof
(System.String))
{
temp
= (string
)row[column.ColumnName];
temp
=
temp.Trim();
temp
=
temp.Replace(containerSeparator, containerSeparatorReplace);
contentBuffer.Append(containerSeparator);
contentBuffer.Append(temp);
contentBuffer.Append(containerSeparator);
}
else if (column.DataType == typeof
(System.Int32))
{
contentBuffer.Append(Convert.ToString((
int
)row[column.ColumnName]));
}
else if (column.DataType == typeof
(System.Byte))
{
contentBuffer.Append(Convert.ToString((
byte
)row[column.ColumnName]));
}
else if (column.DataType == typeof
(System.Double))
{
contentBuffer.Append(Convert.ToString((
double
)row[column.ColumnName]));
}
else if (column.DataType == typeof
(System.Decimal))
{
contentBuffer.Append(Convert.ToString((
decimal
)row[column.ColumnName]));
}
else if (column.DataType == typeof
(System.DateTime))
{
contentBuffer.Append(Convert.ToString((DateTime)row[column.ColumnName]));
}
}
if (columnIndex < table.Columns.Count && rowIndex == 0
)
headerBuffer.Append(column.ColumnName);
if (columnIndex < table.Columns.Count -1
)
{
if (rowIndex == 0
)
headerBuffer.Append(fieldSeparator);
contentBuffer.Append(fieldSeparator);
}
columnIndex
++
;
}
contentBuffer.Append(carriageReturn);
rowIndex
++
;
}
headerBuffer.Append(carriageReturn);
contentBuffer.Insert(
0
, headerBuffer.ToString());
return
contentBuffer.ToString().ToCharArray();
}
}

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   ,
Categories:   C# sample code | Office
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed