• 全部
  • .NET Framework
  • ASP
  • Web Services
  • .NET互联网桌面应用
  • VB
  • 图表区
  • 分析与设计
  • 组件/控件开发
  • AppLauncher
  • 问答

ADO.NET:此 SqlParameterCollection 中未包含带有 ParameterName“@userage”的 SqlParameter。

Poco Carino 2020-10-20 10:11:55
cunchuguocheng.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="cunchuguocheng.aspx.cs" Inherits="cunchuguocheng" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
ID:<asp:TextBox ID="txt_userid" runat="server"></asp:TextBox>
<br />
姓名:<asp:TextBox ID="txt_username" runat="server"></asp:TextBox>
<br />
年龄:<asp:TextBox ID="txt_userage" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="提交" OnClick="Button1_Click" />

<br />
<asp:Label ID="Label1" runat="server" ></asp:Label>

</div>
</form>
</body>
</html>

cunchuguocheng.aspx.cs:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class cunchuguocheng : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source = (local); Initial Catalog = user; Integrated Security = True");
SqlCommand com =new SqlCommand("insertuser",con);
com.CommandType = CommandType.StoredProcedure;
//先清空参数数组,然后再逐项为存储过程中的变量赋值
com.Parameters.Clear();
com.Parameters.Add(new SqlParameter("@userid", SqlDbType.Char, 5));
com.Parameters["@userid"].Value = txt_userid.Text;
com.Parameters.Add(new SqlParameter("@username", SqlDbType.Char, 50));
com.Parameters["@username"].Value = txt_username.Text;
com.Parameters.Add(new SqlParameter("@userid", SqlDbType.Int, 20));
com.Parameters["@userage"].Value = int.Parse(txt_userage.Text);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
int records = Convert.ToInt32(com.ExecuteNonQuery());
if (records > 0)
{
Label1.Text = "添加成功";
}
else
{
Label1.Text = "添加失败";
}
com.Dispose();
if (con.State == ConnectionState.Open)
{
con.Close();

}
}
}
不运行下面两条语句就正确:说明这个年龄这里是有问题的,把有关年龄的全部程序都删除,就会正确运行。
//com.Parameters.Add(new SqlParameter("@userid", SqlDbType.Int, 20));
// com.Parameters["@userage"].Value = int.Parse(txt_userage.Text);


SQL Server中的存储过程:
错误:
CREATE PROCEDURE insertuser
@userid char(5),
@username char(50),
@userage int
AS
BEGIN
insert into users(userid,username,userage) values(@userid,@username,@userage)
END
GO
删掉userage就正确:
CREATE PROCEDURE insertuser
@userid char(5),
@username char(50)
AS
BEGIN
insert into users(userid,username) values(@userid,@username)
END
GO

...全文
1061 点赞 收藏 4
写回复
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
韩老骥 2020-10-20
少写了一行:

com.Parameters.Add(new SqlParameter("@userage", SqlDbType.Int));
回复
正怒月神 版主 2020-10-20
com.Parameters.Add(new SqlParameter("@userid", SqlDbType.Int, 20)); com.Parameters["@userage"].Value = int.Parse(txt_userage.Text); 到底是userid 还是userage
回复
快乐起航2020 2020-10-20
缺少参数呗
回复
Poco Carino 2020-10-20
引用 1 楼 正怒月神 的回复:
com.Parameters.Add(new SqlParameter("@userid", SqlDbType.Int, 20)); com.Parameters["@userage"].Value = int.Parse(txt_userage.Text); 到底是userid 还是userage
应该是userage 我试一试 谢谢啦
回复
发帖
.NET技术社区
创建于2007-09-28

5.8w+

社区成员

.NET技术交流专区
申请成为版主
帖子事件
创建了帖子
2020-10-20 10:11
社区公告
暂无公告