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:

  1. 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.

  2. Double-click on the connect button to open the code editor. The wizard adds the btnConnect_Click event.

  3. 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.
     private void btnConnect_Click(object sender,                             System.EventArgs e) {  string server = (txtServer.Text == "") ?   "localhost" : txtServer.Text;   string database = (txtDatabase.Text == "") ?   "csharpvqs" :   txtDatabase.Text;  } 
  4. 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.
     private void btnConnect_Click (object sender, System.EventArgs e) {    string server = (txtServer.Text == "")    ? "localhost" : txtServer.Text;    string database =    (txtDatabase.Text == "") ?    "csharpvqs" : txtDatabase.Text;  string ConnectionString =   "server=" + server +   ";uid=" + txtUserID.Text +   ";pwd=" + txtPassword.Text +   ";database=" + database + ";";   Session["ConnectionString"] =   ConnectionString;  } 
  5. 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 Response.Redirect tells the client browser not to display output for the current page, but to rather transfer to another page.
     private void btnConnect_Click (object sender, System.EventArgs e) {    string server = (txtServer.Text == "")    ? "localhost" : txtServer.Text;    string database =    (txtDatabase.Text == "") ?    "csharpvqs" : txtDatabase.Text;    string ConnectionString =    "server=" + server +    ";uid=" + txtUserID.Text +    ";pwd=" + txtPassword.Text +    ";database=" + database + ";";    Session["ConnectionString"] =    ConnectionString;  Response.Redirect("contacts.aspx");  } 

graphics/tick.gif 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.




C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net