To Lecture Notes
IT 230 -- 4/27/09
Review Questions
- Where should text files be placed that will be accessed
(read or write) using C#?
Ans: In http://ectweb2.cs.depaul.edu/ssmith/Database
- Explain how the StreamReader and StreamWriter objects are used.
Ans: Create a StreamReader or StreamWriter object using the full path name
to the file in the constructor call. Then read lines with a StreamReader
using the ReadLine method; write lines with a StreamWriter object using
the WriteLine or Write methods.
- Which absolute path name should be used to write to files in the
Database folder?
Ans: e:/ectserver/ssmith/Database/file.txt
- How can you configure a StreamWriter object to append output to the
end of an existing file?
Ans: Create the StreamWriter object like this:
StreamWriter sw = new StreamWriter(path, true);
where true means append.
- Write C# code using Visual Studio to read the first 3 lines from the text file
C:/it230-ssmith/input.txt and place them in the textboxes txtLine1, txtLine2 and
txtLine3.
public void btnSubmit_Click(object source, EventArgs e)
{
StreamReader sr = new StreamReader(C:/it230-ssmith/input.txt);
txtLine1.Text = sr.ReadLine( );
txtLine2.Text = sr.ReadLine( );
txtLine3.Text = sr.ReadLine( );
}
- How do you submit an HTML form to an aspx page to be processed
by the server?
Ans: Put all controls to be submitted in a form with action set to
the URL where the page is to be posted and method = get or post.
Also include an input tag with class=submit.
- How does the aspx page in the previous example obtain the values
of the HTML controls?
Ans: Using Response.Params[controlName].
- Write an HTML page with three HTML textboxes and an
HTML submit button. These controls should be on a form that
is submitted to the page compute-sum.aspx using method=get.
This aspx page should compute the sum of the values in the first
two boxes and put that sum in the third textbox.
- What are the four classs of validation controls we discussed
last time?
ANs: RequiredFieldValidator, CompareValidator, RangeValidator,
RegularExpressionValidator.
The Import Tag
- The Import tag allows the C# statement
System.IO.StreamReader sr = new System.IO.StreamReader(input.txt);
to be rewritten as
StreamReader sr = new StreamReader(input.txt);
- System.IO is a namespace that tells where the class StreamReader
is located in the .Net class library.
- To avoid fully qualified .Net class names, place the following
Import tag immediately after the Page tag:
<%@ Import Namespace=System.IO %>
Posting to a Different ASP.Net Page
- See Example 33 (submit-form5.aspx), which posts its data to the new
page verify2.aspx.
Arrays in C#
Declare an unitinialized array of int of size 5:
int[ ] a = new int[5];
Declare an initalized array of int:
int[ ] a = {5, 3, 7, 5, 3, 5, 4};
Declare an unitinialized array of int of size 4:
int[ ] x = new double[4];
Declare an initalized array of int:
int[ ] x = {3.32, 1.69, 7.83};
Declare an unitinialized array of int of size 6:
int[ ] s = new string[6];
Declare an initalized array of int:
int[ ] s = {dog, cat, mouse};
The indices and values of the int array a:
Expression | Index | Value |
a[0] | 0 | 5 |
a[1] | 1 | 3 |
a[2] | 2 | 7 |
a[3] | 3 | 3 |
a[4] | 4 | 3 |
a[5] | 5 | 5 |
a[6] | 6 | 4 |
Obtaining the value of the fourth item in the int array a:
value = a[4];
Changing the value of the third item in the int array a:
a[3] = 17;
Examples 18 through 21 show how to use arrays to simplify
C# code.
li>
The Session and Application Collections
- A big problem with HTML and ASP.Net pages is that they are stateless:
they don't retain global variables across multiple page postings.
- Try Example 41 (lack-of-state.aspx). The global variable numberOfRefreshes
is set back to zero everytime a refresh occurs.
- A remedy to this problem is to use the Session collection.
- Variables of any class can be added to the Session collection.
- The session collection retains its information across multiple page postings
(refreshes and submits).
- See Example 42 (clickcounter4.aspx). The Session variable clicks retains its
value across multiple page postings.
- Examples of how to use the Session collection:
- Adding the int variable clicks to Session with initial value 0:
Session[clicks] = 0;
Retrieving the value of the int variable clicks from Session:
int n = (int) Session[clicks];
The value of clicks in Session will be null if clicks has not been already added.
If want to add clicks to Session, but are not sure if it has already been added,
use this construction:
if (Session[clicks] != null)
{
Session[clicks] = 0;
}
There is one Session collection for each browser instance that is
displaying the .aspx file.
By contrast, there is only one Application collection over all
browser instances that are displaying the .aspx file.
See Example 43 (clickcounter5.aspx) counts all the clicks of all
the browsers that are currently displaying this .aspx file.
(clickcounter4.aspx)
Application[clicks] is only reset back to zero when all browser
instances are closed and a new browser instance is opened.
See Example 44 (find-word1.aspx). Input an English word. If the word
is in the top 500 in frequency, its frequency rank will be displayed. The
top 500 words are read from the text file,
common-words.txt in the Database folder, everytime a new word is submitted.
Example 45 (find-word2.aspx) Input an English word. If the word
is in the top 500 in frequency, its frequency rank will be displayed. The
top 500 words are read only once from the text file,
common-words.txt in the Database folder, and loaded into an array in
the Session collection.
Project 4