Posts Tagged ‘C#’

New Article: Encoding files as base64 strings

May 8th, 2008

Check out my new article on encoding files as base64 strings. Useful for sending binary data across the wire where sending such binary data may be difficult. Especially useful for webservices and sending files.

Tags: , ,
Posted in Development, General | Comments (0)

Tutorial: .NET XML Into A DataSet

April 8th, 2008

New tutorial for C# about using XML data in a DataSet object for manipulating the data like it came from a database. See the Software Development section, or go straight to it here. Cheers, and happy coding!

Tags: , ,
Posted in C#, Development | Comments (0)

Beep on the Enter Key

August 22nd, 2007

One of the applications that I wrote and maintain here for use at my job is a C# Windows application. A couple of revisions ago I set it up so that the user could press the Enter key to submit the login instead of having to click the actual login button. For the longest time now this has produced a beep sound. ANNOYING!

I finally took the time to address this. On the text boxes I was catching the KeyUp event and checking for the Enter key. Come to find out that to properly make sure that the WHOLE event gets handled I needed to use the KeyPress event, and set the Handled property to true so that it would not flow back down to the key event handler on the form. Now, no more beep!

Tags: ,
Posted in C#, Development | Comments (0)

Images in SQL

February 9th, 2007

So I was presented with a task today of extracting images that were stored in a Microsoft SQL Server 2000 database and save them as JPEG files. Here is a bit of code that will do this. In this code sample I am assuming domain authenticated SQL login. The table name here will be called Photos and the column names id, type, and photo.

  1. SqlConnection connection;
  2. SqlCommand command;
  3. SqlDataReader reader;
  4.  
  5. string outputDirectory = "C:\TestLocation";
  6. PictureBox pic = new PictureBox();
  7.  
  8. using (connection = new SqlConnection("data source=MyServer;initial catalog=Database;persist security info=True;packet size=4096;Trusted_Connection=True"))
  9. {
  10.    connection.Open();
  11.  
  12.    command = new SqlCommand("SELECT id, type, photo FROM Photos", connection);
  13.    command.CommandType = CommandType.Text;
  14.    command.CommandTimeout = 3000;
  15.  
  16.    reader = command.ExecuteReader();
  17.  
  18.    while (reader.Read())
  19.    {
  20.       try
  21.       {
  22.          byte[] rawBytes = (byte[]) reader["photo"];
  23.          MemoryStream memStream = new MemoryStream();
  24.          memStream.Write(rawBytes, 0, rawBytes.Length);
  25.  
  26.          string filename = outputDirectory + "\\image-" + reader["id"].ToString() + "-" + reader["type"].ToString() + ".jpg";
  27.  
  28.          pic.Image = Image.FromStream(memStream);
  29.          pic.Image.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg);
  30.  
  31.          memStream.Close();
  32.       }
  33.       catch (Exception ex)
  34.       {
  35.       }
  36.    }
  37. }

This C# code will loop over a record set and save the images stored in the table as JPEG files. Happy coding!

Tags: , ,
Posted in C#, Development | Comments (0)

Logical Drives and WMI

February 1st, 2007

In an effort to support removable media devices in SyncXpress I’ve had to do a bit of learning about WMI in .NET. It is actually quite cool how much information you can get from WMI about ANY piece of hardware attached to your system, and even INSIDE your system. So in my case I want to find out information about a logical drive device in Windows. Using the System.Management namespace we can access the ManagementObject classes that will allow us to query the WMI services to get information about our device. Here is an example of getting information about your C:\ drive.

  1. ManagementObject wmi;
  2. string serialNumber, name, freeSpace;
  3.  
  4. wmi = new ManagementObject("Win32_LogicalDisk.DeviceID=\"C:\"");
  5.  
  6. serialNumber = wmi["VolumeSerialNumber"].ToString();
  7. name = wmi["Name"].ToString();
  8. freeSpace = wmi["FreeSpace"].ToString();

For more information on what data can be queried on a logical device see http://msdn2.microsoft.com/en-us/library/aa394173.aspx

Tags: ,
Posted in C#, Development | Comments (0)