指定的转换无效。
指定的转换无效。
错误提示
“/NetAlbum”应用程序中的服务器错误。
--------------------------------------------------------------------------------
指定的转换无效。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.InvalidCastException: 指定的转换无效。
源错误:
行 141:
行 142: // Calculate the CustomerID using Output Param from SPROC
行 143: int Id = (int)parameterR.Value;
行 144:
行 145: return Id;
源文件: c:\documents and settings\administrator\桌面\asp.net\netalbum\components\school.cs 行: 143
堆栈跟踪:
[InvalidCastException: 指定的转换无效。]
NetAlbum.School.AddSchool(Int32 MId, String SchoolName, Int32 Type) in c:\documents and settings\administrator\桌面\asp.net\netalbum\components\school.cs:143
NetAlbum.SchoolList.Button1_Click(Object sender, EventArgs e) in c:\documents and settings\administrator\桌面\asp.net\netalbum\school.aspx.cs:174
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1263
--------------------------------------------------------------------------------
版本信息: Microsoft .NET 框架版本:1.0.3705.0; ASP.NET 版本:1.0.3705.0
Code
<table class="EmptyTable" id="Table2" style="BORDER-COLLAPSE: collapse" cellPadding="0" width="100%" BcellSpacing="0">
<tr>
<td class="TableTop" vAlign="bottom" align="middle" height="18">
<img src="images/Default/bullet.gif"> 增 加 学 校</td>
</tr>
<tr>
<td vAlign="top" height="100">
<table border="0" cellpadding="0" cellspacing="0" style="BORDER-COLLAPSE: collapse" height="100">
<tr>
<td height="6" colspan="3"></td>
</tr>
<tr>
<td width="60" height="35" align="right">学校名称</td>
<td width="10" height="35"></td>
<td width="130" height="35">
<asp:TextBox id="SchoolName" runat="server" Width="100px"></asp:TextBox>
</td>
</tr>
<tr>
<td width="60" height="35" align="right">学校种类 </td>
<td width="10" height="35"></td>
<td width="130" height="35">
<select id="Type" style="FONT-SIZE: 9pt" size="1" name="Type" runat="server">
<option value="请选择">请选择</option>
<option value="1">小学</option>
<option value="2" selected>初中</option>
<option value="3">中专</option>
<option value="4">高中</option>
<option value="5">大学</option>
<option value="0">其他</option>
</select> </td>
</tr>
<tr>
<td colspan="3" height="24" align="middle">
<asp:Button id="Button1" runat="server" CssClass="Button" Text="增加">
</asp:Button>
</td>
</tr>
<tr>
<td colspan="3" height="6"></td>
</tr>
</table>
</td>
</tr>
</table>
存储过程
CREATE Procedure AddSchool
@MId Int,
@SchoolName nvarchar(20),
@Type int,
@R INT OUTPUT
AS
SELECT
[Id]
FROM
School
WHERE
MunicipalId=@MId
AND
SchoolName=@SchoolName
IF @@ROWCOUNT<1
SELECT R=0
ELSE
BEGIN
INSERT INTO School
(
MunicipalId,
SchoolName,
Type
)
VALUES
(
@MId,
@SchoolName,
@Type
)
SELECT @R=1
END
GO
CodeBehind
private void Button1_Click(object sender, System.EventArgs e)
{
if(SchoolName.Text=="")
{
FS Fail=new FS();
Fail.FHtml("","必须填写学校名称!");
}
if(SchoolName.Text.Length>20||SchoolName.Text.Length<5)
{
FS Fail=new FS();
Fail.FHtml("","学校名称长度不能小于5位,大于20位!");
}
School MySchool=new School();
int R=MySchool.AddSchool(int.Parse(Request.QueryString["Id"]),SchoolName.Text,int.Parse(Type.Value));
}
public class School
{
public int AddSchool(int MId,string SchoolName,int Type)
{
// Create Instance of Connection and Command Object
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand myCommand = new SqlCommand("AddSchool", myConnection);
// Mark the Command as a SPROC
myCommand.CommandType = CommandType.StoredProcedure;
// Add Parameters to SPROC
SqlParameter parameterMId = new SqlParameter("@MId", SqlDbType.Int,4);
parameterMId.Value = MId;
myCommand.Parameters.Add(parameterMId);
SqlParameter parameterSchoolName = new SqlParameter("@SchoolName", SqlDbType.NVarChar, 20);
parameterSchoolName.Value = SchoolName;
myCommand.Parameters.Add(parameterSchoolName);
SqlParameter parameterType = new SqlParameter("@Type", SqlDbType.Int,4);
parameterType.Value = Type;
myCommand.Parameters.Add(parameterType);
SqlParameter parameterR = new SqlParameter("@R", SqlDbType.Int, 4);
parameterR.Direction = ParameterDirection.Output;
myCommand.Parameters.Add(parameterR);
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
// Calculate the CustomerID using Output Param from SPROC
int Id = (int)parameterR.Value;
return Id;
}
}
问题点数:20、回复次数:3Top
1 楼zitiger(MSN聊天机器人:RobotBB001@hotmail.com)回复于 2003-05-04 11:45:00 得分 0
upTop
2 楼saucer(思归)回复于 2003-05-04 11:52:30 得分 20
are you sure your stored procedure is valid??
SELECT R=0
^^^^^^^^^^^^^^
===>
SET @R = 0Top




