Processes in .NET

Various ways that you can use System.Diagnostics.Process to perform various actions.

Continue reading "Processes in .NET"

Custom Sorting With IComparable and IComparer

The default sorting provided by .NET isn't enough most of the time. To enable sorting by a field that you want --- for example, a post's title --- you need to implement the IComparable interface. To allow the users to sort by multiple different fields, you need to implement IComparer.

Continue reading "Custom Sorting With IComparable and IComparer"

Screen Grabbing with .NET

How to programmatically connect to a webpage and get content via .NET.

ScreenGrabber Screenshot

ScreenGrabber Screenshot

Continue reading "Screen Grabbing with .NET"

How to Cast an ArrayList to an Array

So you have an ArrayList of strings and you want to return it as a string[] array. It's really easy:

return (string[])arrayList.ToArray(typeof(string));

using Female = Earth. LivingBeings. HomoSapiens. Female;

If you didn't know the using statement given in the title is valid, then congratulations! Now you do. ;-)

Why would you use this instead of the more general using Earth.LivingBeings.HomoSapiens.Female? Well:

To Avoid Conflicting Class Names

Suppose you Autopsy.cs uses both Earth.LivingBeings.Humans.Female as well as the Earth.LivingBeings.Monkeys.Female classes. One way to resolve the naming conflict is to laborously type out the fully-qualified class name each time you use it. The other way is to:


using HumanFemale = Earth.LivingBeings.Humans.Female;
using MonkeyFemale = Earth.LivingBeings.Monkeys.Female;

How to Send Authenticated SMTP E-mail


protected void Page_Load(object sender, EventArgs e)
{
 try
 {
  MailMessage message = new MailMessage(
    "from@example.com", 
    "to@example.com", 
    "Some Important Message", 
    "This is a test message and it's not that important.");

SmtpClient client = new SmtpClient("your.smtp.server.com");

System.Net.NetworkCredential credentials = new System.Net.NetworkCredential( "username", "password");

client.UseDefaultCredentials = true;

client.Credentials = credentials;

client.Send(message); }

catch (Exception ex) { Response.Write("Whoops! Couldn't send the e-mail: " + ex.ToString()); } }

Reading Binary Data From Database

Storing binary data directly in the database is a fairly common practice. I recently found myself wondering how to read binary data from the database and write it to the browser.

For example, suppose you have a PDF file stored in the database. How do you send it to the browser? I ended up with this:


protected void Page_Load(object sender, EventArgs e)
{
 using (SqlConnection connection = new SqlConnection("Connection string goes here"))
 {
  connection.Open();

using (SqlCommand command = connection.CreateCommand()) { command.CommandText = "dbo.GetPdf"; command.CommandType = CommandType.StoredProcedure;

command.Parameters.AddWithValue("@Id", 1);

using (SqlDataReader reader = command.ExecuteReader()) { if (reader.Read() == false) { return; }

<span class="kwd">int</span> contentLength = Convert.ToInt32(reader.GetBytes(0, 0, <span class="kwd">null</span>, 0, <span class="kwd">int</span>.MaxValue));

<span class="kwd">byte</span>[] buffer = <span class="kwd">new byte</span>[contentLength];

reader.GetBytes(0, 0, buffer, 0, contentLength);

Response.ContentType = <span class="str">"application/pdf"</span>;

Response.BinaryWrite(buffer);

} } } }

This works, but I wonder if it's the most efficient way to solve the problem? For example, we're calling the GetBytes method twice, which means that we're actually reading the whole document just to get its length. If the document is huge, this is wasteful. In that situation, I'm guessing having a fixed-sized buffer and reading the data in a while loop would be the way to go.

How to List All Tables in a Database

EXEC sys.sp_tables
But this will list every single table, including all the system tables, which we rarely ever want. To list only tables created by the user, add a @table_owner parameter. For most databases, this will be "dbo":

EXEC sys.sp_tables NULL, 'dbo', NULL, NULL, NULL

4 of 7 pages « First  <  2 3 4 5 6 >  Last »

On the Side