- Preparing Your Machine to Work with Strings
- Working with Strings
- Initializing Strings
- Comparing Strings
- Concatenating Strings
- Finding the String Length
- Comparing and Concatenating Strings in the Sample Application
- Creating Strings from Characters
- Using Escape Characters
- Using Literal Strings
- Accessing the String's Characters
- Finding a Substring within a String
- Extracting Part of the String
- Splitting a String
- Joining a String
- Uppercasing and Lowercasing
- Formatting Strings
- Finishing the Sample Application
- Representing Objects as Strings
- Allocating Strings with StringBuilder
Comparing and Concatenating Strings in the Sample Application
It's time to hit the keyboard and put some of these principles into practice. In this section you will implement the login screen for the application.
First, let's talk about database stuff. We are going to connect to a small table called contacts in the csharpvqs database. At the beginning of the chapter you ran a script called createdb.cmd. This script talks to MySQL and tells it to create the csharpvqs database and add the contacts table and some records to the table. The contacts table has the following fields: ID, LastName, FirstName, and Phone. ID is a string field that basically stores social security numbers without the dashes. LastName, FirstName and Phone should be self-explanatory.
The first step in talking to a database is to connect to it. Each database provider has a connect class. In dbProvider, the connect class is called eInfoDesigns.dbProvider .MySqlClient.MySqlConnection. To connect to the database you need to create an instance of this class and tell it four things: what machine you're connecting to, what database, who you are, and your password. We build one string with this information, as you will see later, and feed it to the Connection object. This string is called the connection string. Then we open a connection. For the most part everyone will have the same string. For machine you can specify localhost, which means the current machine. The database doesn't require a password so you can leave the user id and the password blank. The database is going to be csharpvqs, which is the database that contains all the tables for the sample code in this book. So let's get to it.
To implement the login screen:
In Solution Explorer, right-click on the file login.aspx and choose View Designer from the popup menu. You will see the form you created at the beginning of the chapter.
Double-click on the connect button to open the code editor. The wizard adds the btnConnect_Click event.
Add the code in Figure 4.26. This code checks to see if the server and database fields have been set. If they haven't been set to anything, this code will use the default values of localhost and csharpvqs correspondingly.
Figure 4.26 Using the conditional operator, you can set the variables equal to the contents of the textbox, or if the textbox is empty, to a default value.
Now enter the code in Figure 4.27. This code concatenates several strings to create one connection string. Notice the format for the connection string. All .NET database providers follow a similar format. In fact, this connection string is identical to a connection string you would use for SQL Server. This code also stores the connection string in the Session object so that other forms can use it. You learned about the Session object from Chapter 3.
Figure 4.27 This code constructs a connection string. Building a connection string is the first step in connecting to a database. It tells the connection object what database to connect to, the user id, the password, etc.
Enter the code in Figure 4.28. This figure contains the code to jump to another HTML page: contacts.aspx. You'll write this page later in the chapter.
Figure 4.28 This code constructs a connection string. Building a connection string is the first step in connecting to a database. It tells the connection object what database to connect to, the user id, the password, etc.
Tip
The application at this point checks the contents of a couple of fields, and then creates a connection string from the values of the fields. It saves the connection string in the Session object. One new command is Response.Redirect. This command tells the client's browser to go to a different page. The result is that the client will click the connect button and their browser will refresh with a page to view the contacts in the contacts table.