Presentation is loading. Please wait.

Presentation is loading. Please wait.

Viewing DataTable Contents1 CTEC2902 Advanced Programming Viewing Data Table Contents.

Similar presentations


Presentation on theme: "Viewing DataTable Contents1 CTEC2902 Advanced Programming Viewing Data Table Contents."— Presentation transcript:

1 Viewing DataTable Contents1 CTEC2902 Advanced Programming Viewing Data Table Contents

2 Viewing DataTable Contents2 The story so far… You know how to Create data tables DataTable dtStaff = New DataTable; Connect to a SQL-Server database, via clsSQLServer clsSQLServer DB = New clsSQLServer(“MyDB.mdf"); Run a stored procedure to select records DB.Execute("sp_tblStaff_GetName"); Save in a data table the records selected by stored procedure dtStaff = DB.QueryResults; How do you view the data in a data table? Let us find out… Viewing The Data

3 Viewing DataTable Contents3 Viewing The Data Using The Grid View

4 Viewing DataTable Contents4 ‘Create database object clsSQLServer DB = New clsSQLServer (“books.mdf“); ‘Create data table object DataTable dtDetails = New DataTable; ‘Populate data table object, using the database object DB.Execute(“sp_tblData_GetAll“); dtDetails = DB.QueryResults; ‘Bind data table to data grid; to do this… ‘Set data source property of data grid to data table name grdViewer.DataSource = dtDetails; ‘Actually show the data grdViewer.DataBind(); Viewing The Data Using Grid View Control What you get is …

5 Viewing DataTable Contents5 Record Etc. Data Table Viewing The Data Data Grid View Can adjust itself in size to Number of records Number of fields in the source data table BUT could mess up your page Using DataSource property gives a data-bound control Displays ALL the data in source data table Displays field names as they appear in the source data table

6 Viewing DataTable Contents6 Viewing The Data Using The List Box

7 Viewing DataTable Contents7 Record Etc. Data Table Viewing The Data In a data-bound list box Adjusts itself to number of records Can display ONLY a single column of data (or field) Contains many records and fields To specify which field to display, use DataTextField property

8 Viewing DataTable Contents8 ‘Create your objects clsSQLServer DB = New clsSQLServer (“BigCorp.mdf“); DataTable dtDetails = New DataTable; ‘Populate your data table DB.Execute(“sp_tblEmployees_GetAll"); dtDetails = DB.QueryResults; ‘Set data source property to data table name lstDetails.DataSource = dtDetails; ‘Say which field you want to show in list box lstDetails.DataTextField = “Surname" ; ‘Now show the data lstDetails.DataBind(); Viewing The Data In a data-bound list box

9 Viewing DataTable Contents9 ‘Create SQL object clsSQLServer DB = New clsSQLServer ("BigCorp.mdb“); DataTable dtEmps = New DataTable; ‘Populate the local data table DB.Execute(“sp_tblStaff_GetFullName“); dtEmps = DB.QueryResults; ‘Set data source property to data table name lstSurnames.DataSource = dtEmps; ‘Set display member property to field you want to show lstSurnames.DataTextField = “Surname" ; ‘ Now show the data lstSurnames.DataBind(); Viewing The Data This code will crash – why? SELECT Surname + ', ' + FirstName AS Fullname FROM tblStaff No Surname field (or column) in dtEmps table (see SELECT) Use FullName instead

10 Viewing DataTable Contents10 Next Week How to reach individual records and item/field values? Viewing The Data


Download ppt "Viewing DataTable Contents1 CTEC2902 Advanced Programming Viewing Data Table Contents."

Similar presentations


Ads by Google