15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1174 ❘ ChaPTer 40 cOre Asp.net<br />

Here you are using red, which will give you a display along the lines of<br />

Figure 40 - 11, in which June 12, 15, <strong>and</strong> 22 (2010) all contain events.<br />

With the addition of the date - selection logic, it is now impossible to select a<br />

day that is shown in red. If you attempt it, a later date is selected instead (for<br />

example, selecting June 15 results in the selection of June 16).<br />

adding events to the Database<br />

The submitButton_Click() event h<strong>and</strong>ler currently assembles a string from the figure 40-11<br />

event characteristics <strong>and</strong> displays it in the resultLabel control. To add an event<br />

to the database, you simply reformat the string created in a SQL INSERT query <strong>and</strong> execute it.<br />

Note that in the development environment that you are using, you don ’ t have to worry<br />

too much about security. Adding a SQL Server 2008 Express database via a web site<br />

solution <strong>and</strong> confi guring SqlDataSource controls to use it will automatically give<br />

you a connection string that you can use to write to the database. In more advanced<br />

situations, you might want to access resources using other accounts — for example,<br />

a domain account used to access a SQL Server instance elsewhere on a network.<br />

The capability to do this (via impersonation, COM+ Services, or other means) exists<br />

in ASP.<strong>NET</strong>, but is beyond the scope of this chapter. In most cases, confi guring the<br />

connection string appropriately is as complicated as things need to get.<br />

Much of the following code will therefore look familiar:<br />

protected void submitButton_Click(object sender, EventArgs e)<br />

{<br />

if (this.IsValid)<br />

{<br />

System.Text.StringBuilder sb = new System.Text.StringBuilder();<br />

foreach (ListItem attendee in attendeeList.Items)<br />

{<br />

if (attendee.Selected)<br />

{<br />

sb.AppendFormat(“{0} ({1}), “, attendee.Text, attendee.Value);<br />

}<br />

}<br />

sb.AppendFormat(“ <strong>and</strong> {0}”, nameBox.Text);<br />

string attendees = sb.ToString();<br />

try<br />

{<br />

System.Data.SqlClient.SqlConnection conn =<br />

new System.Data.SqlClient.SqlConnection(<br />

ConfigurationManager.ConnectionStrings[<br />

“MRBConnectionString”].ConnectionString);<br />

System.Data.SqlClient.SqlComm<strong>and</strong> insertComm<strong>and</strong> =<br />

new System.Data.SqlClient.SqlComm<strong>and</strong>(“INSERT INTO [Events] “<br />

+ “(Name, Room, AttendeeList, EventDate) VALUES (@Name, “<br />

+ “@Room, @AttendeeList, @EventDate)”, conn);<br />

insertComm<strong>and</strong>.Parameters.Add(<br />

“Name”, SqlDbType.VarChar, 255).Value = eventBox.Text;<br />

insertComm<strong>and</strong>.Parameters.Add(<br />

“Room”, SqlDbType.Int, 4).Value = roomList.SelectedValue;<br />

insertComm<strong>and</strong>.Parameters.Add(<br />

“AttendeeList”, SqlDbType.Text, 16).Value = attendees;<br />

insertComm<strong>and</strong>.Parameters.Add(<br />

“EventDate”, SqlDbType.DateTime, 8).Value =<br />

calendar.SelectedDate;<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!