Thursday, August 9, 2007

ASP.NET Data Controls vs Data Objects

1. Data Controls vs Data Objects
A. Data controls are wizards that create code for ADO.NET data objects
B. Data controls are found on the tool box. Data object you create in code.
2. Data Controls are Microsoft tools to create the ADO.NET objects for you.
A. No Code solution to data access
1. Wizards write your code on the fly
B. Careful!!!!
C. Combination of object / wizards
3. Tool Box . Data Tab
A. Note you have duplicate controls for each data access type.
1. Once you select on type…you need to keep using that ‘type’
B. Drag the DataAdapter onto the Form
C. The DataAdapter will create the connection object
1. Caution…use NEW connection the first time.
D. Use the Query Builder to create the SQL statement
E. When you finish…it will tell you what you created
4. So far this is only the connection…but no data
A. Can not bind controls to a connection SO populate a dataset
5. The no code method: Right click on the DataAdapter
A. If you use the Generate Dataset you get a Typed DataSet
1. Creates a Type Library for your Dataset as well as a dataset. Nice for programmatically accessing your data but …. overhead
2. =…a wrapped up (user friendly) dataset
B. Creates an xsd file that defines the XML database created in memory….
1. But does not create the dataset
C. You can bind a control to a Typed or non typed dataset
6. Binding the DataGrid (in code or design time)
A. DataSource = the dataset you created
B. DataMember = there is only one table in the dataset so obvious
C. But…the dataset is empty….
7. Filling the dataset to populate the grid
A. In PageLoad…
1. da.fill(ds) loads the DataSet through the data adapter
2. Grid.DataBind…now the ds has data so bind it.
8. Cool tip: Using Server Explorer to check SQL Syntax
A. Open server explorer (CTRL ALT X) and drill down to the table to Query
B. Right click and retrieve data. View as tab
C. You should have a Query Tool bar that lets you switch views
D. Click on SQL to see SQL code and paste in yours from your code
E. Click on ! to run it…If it works here…it will work in your code.