Introduction

I have spent numerous hours searching for solutions to programming problems that I think should have a more accessible solution. This blog will contain my experiences and the solutions that I found. Your comments are welcome of course.

Wednesday, August 18, 2010

Content vs Compile

I ran into this problem a few weeks ago while writing a class in one of my ASP.Net applications. For some reason, the Visual Studio 2010 environment considers any class you add to the solution as "content". What content means is that the code doesn't compile. This also means that you can not access the code using intelli-sense nor add references to the class in other parts of your code. It can be extremely frustrating to debug.

To solve the problem, select the class in question and then change "Build Action" in the properties window of that class to "Compile" instead of content.

Wednesday, May 26, 2010

ASP.NET C#: Add Confirm Message to GridView Delete Button

A good feature to have as part of any GridView is the ability to delete an item from the grid.
If the grid is dynamically linked to a DataSet then the buttons must be generated at run-time. This causes a problem if you want to perform some kind of confirmation to the delete operation so that the user doesn't simply delete an item by mistake.

The confirmation is easily achieved using an AJAX Tool Kit Extender control called the Confirm Button Extender Control. The problem is getting the control associated with the dynamically generated buttons.

The only way to achieve this would be to actually include the Confirm Button Extender Control definition as part of your template definition for the Delete button.

the following code demonstrates:
<Columns>
<asp:TemplateField HeaderText="Delete Item">
<ItemTemplate>
<asp:LinkButton
ID="delButton"
runat="server"
OnClick="delButton_Click">Delete</asp:LinkButton>
<asp:ConfirmButtonExtender
ID="cbeDelete"
runat="server"
TargetControlID="delButton"
ConfirmText="Are you sure you want to delete this item"
ConfirmOnFormSubmit="true"></asp:ConfirmButtonExtender>
</ItemTemplate>
</asp:TemplateField>
</Columns>

Thursday, June 19, 2008

C# Escape Characters

When trying to use a double quote or a single quote as part of a string you can use an escape character "\" to let the compiler know it is part of the string and not a regular quotation in the C# Language.

When declaring a string variable, certain characters can't, for various reasons, be included in the usual way. C# supports two different solutions to this problem.

The first approach is to use 'escape sequences'. For example, suppose that we want to set variable a to the value:

"Hello World
How are you"

We could declare this using the following command, which contains escape sequences for the quotation marks and the line break.

string a = "\"Hello World\nHow are you\"";

The following table gives a list of the escape sequences for the characters that can be escaped in this way:

The second approach is to use 'verbatim string' literals. These are defined by enclosing the required string in the characters @" and ". To illustrate this, to set the variable 'path' to the following value:

C:\My Documents\

we could either escape the back-slash characters

string path = "C:\\My Documents\\"

or use a verbatim string thus:

string path = @"C:\MyDocuments\"

Usefully, strings written using the verbatim string syntax can span multiple lines, and whitespace is preserved. The only character that needs escaping is the double-quote character, the escape sequence for which is two double-quotes together. For instance, suppose that you want to set the variable 'text' to the following value:

the word "big" contains three letters.

Using the verbatim string syntax, the command would look like this:

string text = @"the word ""big"" contains three letters."

Saturday, February 9, 2008

Get Machine Name and IP Address

This is another part of the program I was writing earlier. I usually work on this program either on my work machine or on my laptop. I have a database that has the same design on either machine and I keep it updated. I ran into the problem of having to go and change the connection string for my Datasets every single time that I would switch machines for testing. So instead of going through that I wanted to find a way to switch the connection string automatically. To that extend I wrote the following code.
Imports System.Net

Dim MachineIPList() As IPAddress>
Dim MachineHostName As String
Dim MachineIP As IPAddress

MachineHostName = DNS.GetHostName()
MachineIPList = DNS.GetHostEntry(MachineHostName).AddressList

For Each MachineIP In MachineIPList
Control.Text = MachineIP.ToString()
Next

Thursday, January 24, 2008

Get a Tree View of Folder Structure using Visual Basic 2005

I wanted to create a form that contains a tree view. The tree view would then build a list of all the folders on a particular drive selected. I ran into a few problems with writing this code. Mainly, that when the program tries to read information from the "System Volume Information" folder it returns an exception. The following code shows how to get around this problem and create the list.
The tree generation is started by clicking a button called btnRead. I also have a control on the form called tvFolders (TreeView Folders).

Imports System.IO

Private Sub BuildFolderTree(ByVal frmNode As Tree, ByVal curPath As String)

Dim curDirInfo As DirectoryInfo
Dim newFolder As TreeNode
Dim curFolder As String
Dim subFolder As String

Try
For Each curFolder In My.Computer.FileSystem.GetDirectories(curPath)
subFolder = My.Computer.FileSystem.GetDirectories(curFolder)
curDirInfo = My.Computer.FileSystem.GetDirectoryInfo(curFolder)
If Not (curDirInfo.Attributes = 22 Or curDirInfo.Attributes = FileAttributes.Hidden) Then

If frmNode Is Nothing Then

newFolder = tvFolders.Nodes.Add(subFolder)

Else

newFolder = frmNode.Nodes.Add(subFolder)

End If
BuildFolderTree(newFolder, My.Computer.FileSystem.CombinePath(curPath, subFolder))

End If

Next curFolder

Catch curEx As Security.SecurityException
' You can put a warning message here or just ignore it.
Catch curEx As UnauthorizedAccessException
' You can put a warning message here or just ignore it.

System.Windows.Forms.Application.DoEvents()

End Sub

This code shows also how Recursive Programming works. In order for the tree to be built, the program has to recursively go through each folder and check and see if it has sub folders. If it does a tree has to be built for that subfolder as well. This is the reason for having a BuildFolderTree sub procedure call in the middle of the sub procedure.

One Part I am not sure about is the curDirInfo.Attributes = 22. I did some research to find out why the attribute is equal to 22 but I could not find anything about a Folder Attribute equaling 22. The list of folder Attributes are as follows and doesn't include a 22.

Normal 0
ReadOnly 1
Hidden 2
System 4
Volume 8
Directory 16
Archive 32
Alias 64
Compressed 128

Wednesday, January 23, 2008

Get a List of Access Objects Using Visual Basic 2005

I am writing a program in visual basic 2005 that would perform a number of file system operations. Find the number of files on a drive, the number of folders on a drive or in a specific folder, and mainly find Old Files on a network drive.

I thought that it would be very beneficial to get information about Access Databases and Excel Spreadsheets on the drives as well to eliminate duplicates.

I have a button on the main form of the program (screen shot will be provided soon) that searches the drive for all Access Database (another snippet of code about that later). The program fills a list box with the names of all the Access Databases it finds in a specific location. From the list, I wanted the user to be able to click a button and then get a complete list of tables, queries, modules, and other objects in that database. I wanted this to be done instantly.

This can be achieved in one of two ways:

First Method
1. Click Project.
2. Click Add Reference...
3. Click the COM Tab
4. Select "Microsoft ActiveX Data Objects 2.5 Library"
5. Select "Microsoft ADO Ext. 2.8 for DDL and Security"
6. Click Ok

Use the following Code:

Imports ADODB
Imports ADOX

Dim conn As New ADODB.Connection
Dim cat As New ADOX.Catalog
Dim Table As ADOX.Table
Dim Qry As ADOX.View
Dim DBName As string

DBName = "C:\temp.mdb"

conn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source= " & DBName & " ")
cat.ActiveConnection = AccConn


For Each Table in cat .Tables
lstBox.Items.Add(Table.Name)
lstBox.Items.Add(Table.Type)
Next


For Each Qry in cat.Views
lstBox.Items.Add(Qry.Name)
lstBox.Items.Add(Qry.Command)
Next


Second Method
The second method uses a SQL Query to the Access Database that reads the information directly from the table called MSysObjects, MySysAccessObjects, MySysQueries.

These are system tables that can be queries directly.

for example
SELECT
MsysObjects.Id,
MsysObjects.ForeignName,
MsysObjects.DateCreate,
MsysObjects.Database,
MsysObjects.Connect,
MsysObjects.Name,
MsysObjects.Owner,
MsysObjects.Type

FROM
MsysObjects;


This Code will give you the list of all objects in the System Objects table. You can filter it according to what you're looking for. Then use that information inside of your VB Code. (Another Snippet coming later).