我在看asp.net 揭秘第二版的时候,书中第六章里的c#代码为什么全都运行不起来!例如:
//DataObject.cs
using System;
using System.IO;namespace myComponents {
public class DataObject {
public void SaveOrder(string customer, string product, double unitPrice, int quantity, string state) {
string strPath = @"c:\orders.txt";
StreamWriter strmFile; strmFile = File.AppendText( strPath );
strmFile.Write( "customer: " + customer + Environment.NewLine );
strmFile.Write( "product: " + product + Environment.NewLine );
strmFile.Write( "unit price: " + unitPrice.ToString() + Environment.NewLine );
strmFile.Write( "quantity: " + quantity.ToString() + Environment.NewLine );
strmFile.Write( "state: " + state + Environment.NewLine );
strmFile.Write( "=============" + Environment.NewLine );
strmFile.Close();
}
}
}
//BizObject.cs
using System;namespace myComponents {
public class BizObject {
public void CheckOrder(string customer, string product, double unitPrice, int quantity, string state) {
if ( quantity <= 0 || quantity > 100 ) {
throw new ArgumentException( "Invalid Quantity!" );
} if ( state == "California" && product == "Hair Dryer" ) {
throw new ArgumentException( "Californians cannot own Hair Dryers!" );
} if ( state == "Washington" ) {
unitPrice += unitPrice * .06;
}
DataObject myDataObject = new DataObject();
myDataObject.SaveOrder( customer, product, unitPrice, quantity, state );
}
}
}
//OrderForm.aspx
<%@ Page Language="C#"%>
<%@ Import Namespace="myComponents" %><script runat=server>void Button_Click(object s, EventArgs e)
{
BizObject myBizObject = new BizObject();
if (IsValid)
{
try
{
myBizObject.CheckOrder(txtCustomer.Text, dropProduct.SelectedItem.Text, Convert.ToDouble(txtUnitPrice.Text), Convert.ToInt32(txtQuantity.Text), dropState.SelectedItem.Text );
}
catch (Exception expException)
{
lblError.Text = expException.Message;
}
}
}</Script><html>
<head><title>OrderForm.aspx</title></head>
<body><form Runat="Server"><h2>Enter an Order:</h2><asp:Label
id="lblError"
ForeColor="Red"
Font-Bold="True"
EnableViewState="False"
Runat="Server" /><p>
Customer Name:
<br>
<asp:TextBox
id="txtCustomer"
Runat="Server" /><asp:RequiredFieldValidator
ControlToValidate="txtCustomer"
Text="You must enter a customer name!"
Runat="Server" /><p>
Product:
<br>
<asp:ListBox
id="dropProduct"
Runat="Server">
<asp:ListItem Text="Hair Dryer" />
<asp:ListItem Text="Shaving Cream" />
<asp:ListItem Text="Electric Comb" />
</asp:ListBox><asp:RequiredFieldValidator
ControlToValidate="dropProduct"
Text="You must select a product!"
Runat="Server" /><p>
Unit Price:
<br>
<asp:TextBox
id="txtUnitPrice"
Runat="Server" /><asp:RequiredFieldValidator
ControlToValidate="txtUnitPrice"
Text="You must enter a unit price!"
Runat="Server" />
<p>
Quantity:
<br>
<asp:TextBox
id="txtQuantity"
Runat="Server" /><asp:RequiredFieldValidator
ControlToValidate="txtQuantity"
Text="You must enter a quantity!"
Runat="Server" /><asp:CompareValidator
ControlToValidate="txtQuantity"
Text="Quantity must be a number!"
Operator="DataTypeCheck"
Type="Integer"
Runat="Server" /><p>
Customer State:
<br>
<asp:DropDownList
id="dropState"
Runat="Server">
<asp:ListItem Text="California" />
<asp:ListItem Text="Nevada" />
<asp:ListItem Text="Washington" />
</asp:DropDownList><p>
<asp:Button
Text="Place Order"
OnClick="Button_Click"
Runat="Server" /></form></body>
</html>
所有的c#文件都已编译成了DLL文件,为什么会出现下列错误啊:
编译错误
说明: 在编译向该请求提供服务所需资源的过程中出现错误。请检查下列特定错误详细信息并适当地修改源代码。 编译器错误信息: CS0246: 找不到类型或命名空间名称“myComponents”(是否缺少 using 指令或程序集引用?)源错误: 行 1: <%@ Page Language="C#" %>
行 2: <%@ Import Namespace="myComponents" %>
行 3:
行 4: <script runat="Server">
//DataObject.cs
using System;
using System.IO;namespace myComponents {
public class DataObject {
public void SaveOrder(string customer, string product, double unitPrice, int quantity, string state) {
string strPath = @"c:\orders.txt";
StreamWriter strmFile; strmFile = File.AppendText( strPath );
strmFile.Write( "customer: " + customer + Environment.NewLine );
strmFile.Write( "product: " + product + Environment.NewLine );
strmFile.Write( "unit price: " + unitPrice.ToString() + Environment.NewLine );
strmFile.Write( "quantity: " + quantity.ToString() + Environment.NewLine );
strmFile.Write( "state: " + state + Environment.NewLine );
strmFile.Write( "=============" + Environment.NewLine );
strmFile.Close();
}
}
}
//BizObject.cs
using System;namespace myComponents {
public class BizObject {
public void CheckOrder(string customer, string product, double unitPrice, int quantity, string state) {
if ( quantity <= 0 || quantity > 100 ) {
throw new ArgumentException( "Invalid Quantity!" );
} if ( state == "California" && product == "Hair Dryer" ) {
throw new ArgumentException( "Californians cannot own Hair Dryers!" );
} if ( state == "Washington" ) {
unitPrice += unitPrice * .06;
}
DataObject myDataObject = new DataObject();
myDataObject.SaveOrder( customer, product, unitPrice, quantity, state );
}
}
}
//OrderForm.aspx
<%@ Page Language="C#"%>
<%@ Import Namespace="myComponents" %><script runat=server>void Button_Click(object s, EventArgs e)
{
BizObject myBizObject = new BizObject();
if (IsValid)
{
try
{
myBizObject.CheckOrder(txtCustomer.Text, dropProduct.SelectedItem.Text, Convert.ToDouble(txtUnitPrice.Text), Convert.ToInt32(txtQuantity.Text), dropState.SelectedItem.Text );
}
catch (Exception expException)
{
lblError.Text = expException.Message;
}
}
}</Script><html>
<head><title>OrderForm.aspx</title></head>
<body><form Runat="Server"><h2>Enter an Order:</h2><asp:Label
id="lblError"
ForeColor="Red"
Font-Bold="True"
EnableViewState="False"
Runat="Server" /><p>
Customer Name:
<br>
<asp:TextBox
id="txtCustomer"
Runat="Server" /><asp:RequiredFieldValidator
ControlToValidate="txtCustomer"
Text="You must enter a customer name!"
Runat="Server" /><p>
Product:
<br>
<asp:ListBox
id="dropProduct"
Runat="Server">
<asp:ListItem Text="Hair Dryer" />
<asp:ListItem Text="Shaving Cream" />
<asp:ListItem Text="Electric Comb" />
</asp:ListBox><asp:RequiredFieldValidator
ControlToValidate="dropProduct"
Text="You must select a product!"
Runat="Server" /><p>
Unit Price:
<br>
<asp:TextBox
id="txtUnitPrice"
Runat="Server" /><asp:RequiredFieldValidator
ControlToValidate="txtUnitPrice"
Text="You must enter a unit price!"
Runat="Server" />
<p>
Quantity:
<br>
<asp:TextBox
id="txtQuantity"
Runat="Server" /><asp:RequiredFieldValidator
ControlToValidate="txtQuantity"
Text="You must enter a quantity!"
Runat="Server" /><asp:CompareValidator
ControlToValidate="txtQuantity"
Text="Quantity must be a number!"
Operator="DataTypeCheck"
Type="Integer"
Runat="Server" /><p>
Customer State:
<br>
<asp:DropDownList
id="dropState"
Runat="Server">
<asp:ListItem Text="California" />
<asp:ListItem Text="Nevada" />
<asp:ListItem Text="Washington" />
</asp:DropDownList><p>
<asp:Button
Text="Place Order"
OnClick="Button_Click"
Runat="Server" /></form></body>
</html>
所有的c#文件都已编译成了DLL文件,为什么会出现下列错误啊:
编译错误
说明: 在编译向该请求提供服务所需资源的过程中出现错误。请检查下列特定错误详细信息并适当地修改源代码。 编译器错误信息: CS0246: 找不到类型或命名空间名称“myComponents”(是否缺少 using 指令或程序集引用?)源错误: 行 1: <%@ Page Language="C#" %>
行 2: <%@ Import Namespace="myComponents" %>
行 3:
行 4: <script runat="Server">
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货