The document contains 7 code samples from different programming domains:
1. Data modeling and user interface design
2. SQL stored procedure
3. C# regular expressions
4. C# error logging to file
5. C#/ADO.NET method to retrieve a library member
6. XML web configuration file
7. ASP.NET web form with controls and validation
The samples provide snippets of code related to various programming tasks like data access, validation, error handling, and user interfaces.
3. Sample 2 (SQL INSERT Stored Procedure):
GO
/****** Object: StoredProcedure [dbo].[GetMemberByItem] Script Date:
01/22/2008 14:33:53 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Nii Amah Hesse>
-- Create date: <Create Date,,12/20/2007>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[GetMemberByItem]
-- Add the parameters for the stored procedure here
@isbn int
,@copy_no smallint
AS
IF NOT EXISTS
(SELECT * FROM dbo.copy WHERE isbn = @isbn AND copy_no = @copy_no)
BEGIN
RETURN -17 --ItemNotFound
END
IF NOT EXISTS
(SELECT member_no FROM dbo.loan WHERE isbn = @isbn AND copy_no = @copy_no)
BEGIN
RETURN -19 --ItemNotOnLoan
END
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
IF EXISTS
(SELECT a.member_no FROM dbo.adult AS a
INNER JOIN dbo.loan AS l
ON a.member_no = l.member_no
WHERE l.isbn = @isbn AND l.copy_no = @copy_no)
BEGIN
SELECT
m.member_no
,m.lastname
,m.firstname
,m.middleinitial
,a.street
,a.city
,a.state
,a.zip
,a.phone_no
,a.expr_date
FROM dbo.member AS m
INNER JOIN dbo.adult AS a
ON m.member_no = a.member_no
INNER JOIN dbo.loan AS l
ON m.member_no = l.member_no
WHERE l.isbn = @isbn AND l.copy_no = @copy_no
END
ELSE
4. SELECT
m.member_no
,m.lastname
,m.firstname
,m.middleinitial
,a.street
,a.city
,a.state
,a.zip
,a.phone_no
,a.expr_date
,j.adult_member_no
,j.birth_date
FROM dbo.juvenile AS j
INNER JOIN dbo.adult AS a
ON j.adult_member_no = a.member_no
INNER JOIN dbo.member AS m
ON m.member_no = j.member_no
INNER JOIN dbo.loan AS l
ON l.member_no = j.member_no
WHERE l.isbn = @isbn AND l.copy_no = @copy_no
IF @@error <> 0
BEGIN
RETURN -18 --NoSuchMember
END
END
Sample 3 (C# Regular Expressions):
private void ZipCodeTextBox_Validating(object sender, CancelEventArgs e)
{Regex zipExp = new Regex(@"^d{5}$");
if (!zipExp.IsMatch(((Control)sender).Text))
{Regex zipExp2 = new Regex(@"^d{5}(-d{4})$");
if (!zipExp2.IsMatch(((Control)sender).Text))
{errorProvider1.SetError(ZipCodeTextBox, "ZipCode format: ##### or #####-####");
e.Cancel = true;}}}
Sample 4 (C# Error File Logging):
private void PrintError(string myErrorMessage, SystemException se)
{
string mydatetime = "Date" + DateTime.Today.Year.ToString();
mydatetime = mydatetime + "_" + DateTime.Today.Month.ToString();
mydatetime = mydatetime + "_" + DateTime.Today.Day.ToString();
mydatetime = mydatetime + "Time" + DateTime.Now.Hour.ToString();
mydatetime = mydatetime + "_" + DateTime.Now.Minute.ToString();
mydatetime = mydatetime + "_" + DateTime.Now.Second.ToString();
string myfile = @"C:error_" + mydatetime + ".txt";
StreamWriter sw = File.CreateText(myfile);
sw.WriteLine("Error Date: " + DateTime.Today.ToShortDateString());
sw.WriteLine("Error Time: " + DateTime.Now.ToLongTimeString());
sw.WriteLine("Custom Message: " + myErrorMessage);
sw.WriteLine("Message: " + se.Message);
sw.WriteLine("Source: " + se.Source);
sw.Close();
}
Sample 5 (C#/ADO.NET: Get Member Method):
#region GetMember Method
6. elder.ExpirationDate = (DateTime)libReader["expr_date"];
elder.FirstName = libReader["firstname"].ToString();
elder.LastName = libReader["lastname"].ToString();
elder.MemberID = (short)libReader["member_no"];
elder.MiddleInitial =
libReader["middleinitial"].ToString();
elder.PhoneNumber = libReader["phone_no"].ToString();
elder.State = libReader["state"].ToString();
elder.Street = libReader["street"].ToString();
elder.ZipCode = libReader["zip"].ToString();
}
}
if (Convert.ToInt16(libCommand.Parameters["@ReturnVal"].Value) !
= 0)
{
LibraryException ex = new LibraryException();
ex =
ex.ErrCodeGenerator(Convert.ToInt16(libCommand.Parameters["@ReturnVal"].Value));
if (ex.OtherMemberID > 0)
{
throw new LibraryException(ex.LibraryErrorCode,
ex.OtherMemberID);
}
else
{
throw new LibraryException(ex.LibraryErrorCode);
}
}
if (adult == true)
{
return elder;
}
else
{
return youth;
}
}
catch (LibraryException ex)
{
if (ex.OtherMemberID > 0)
{
throw new LibraryException(ex.LibraryErrorCode,
ex.OtherMemberID);
}
else
{
throw new LibraryException(ex.LibraryErrorCode);
}
}
finally
{
//Close Connection
if (!libReader.IsClosed)
{
libReader.Close();
}
if (libCon.State != ConnectionState.Closed)
{
libCon.Close();
}
}
}
7. #endregion
Sample 6 (Web Service Web Config (XML)):
<?xml version="1.0" encoding="utf-8"?>
<!--
Note: As an alternative to hand editing this file you can use the
web admin tool to configure settings for your application. Use
the Website->Asp.Net Configuration option in Visual Studio.
A full list of settings and comments can be found in
machine.config.comments usually located in
WindowsMicrosoft.NetFrameworkv2.xConfig
-->
<configuration>
<configSections>
<section name="microsoft.web.services3"
type="Microsoft.Web.Services3.Configuration.WebServicesConfiguration,
Microsoft.Web.Services3, Version=3.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35" />
</configSections>
<appSettings />
<connectionStrings>
<add name="library" connectionString="Data Source=localhost;Initial
Catalog=library;Integrated Security=True" />
</connectionStrings>
<system.web>
<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
-->
<compilation debug="true">
<assemblies>
<add assembly="Microsoft.Web.Services3, Version=3.0.0.0,
Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
</compilation>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<authentication mode="Windows" />
<!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
-->
<customErrors mode="On" />
<webServices>
<soapExtensionImporterTypes>
<add type="Microsoft.Web.Services3.Description.WseExtensionImporter,
Microsoft.Web.Services3, Version=3.0.0.0, Culture=neutral,