110 likes | 253 Vues
This guide covers the essentials of accessing data through graphical user interfaces (GUIs) in ASP.NET, focusing on `GridView` and `DetailsView` controls. Learn how to connect to SQL data sources, configure connections using the SQLDataSource wizard, and display data effectively. Each control serves a specific purpose—GridView for showing multiple records with support for paging and sorting, and DetailsView for displaying single records with insert and update functionality. Additionally, discover how to manage configuration settings and access data rows and cells efficiently.
E N D
Agenda • Data Sources • SQL Data Source • Displaying Data • GridView • DetailsView • Configuration Manager • Accessing Data
Data Sources • Embody a connection to a database • Location • Name • Contain an SQL command • Example: • SqlDataSource is used with MS SQL Server
SQL DataSource • It represents: • name and location of database • SQL command • Specific to MS SQL Server • Uses a wizard for configuration • Prefix: “sds”
Displaying Data • Two GUI controls used for displaying data: • GridView: shows all records • DetailsView: shows selected record • Need to be bound to a Data Source, such as • SQLDataSource • DataReader • Customizable • See note on Design in Unit 3.1 Step-By-Step
GridViews • Show all records of a dataset • Can do more than display • Paging • Sorting data • Insert, Update functions • Custom Template Fields
DetailsView • Show only a single record at a time • Supports inserting and updating similar to GridViews • Often paired with GridViews
Configuration Manager • Web applications need governing rule-sets • For login controls, web services, or databases • They exist as web.config files in ASPPub • For Databases Connection Strings • Store path, database filename, database driver • Example: (Can you tell the “name” of the connection string?) <configuration> <connectionStrings> <add name="Products" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\SouthBreezeGroceries.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/> </connectionStrings> <other-web-service-stuff> </other-web-service-stuff> </configuration>
Accessing Data: Create a GridViewRow • GridViews are collections of rows. • So your code needs to loop through these rows • First, create an object of the type GridViewRow GridViewRowGrvCheck GridViewGrvOrders
Accessing Data: Setup the loop • Setup a loop to go through all rows • The FOREACH loop is a variation of the FOR loop • It can automatically cycle through rows. • For example: foreach (GridViewRowgrvCheck in grvOrders.Rows) { // do stuff for one row at a time // collect all data, do any totals }
Accessing Data: Working with cells • Each row has many columns, or cells. • Cells are indexed (counting from zero). • In the example below: • The first column, i.e. Column 0 is occupied by the Select button • The second column contains OrderID. Since indices begin at 0, the second column is accessed by the index of 1, as shown below: OrderID = Convert.ToInt32(grvCheck.Cells[1].Text); grvCheck.Cells[1].Text