Monday, July 18, 2011

Creating a .CSV file in ASP.NET

I wanting to create a .CSV file from a query to SQL Server in ASP.NET. The link below has an excellent description of creating the .CSV file without having to create a temp file like I was originally planning to do. This post doesn't show how to wrap the query results up, but that is pretty simple to figure out:

http://wiki.asp.net/page.aspx/401/export-to-csv-file/

Thursday, July 14, 2011

Selecting a Folder in VB.NET

To go along with my previous post on the Open File and Save File dialogs in VB.NET, I am providing this post using FolderBrowserDialog to select a specific Directory.

Browsing to Select a Directory in VB.NET
     Dim dirDlg As New FolderBrowserDialog

dirDlg.RootFolder = Environment.SpecialFolder.MyComputer

If dirDlg.ShowDialog() = DialogResult.OK Then

' do something with the selected path
Debug.Print("Directory: " + dirDlg.SelectedPath)

Else

' the dialog was cancelled

End If


The FolderBrowsingDialog object has many options, but I have only shown the most commonly used one in the example. The RootFolder property lets you specify the directory to start browsing from. You can set this property to an explicit path (like "c:\MyData") or you can use the special directories that are defined for you:
Environment.SpecialFolder.MyDocument
Environment.SpecialFolder.MyComputer
Environment.SpecialFolder.MyPictures
Environment.SpecialFolder.Desktop

The default is Environment.SpecialFolder.MyComputer.

After the ShowDialog call returns OK, use the SelectedPath property to get the selected directory.

Selecting Files to Open or Save in VB.NET

VB.NET has come a long way from the clunky Common Dialog control of VB6 for implementing a standard dialog that will let you browse to find a file to Open or Save. Here is an example of each:

Browsing to Open a File in VB.NET
      Dim fileDlg As New OpenFileDialog

fileDlg.Filter = "CSV File (*.csv)|*.csv|txt files (*.txt)|*.txt|All files (*.*)|*.*"
fileDlg.DefaultExt = "csv"

' could be omitted to use default, or a specific dir can be given
fileDlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

If fileDlg.ShowDialog() = DialogResult.OK Then

' Do something with the selected filename
Debug.Print(fileDlg.FileName)

Else

' the dialog was cancelled

End If

The OpenFileDialog object is used here, and I have only shown the most commonly used options. The Filter property lets you specify the which File Types will be selectable form the Open File Dialog (in a dropdown list). The value is set to a string that is a pipe (|) delimited list composed to a File Type Description and Filter Pattern. If you have more than one File Type, you can add another pipe, then another Description|Filter Pattern, and so on. If you leave the Filter property empty, then then Open File Dialog will just not have a drop down to select the File Type filter. Here are some examples:

"CSV File (*.csv)|*.csv"
"CSV File (*.csv)|*.csv|txt files (*.txt)|*.txt"
"CSV File (*.csv)|*.csv|txt files (*.txt)|*.txt|All files (*.*)|*.*"

If you are using Filter with more than one File Type specified, you can also use DefaultExt to specify which File Type to use as the default. Just specify the extension as a string (with no dot). This property also will set the default file extension to use if you don't specify it in the filename (if typed in), so this property can also be used without the Filter property being set.

The InitialDirectory property will let you define which directory to open up in. You can set this property to an explicit path (like "c:\MyData") or you can use the special directories that are defined for you, like:
Environment.SpecialFolder.MyDocument
Environment.SpecialFolder.MyComputer
Environment.SpecialFolder.MyPictures
Environment.SpecialFolder.Desktop

There is also a Title property that can be used to change the name of the Open File Dialog. In this example, it is not used and defaults to "Open File".

After the ShowDialog call returns OK, use the FileName property to get the selected filename.

Browsing to Save a File in VB.NET
   Dim fileDlg As New SaveFileDialog

fileDlg.Filter = "CSV File (*.csv)|*.csv|txt files (*.txt)|*.txt|All files (*.*)|*.*"
fileDlg.DefaultExt = "csv"

' InitialDirectory can be omitted to use default, or used to specify an explicit directory
fileDlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

fileDlg.OverwritePrompt = True

If fileDlg.ShowDialog() = DialogResult.OK Then

' Do something with the selected filename
Debug.Print(fileDlg.FileName)

Else

' the dialog was cancelled

End If

Saving a file is similar to opening a file. You use a SaveFileDialog object instead of a OpenFileDialog object. Many of the properties work the same way as with the OpenFileDialog such as Filter, DefaultExt, and InitialDirectory. I also show OverwritePrompt set to True (which is the default). This setting will cause the dialog to prompt you when you try to save over an existing file.

After the ShowDialog call returns OK, use the FileName property to get the filename.

If you want to select a Directory instead of a file, look at this post.

Wednesday, July 13, 2011

Setting all elements of an Array to Null in C# (like with memset in C++)

I sometime find it strange that things we take for granted in C++ are hard in C#. Probably I should say hard to find the equivalent for rather than hard to do. I routinely clear out my arrays in C++ with memset (setting each item to 0). To get the equivalent in C#, you can use the Array.Clear(...) method like this:
Array.Clear(MyArray,0,MyArray.Length);
Here is a more detailed example:

http://www.dotnetperls.com/array-clear

Tuesday, July 12, 2011

Preventing Flicker with GDI+ drawing in C#

In C#, the OnPaintBackground method is called before the OnPaint event and it draws the background color for the entire view. If you are already drawing the background in the OnPaint event, then this process will cause flicker. You can override the OnPaintBackground and just do nothing in it to prevent the flicker:
protected override void OnPaintBackground(PaintEventArgs pevent)
{
// do nothing
}