c# code 39 C HA PTER 11 USE O F XML I N T HE .NET F RAME WO RK in C#.NET

Creation Code 3 of 9 in C#.NET C HA PTER 11 USE O F XML I N T HE .NET F RAME WO RK

C HA PTER 11 USE O F XML I N T HE .NET F RAME WO RK
Creating USS Code 39 In C#.NET
Using Barcode creator for VS .NET Control to generate, create Code 39 Extended image in VS .NET applications.
www.OnBarcode.com
ANSI/AIM Code 39 Decoder In Visual C#.NET
Using Barcode recognizer for VS .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
The <appSettings> section of web.config allows you to store such application-specific settings. You can then read these settings in your source code. Tomorrow if the settings change, you need to change just the web.config file and not the source code. Let s modify our Contact Us web form to use application configuration settings. Open the same website by choosing File Open Web Site from the menu. Open the web.config file in the IDE and modify the <appSettings> section as shown in Listing 11-20. Listing 11-20. Storing Values in the <appSettings> Section <appSettings> <add key="host" value="localhost"/> <add key="email" value="you@yourdomain.com"/> </appSettings> The <appSettings> section can contain one or more <add> elements. The <add> element has two attributes: The key attribute defines a key with which the value will be accessed in the code. The value attribute specifies the actual value of the key. In our example, we defined two keys: host and email. The former key stores the value of the SMTP host, and the latter stores your email address. Now open the code-behind web form and modify the Click event handler of the Submit button as shown in Listing 11-21. Listing 11-21. Retrieving Values from the <appSettings> Section protected void Button1_Click(object sender, EventArgs e) { string host = ConfigurationManager.AppSettings["host"]; string email = ConfigurationManager.AppSettings["email"]; SmtpClient client = new SmtpClient(host); client.Credentials = CredentialCache.DefaultNetworkCredentials; MailMessage msg = new MailMessage(); msg.From = new MailAddress(TextBox2.Text); msg.To.Add(email); msg.Subject = TextBox3.Text; msg.Body = "[" + DropDownList1.SelectedItem.Text + "]" + TextBox4.Text + "\r\n" + TextBox1.Text + "\r\n" + TextBox5.Text; client.Send(msg); Label9.Text = "Your message has been sent. Thank you!"; } Observe the code marked in bold. The code uses a class called ConfigurationManager, which resides in the System.Configuration namespace. By default System.Configuration is imported in the code-behind. The AppSettings property of the ConfigurationManager class exposes the entire <appSettings> section as a NameValueCollection. You can access individual values by using an index or a key name, though it is more common to access them by using key names.
Make EAN128 In C#
Using Barcode generator for Visual Studio .NET Control to generate, create UCC-128 image in .NET framework applications.
www.OnBarcode.com
PDF 417 Maker In C#.NET
Using Barcode encoder for .NET Control to generate, create PDF 417 image in Visual Studio .NET applications.
www.OnBarcode.com
CHAPTER 11 US E OF XML IN THE .N ET FRA MEWORK
Printing Code 3 Of 9 In Visual C#.NET
Using Barcode creator for .NET Control to generate, create Code 39 image in .NET applications.
www.OnBarcode.com
ECC200 Drawer In C#.NET
Using Barcode drawer for .NET Control to generate, create DataMatrix image in VS .NET applications.
www.OnBarcode.com
The code retrieves the values of two keys host and email and stores them in a string variable. The constructor of the SmtpClient class now accepts the value stored in the host string variable instead of a hard-coded value. Similarly, the Add() method accepts the value stored in the email string variable and not a hard-coded value. If you run the application, you should get the results as before, but now you are free to change the host name and email address without touching the source code.
Paint Barcode In C#
Using Barcode maker for .NET framework Control to generate, create Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Encode 2 Of 7 Code In C#
Using Barcode generator for VS .NET Control to generate, create USS Codabar image in .NET applications.
www.OnBarcode.com
Storing and Retrieving Database Connection Strings
Code-39 Encoder In Java
Using Barcode creation for Java Control to generate, create ANSI/AIM Code 39 image in Java applications.
www.OnBarcode.com
Code 3/9 Recognizer In Java
Using Barcode decoder for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Storing database connection strings outside the source code is probably the most common configuration task. ASP.NET provides a special section of web.config to store database connection strings called <connectionStrings>. The <connectionStrings> section allows you to store one or more database connection strings that can be retrieved later in your code. To retrieve the connection strings stored in the <connectionStrings> section, you again need to use the ConfigurationManager class. To illustrate the use of the <connectionStrings> section, you will develop a simple employee listing web form. The web form will display a list of employees in a GridView control. To begin, create a new website by using Visual Studio. Add a web.config file to the website by using the Add New Item dialog box (Figure 11-22).
Code 128 Code Set B Creator In None
Using Barcode generation for Microsoft Excel Control to generate, create Code 128 Code Set A image in Excel applications.
www.OnBarcode.com
Creating USS Code 39 In VS .NET
Using Barcode generator for VS .NET Control to generate, create USS Code 39 image in .NET applications.
www.OnBarcode.com
Figure 11-22. Adding a web.config file
USS Code 128 Creation In VB.NET
Using Barcode encoder for .NET Control to generate, create Code 128C image in Visual Studio .NET applications.
www.OnBarcode.com
Decode European Article Number 13 In Visual Basic .NET
Using Barcode reader for .NET framework Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
C HA PTER 11 USE O F XML I N T HE .NET F RAME WO RK
Decoding Barcode In Java
Using Barcode Control SDK for Java Control to generate, create, read, scan barcode image in Java applications.
www.OnBarcode.com
Barcode Drawer In Visual Basic .NET
Using Barcode generator for .NET framework Control to generate, create Barcode image in VS .NET applications.
www.OnBarcode.com
Open the web.config file in the IDE and modify the <connectionStrings> section as shown in Listing 11-22. Listing 11-22. Adding a Connection String to the <connectionStrings> Section <connectionStrings> <add name="connectionstring" connectionString="data source=.;initial catalog=Northwind; integrated security=true" providerName="System.Data.SqlClient"/> </connectionStrings> The <connectionStrings> section can contain one or more <add> elements, each defining a database connection string: The name attribute of the <add> element defines a name for that connection string. This name is used later to access the connection string. The connectionString attribute specifies the actual database connection string. Finally, the providerName attribute indicates the .NET data provider that can be used to communicate with the database. Now open the default web form and drag and drop a GridView control onto it. Then type in the code shown in Listing 11-23 in the Page_Load event of the web form. Listing 11-23. Retrieving the Connection String protected void Page_Load(object sender, EventArgs e) { string strConn= ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString; SqlDataAdapter da = new SqlDataAdapter("SELECT EmployeeID,FirstName, LastName FROM Employees", strConn); DataSet ds = new DataSet(); da.Fill(ds, "employees"); GridView1.DataSource = ds; GridView1.DataBind(); } The code uses the ConfigurationManager class to retrieve the connection string value. The ConnectionStrings collection can be accessed by using an index or a connection string name. In our example, we access it with a name.
GS1-128 Drawer In None
Using Barcode generator for Font Control to generate, create EAN / UCC - 13 image in Font applications.
www.OnBarcode.com
ANSI/AIM Code 39 Printer In None
Using Barcode encoder for Office Word Control to generate, create Code 3 of 9 image in Office Word applications.
www.OnBarcode.com
GTIN - 128 Generation In Visual Studio .NET
Using Barcode drawer for ASP.NET Control to generate, create GS1-128 image in ASP.NET applications.
www.OnBarcode.com
Read Code 3 Of 9 In None
Using Barcode recognizer for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Copyright © OnBarcode.com . All rights reserved.