Sides wrote:I kind of agree with eraser, but normally considered good practice to do that type of stuff in a helper or wrapper class... like maybe create a ConvertHelper that has a string ConvertToString(object value) method in it which does the != DBNull check before doing the convert inside the method. If you have to write the if statement more than twice then yeah - definitely create a helper/wrapper class.
Gotta ask though... what's the scenario that you're trying to convert the middle name to string ??? Actually, your exception sounds like you're trying to cast rather than convert.... bit surprised at you having to convert or cast from a DataSet/DataTable like that...
Joins etc. are all data operations, and the big rule to leave as much of that to SQL as you can... as in you write a StoredProc or a View that gives you what you want, and in C# you just set up your SqlDataAdapter/SqlCommand with the T-SQL to call it.
middle name was just an example.because I thought it would be easier to explain.
The actual task is creating an invoice. Selects from a a customer table and an address table. The address table has a flag for billing and delivery address types. A customer can have 2 addresses, a billing address and a delivery address. If the billing and delivery go to the same ponit there is no delivery address. An address has multiple lines. addr1, addr2, addr3, suburb etc. Most of the time addr2 & 3 are empty. Half of the time all of the delivery address fields are empty. The select statement is a stored procedure... customer inner joined to address for billing, left outer joined for delivery address. There isnt a problem with the selects.
When the data comes back there is a long list of assigning those data fields to the fields that appear on the invoice. Invariably, some of these fields are null. Its very ugly haveing an if statement around each of them, hence why I am using the coalesce(addr2, '') etc for each field in the stored proc. Was just wondering if thats the nicest way really. I know and empty string is not a null string, but I cant assign a null database object to a string. Thats not of real concern to me though as in the application instead of checking for null I check for empty string. I'm able to print direct to a pdf file using iTextSharp library, its all working well, just that my OCD is worried about the aesthetics of the code more so than its functionality :P
And I could join a geek forum and have done in the past, but often they explain things at an uber geek level which doesnt help at all. And I love you guys!