Subscribe to SQL Server Magazine | See More SQL Server and Database Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!
Main Article    Managing Hierarchical Rowsets
DOWNLOAD THE CODE:
Download the Code 97180.zip

To make the application more useful as a general-purpose demonstration, I added several features that illustrate a few tricky technical points.

Importing pictures. I wanted to show how to insert pictures into the database, so I included a routine to capture the filename of a picture to be imported into the database. Web Listing A shows the routine’s code. I don’t want to discuss the suitability of storing binary large objects (BLOBs) in the database here; suffice it to say that this routine works effectively for importing pictures into SQL Server.

Handing new row initialization. When a user wants to add a new row to the database, it’s a good idea to provide correct default values for the form fields. You can provide the values in a variety of ways, but I used the TableNewRow event handler. In this case, if the user clicks the plus (+) sign in the GetCustomersByStateBindingNavigator, a new row is created behind the scenes and the TableNewRow event is fired, letting you apply whatever logic you want to fill in the new row values. These changes aren’t committed to the database, just to the underlying DataTable. You can find where to add this code by using the TableAdapter Designer. Simply right-click the appropriate TableAdapter diagram and select View Code. You’ll see a blank form where you can add your code to the TableAdapter code. In this case, you’re working with a partial class, which isn’t overwritten when you reconfigure the TableAdapter. The code in Web Listing B implements the TableNewRow event handler for the Customers and Orders tables.

Providing a list of addresses. Because a customer can have several addresses, I provided a drop-down list of addresses in the UI. I implemented the drop-down list by adding another table (Addresses) to the TableAdapter diagram, adding an FK constraint, and disabling the update, delete, and accept/reject rules. I also programmed the SQL Server database to automatically delete all of a customer’s addresses when the customer was deleted.

End of Article




Post Your Comments Here

You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now

Reader Comments

Neither zip 97182 or 97180 have vs project or database??

mjgreco

Article Rating 4 out of 5

Hi mjgreco, Thanks so much for your feedback. We have checked both .zip files and found that everything that was provided by the author is accounted for in the .zip files. However, if you would like to discuss this further, please feel free to contact me directly at mbearly@sqlmag.com.

Also, I queried the author to see about the database. According to William Vaughn, "The database (IIRC) is Adventureworks (one of the sample database)."

Thanks!

Megan Bearly Associate Editor, SQL Server Magazine mbearly@sqlmag.com

meganbearly

Article Rating 5 out of 5

 
 

ADS BY GOOGLE