Friday, July 18, 2008

Change TableAdapter Connection Programmatically

I ran into an issue on a project today. I was using DataSets created in the Visual Studio 2008 DataSet designer to facilitate data access in a handheld application I am working on. The problem arose when I realized that I wanted to be able to programmatically specify the connection string to be used at run-time. I realized that as I was creating TableAdapters in the DataSet designer, it was hard-coding the connection string into the generated source code. It didn't occur to me at the time that this would be a problem, as I was really just experimenting and testing anyway. Well, when I was ready to set the connection strings in code, I couldn't find any visible properties or methods to allow it! AAH!

Well, after some Google searching, I stumbled onto what is one of those "so simple, I'm kicking myself" answers. In the dataset designers, if you click on a TableAdapter and look in the properties pane, one of the properties of the TableAdapter is ConnectionModifier. By default, mine were set to Internal, making the connection object and it's associated connection string invisible to the rest of my application code. By changing the ConnectionModifier property to Public, I now have access to the connection object of my TableAdapters, and I can set/change them programmatically at run-time.

No comments: