- •maranGraphics
- •CREDITS
- •ACKNOWLEDGMENTS
- •ABOUT THE AUTHORS
- •AUTHORS’ ACKNOWLEDGMENTS
- •TABLE OF CONTENTS
- •HOW TO USE THIS BOOK
- •INTRODUCTION TO C#
- •START VISUAL STUDIO .NET
- •OPEN A NEW C# PROJECT
- •OPEN A C# WEB PROJECT
- •SET JSCRIPT .NET AS THE DEFAULT SCRIPT LANGUAGE
- •EXPLORE THE CLASS VIEW WINDOW
- •VIEW THE CONTENTS WINDOW
- •GET HELP USING THE INDEX WINDOW
- •SEARCH FOR HELP
- •ADD COMPONENTS FROM THE TOOLBOX
- •ADD A TASK TO THE TASK LIST
- •CHANGE FORM PROPERTIES IN THE PROPERTIES WINDOW
- •ADD A CUSTOM TOOLBAR
- •DELETE A TOOLBAR
- •CHANGE THE VISUAL STUDIO ENVIRONMENT
- •MANAGE OPEN WINDOWS
- •OPEN A PROJECT
- •VIEW THE MAIN METHOD
- •COMBINE PROGRAM TYPES
- •ADD REFERENCE TYPES
- •ADD OPERATORS
- •INSERT ATTRIBUTES
- •ENTER CLASSES
- •ADD COMMENTS TO CODE
- •WRITE YOUR FIRST PROGRAM
- •ENTER XML DOCUMENTATION
- •ACCESS DOCUMENTATION
- •LOG A BUG REPORT
- •VIEW INFORMATION ABOUT C# BUILDING BLOCKS
- •PROGRAM CLASSES
- •ADD A CLASS
- •EMPLOY CLASS INHERITANCE
- •PROGRAM INSTANCE CONSTRUCTORS
- •INSERT DESTRUCTORS
- •PROGRAM STRUCTS
- •DISPLAY HEAP AND STACK INFORMATION
- •FIND TYPE INFORMATION
- •PROGRAM CONSTANT EXPRESSIONS
- •SPECIFY VALUE TYPES
- •PROGRAM NUMERIC TYPES
- •PROGRAM THE BOOLEAN TYPE
- •DECLARE REFERENCE TYPES
- •ENTER REFERENCE TYPE DECLARATIONS
- •CONVERT VALUE TYPES TO REFERENCE TYPES
- •PROGRAM POINTER TYPES
- •INSERT THE VOID TYPE
- •ADD INTERFACE PROPERTIES
- •ADD AN INTERFACE INDEX
- •VIEW INFORMATION ABOUT METHODS
- •ADD A METHOD
- •ADD STATIC METHODS
- •INCLUDE NON-STATIC METHODS
- •ENTER DELEGATES
- •PROGRAM EVENTS
- •ADD AN EVENT-HANDLING METHOD
- •VIEW INFORMATION ABOUT ARRAYS
- •ENTER SINGLE-DIMENSIONAL ARRAYS
- •ADD MULTIDIMENSIONAL ARRAYS
- •PROGRAM ARRAY-OF-ARRAYS
- •ITERATE THROUGH ARRAY ELEMENTS
- •SORT ARRAYS
- •SEARCH ARRAYS
- •IMPLEMENT A COLLECTIONS CLASS
- •PROGRAM STRUCTS
- •ADD AN INDEXER
- •INCLUDE ENUMERATIONS
- •CREATE STRING LITERALS AND VARIABLES
- •ASSIGN VALUES TO STRINGS
- •CONCATENATE STRINGS
- •COMPARE STRINGS
- •SEARCH FOR SUBSTRINGS
- •REPLACE CHARACTERS
- •EXTRACT SUBSTRINGS
- •CHANGE THE CHARACTER CASE
- •TRIM SPACES
- •REMOVE CHARACTERS
- •SPLIT A STRING
- •JOIN STRINGS
- •PAD STRINGS
- •VIEW INFORMATION ABOUT PROPERTIES
- •COMPARE PROPERTIES AND INDEXERS
- •PROGRAM PROPERTY ACCESSORS
- •DECLARE ABSTRACT PROPERTIES
- •INCLUDE PROPERTIES ON INTERFACES
- •VIEW INFORMATION ABOUT WINDOWS FORMS
- •ADD A WINDOWS FORM IN THE WINDOWS FORM DESIGNER
- •SET THE FORM TYPE
- •CHOOSE THE STARTUP WINDOWS FORM
- •CREATE A MODAL FORM
- •LAYOUT A FORM
- •SET A FORM LOCATION
- •CHANGE FORM PROPERTIES
- •CREATE A TRANSPARENT FORM
- •AN INTRODUCTION TO WEB FORMS AND CONTROLS
- •CREATE AN ASP.NET WEB SITE
- •CREATE A WEB FORM
- •ADD SERVER CONTROLS TO A WEB FORM
- •READ AND CHANGE PROPERTIES FROM OBJECTS ON A WEB FORM
- •USING SERVER-SIDE COMPONENTS ON WEB FORMS
- •INTRODUCING DATA ACCESS WITH ADO.NET
- •DISPLAY DATA WITH THE DATAGRID CONTROL
- •CONFIGURE THE DATAGRID CONTROL
- •INSERT DATA INTO A SQL DATABASE
- •UPDATE DATA FROM A SQL DATABASE
- •DELETE DATA FROM A SQL DATABASE
- •EXECUTE A STORED PROCEDURE IN A SQL DATABASE
- •READ XML FROM A FILE
- •SAVE XML TO A FILE
- •QUERY XML WITH XPATH
- •APPLY XSL TO XML
- •INTRODUCTION TO DISTRIBUTED APPLICATIONS
- •CREATE AN APPLICATION WITH PRIVATE ASSEMBLIES
- •CREATE AN APPLICATION WITH SHARED ASSEMBLIES
- •VERSION A SHARED ASSEMBLY
- •CONFIGURE A CLIENT FOR A VERSIONED ASSEMBLY
- •CREATE A WEB SERVICE
- •USING A WEB SERVICE
- •INTRODUCTION TO EXCEPTION HANDLING
- •THROWING AN EXCEPTION
- •HANDLING EXCEPTIONS WITH THE CATCH BLOCK
- •USING THE FINALLY BLOCK
- •WRITE ERRORS TO THE APPLICATION LOG
- •BASIC EXAMPLES
- •WHAT’S ON THE CD-ROM
- •USING THE E-VERSION OF THIS BOOK
- •INDEX
- •Symbols & Numbers
C#
CONFIGURE THE DATAGRID CONTROL
The DataGrid control is one of the richest Web Server
Controls that you have available in the ASP.NET framework.
To access the majority of the DataGrid control’s features, open the Property Builder dialog box. You can choose from among five views: General, Columns, Paging, Format, and Borders. The Property Builder dialog box is essentially a fancy user interface to the Properties dialog box, which is used for configuring all controls. Due to the DataGrid control having so many built-in features, the Property Builder dialog box comes in handy for quick configurations.
Another way you can configure the DataGrid control is the AutoFormat dialog box. The AutoFormat dialog window
is very similar to the auto format capabilities found for tables in Microsoft Word and Excel. The AutoFormat dialog box is a very quick way to format the grid, but you are stuck with a predetermined list of styles.
Both the Property Builder and Auto Format dialog boxes are available in the pop-up menu for the DataGrid; you can access the pop-up menu by right-clicking the DataGrid. To familiarize yourself with the DataGrid control’s capabilities, use both of these dialog boxes and make changes to the settings provided. After
you make these changes, go to the HTML for the Web form and notice the changes made to the asp:DataGrid element in your Web form.
CONFIGURE THE DATAGRID CONTROL
Autoformat
3601-X fg1205_06.eps
222 for more |
|
› Right-click the data grid |
|
||
adding server |
and select AutoFormat from |
|
form. |
the pop-up menu that |
|
DataGrid |
appears. |
|
|
|
|
data set. |
|
|
232 for more binding a data
set.
ACCESSING DATA WITH C# AND ADO.NET 12
You can take the code from the Apply It on page 233 one step further by adding sorting to the columns. To implement sorting, set the
AllowSorting attribute on the DataGrid tag equal to true and map the OnSortCommand to an event handler. When a sort request is made, a page level variable (SortExpression) is updated based on the column that was selected.
Example:
string SortExpression = ""; void Grid_Change(Object sender,
DataGridPageChangedEventArgs e) { dgdTitles.CurrentPageIndex = e.NewPageIndex; BindData(); }
void Sort_Grid(Object sender, DataGridSortCommandEventArgs e) { SortExpression = e.SortExpression.ToString(); BindData(); }
void BindData() {
if (SortExpression == "") SortExpression = "title";
SqlConnection cnPubs = new SqlConnection(
"server=(local);uid=sa;pwd=;database=pubs"); SqlDataAdapter daTitles = new SqlDataAdapter(
"select title, notes, price, pubdate from "
+ "titles order by " + SortExpression, cnPubs);
// Use this Data Adapter for rebinding. }
Professional 1
■ The AutoFormat dialog box appears.
ˇ Click to select a scheme for your data grid.
Á Click the OK button.
‡ Build and browse the Web page.
Note: See page 220 for more information on building and browsing a Web page.
■ The data grid appears in the preview window formatted with the scheme selected.
235
C#
INSERT DATA INTO A SQL DATABASE
For .NET applications, you can use the System.Data namespace for inserting data into SQL databases. Using the System.Data namespace allows you to insert into
any database with the same basic code. Switching to another database usually only requires changing the ConnectionString property on the database connection.
A simple way to get new data persisted into a SQL database is by running a SQL insert statement. SQL insert statements allow you to populate a database table with a new row of data that is provided by your application. You can collect new data from the user and dynamically build out a SQL insert.
The basic process of running an insert statement is to first acquire a Connection object so that you can communicate to the database. The key to obtaining a Connection object is to build a connection string that contains the authentication, server, and data catalog information (with other optional information). After a connection is obtained, you can use the connection to obtain a Command object. With the Command object, you can set the CommandText property equal to the insert statement. Then, you can execute the insert statement using one of several execution options. The most likely option to use is the ExecuteNonQuery.
INSERT DATA INTO A SQL DATABASE
the namespace
.
the class name
Main function.
Á Save the file.
‡ Add a SqlConnection variable and initialize the variable with a connection string to your database.
° Add a string variable for the insert command and initialize the string with a SQL statement that will add a row to your table.
If you insert data with the same primary key more than once, you will violate a constraint in the pubs database. If you are running a sample without proper error handling, you will halt/kill the application. To degrade gracefully you should implement exception-handling code in the try/catch/finally blocks.
ACCESSING DATA WITH C# AND ADO.NET 12
Example:
//In the if block change the code to the following
//(to capture exceptions like the primary key already
//exists, which will be the case if you run this
//sample more than once).
SqlCommand cmdTitles = new SqlCommand(sInsertCmd, cnPubs);
try
{
cmdTitles.Connection.Open();
cmdTitles.ExecuteNonQuery();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
cmdTitles.Connection.Close();
}
· Add a SqlCommand variable and use the string with the insert command and the connection created.
‚ Open a connection, execute the query, and close the connection.
— Add a message to the console about the SQL statement being executed.
± Set a debug stop.
¡ Click F5 to save, build, and run the console application.
■ The message about the SQL statement appears.
237
