Archive for the ‘Programming’ Category

Mixed Language Programming in ASP.Net

Thursday, November 18th, 2010

For historical reasons I find myself developing some ASP.Net content in a mixed language environment.  I have some older, legacy code written in C# that I need to integrate into a new web site written using VB.Net.  Putting the VB.Net .ASPX pages into a separate directory from the C# .ASPX pages got around most of the problem, but I was left with some common code that belongs in the App_Code directory of both sites.  .Net won’t let you compile mixed languages into a single assembly, and since everything the the App_Code directory needs to be compiled into a single assembly I was in a bit of a quandary about what to do.

Fortunately, it turns out there is a simple solution to the problem.  You just need to put your C# code into a sub-directory of the App_Code directory and make an entry in the  <compilation> section of the web.config file. Add the following XML:


<compilation ... >
<codeSubDirectories>
<add directoryName="App_Code_CS" />
</codeSubDirectories>
...
</compilation>

Where App_Code_CS is the name of the sub-directory containing the C# code.

Forcing Data Entered into a WPF DataGrid to UpperCase

Wednesday, November 17th, 2010

All I wanted to do was force data entered directly into a DataGrid into upper case.  You would think this would be an easy thing to do.  Far from it.  It involved delving into things called ValueConverters, which are not that difficult to write but are a major pain in the butt to actually use.

(more…)

Adding Rows to a WPF DataGrid Programatically

Wednesday, November 17th, 2010

If you are going to let your users edit data in a DataGrid, at some point you will want to allow them to add records to the grid.  Adding a new row is simplicity itself, you just add a new item to the collection that is acting as your data source.  Suppose we are working with the data source from my prior DataGrid article.  We just add a new Account object to our Accounts collection, like this:


Accounts.Add(New Account())

This adds a new row to the DataGrid but it doesn’t put the grid into edit mode with the new row as the currently selected row.  Doing this is a bit more involved.  First we need to set the currently selected row by setting MyGrid.SelectedIndex to the index of the Account object in our collection.  For a newly added row, that would be Accounts.Count - 1. i.e. the last object in the collection.  Then we need to set the currently selected column by setting MyGrid.CurrentColumn to the first column in the row.  This will always be MyGrid.Columns(0).  Then we need to put the grid into edit mode by calling MyGrid.BeginEdit(), which puts the currently selected cell into edit mode.  Finally we need to give the grid focus by calling MyGrid.Focus().  The complete code to add a row and set the grid into edit mode looks like this:


' Add a new account to our list of accounts
Accounts.Add(New Account())
' Make the newly added account the current row of the grid
MyGrid.SelectedIndex = Accounts.Count - 1
' Make the ID field the current column of the grid
MyGrid.CurrentColumn = MyGrid.Columns(0)
' Put the grid into editing mode
MyGrid.BeginEdit()
' Give the grid control focus
MyGrid.Focus()

It seems like nothing is ever easy with this stuff.  Sigh.

Binding Data to a WPF DataGrid

Wednesday, November 17th, 2010

Binding a data source to a WPF data grid is not exactly difficult, but it isn’t exactly easy or convenient either.  The only way that I have found to do it, so far, involves a combination of code behind logic and manually editing the XAML.  There are some short cut methods that can reduce the amount of XAML code that needs to be manually edited, but I find these short cut methods to be less than completely satisfactory.

(more…)

Developing .Net Applications on a Network Share

Wednesday, November 17th, 2010

If you keep all of your development projects on a network share like I do then you have probably had to deal with certain challenges regarding permissions along the way.  In the Win32 world this isn’t usually too bad, but in the .Net world it can become a major pain in the butt.

When you keep your project on a network share, all of the assemblies that your project references and all other assemblies required by your project a typically kept in the project’s bin directory.  Everything will seem to be just fine until you try to execute your application.  At this point you will start getting all kinds of strange errors when your applications tries to load dependent assemblies from the network share.  It turns out that .Net 4.0, by default, doesn’t allow assemblies to be loaded from a share.

Once you know the secret, fixing this is actually not that difficult.  You have to add a line to the app.config file for your project.   In the <runtime> section of the configuration file you need to add:<loadFromRemoteSources enabled=”true” />

That’s all there is to it.

Developing ASP.Net Applications on a Network Share

Tuesday, November 9th, 2010

If you keep all of your development projects on a network share like I do then you have probably had to deal with certain challenges regarding permissions along the way.  In the Win32 world this isn’t usually too bad, but in the .Net world it can become a major pain in the butt.  This is especially true of ASP.Net applications.

I recently started work on my first ASP.Net application.  Everything was going fine until I tried to execute it.  Visual Studio’s internal web server basically told me to piss off.   It turns out that, by default, ASP.Net will not execute applications from a network share.

Once you know the secret, fixing this is actually not that difficult.  You have to add a line to the devenv.exe.config file for your version of Visual Studio.  For Visual Studio 2010 this is found in C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE.    In the <runtime> section of the configuration file you need to add:

<loadFromRemoteSources enabled=”true” />

That’s all there is to it.

SecurityException While Accessing WCF Service

Tuesday, November 9th, 2010

I am just getting started with Silverlight.  For my first application, I wrote a simple form that interacts with a WCF service for database access.

The first time I tried to access the WCF service from the Silverlight application I got a SecurityException.  The error message that the exception gave was long and rambling and mentioned things like “cross domain access” and other fairly meaningless bafflegab.  I spent a lot of time Googling for solutions to this problem and the results all had to do with tweaking the IIS security settings, which are irrelevant when using Visual Studio’s internal web server.

(more…)

Forcing a Delphi Program to Run with Administrator Privilege

Monday, June 21st, 2010

No matter how hard you try to eliminate the need for administrator access from your programs, sometimes they just need to run as administrator in order to work.  I came across this requirement while recoding our application’s configuration utility.  I dutifully removed all configuration entries  from the registry and moved them to the “All Users” folder but I needed to be able to configure some of our programs to run as Windows services.  No matter how you look at it, this requires administrator privilege.

I didn’t want to force our users to “run as administrator” on Vista, Windows 7 and Windows 2008.  Can you imagine the number of support calls that would generate? :(  So, I starting looking for a way to force the program to request administrator privilege .

(more…)

IBObjects, Firebird and “request synchronization” Errors

Tuesday, January 19th, 2010

I have been in the process of migrating my applications from an older version of IBOjects to version 4.8.6.  Not the latest version but good enough for my purposes.  Anyway, in certain circumstances, one of my applications started throwing “request synchronization error” exceptions. It never did this when running under the older version of IBObjects.  I spent quite a bit of time debugging my application and IBOjects trying to find a reason for the error with no luck.  Finally, just for something to try, I replaced fbclient.dll with the latest version and the problem is now gone.  Go figure!

Getting the List of Configured DNS Servers

Wednesday, January 13th, 2010

If you ever find yourself in a position where you need to know that IP addresses of the DNS servers configured on a Windows system just use the GetNetworkParams Windows API call.  This is not implemented in the Delphi 2007 component library, but luckily it is implemented in the Jedi Code library.