我在SQL存储过程中有三个变量,两个为输入变量,一个为输出变量
@x,
@y,
@z output
我要现实在ASP.NET中@x+@y=@z样子的等式.就是用户输入变量@x和@y前台显示@z的值.
页面编译过了.可是还会报错.请高手指教下:
SQL存储过程:Create Procedure getOut
@x int,
@y int,
@z int Output
As
Set @z=@x+@y
Return @z
Go----------------------------------ASP.NET代码:using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class getOut : System.Web.UI.Page
{
String Output; protected void Page_Load(object sender, EventArgs e)
{
SqlConnection FConnection;
SqlCommand FCommand;
String FConnString; FConnString = "Server=(Local);Database=CMS;Uid=sa;Pwd=;";
FConnection = new SqlConnection(FConnString);
FConnection.Open(); FCommand = new SqlCommand();
FCommand.CommandType=CommandType.StoredProcedure;
FCommand.CommandText = "getOut";
FCommand.Parameters.Add("@x", SqlDbType.Int).Value = this.xValue.Text;
FCommand.Parameters.Add("@y", SqlDbType.Int).Value = this.yValue.Text;
FCommand.Parameters["@z"].Direction = ParameterDirection.Output;
Output = FCommand.Parameters["@z"].Value.ToString();
FCommand.Connection = FConnection;
FCommand.ExecuteNonQuery();
}
protected void Button1_Click(object sender, EventArgs e)
{
if(this.IsPostBack)
{
this.zValue.Text = Output;
}
}
}--------------------------------------
报错提示:此 SqlParameterCollection 中未包含带有 ParameterName“@z”的 SqlParameter。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.IndexOutOfRangeException: 此 SqlParameterCollection 中未包含带有 ParameterName“@z”的 SqlParameter。源错误:
行 30: FCommand.Parameters.Add("@x", SqlDbType.Int).Value = this.xValue.Text;
行 31: FCommand.Parameters.Add("@y", SqlDbType.Int).Value = this.yValue.Text;
行 32: FCommand.Parameters["@z"].Direction = ParameterDirection.Output;
行 33: Output = FCommand.Parameters["@z"].Value.ToString();
行 34: FCommand.Connection = FConnection;
@x,
@y,
@z output
我要现实在ASP.NET中@x+@y=@z样子的等式.就是用户输入变量@x和@y前台显示@z的值.
页面编译过了.可是还会报错.请高手指教下:
SQL存储过程:Create Procedure getOut
@x int,
@y int,
@z int Output
As
Set @z=@x+@y
Return @z
Go----------------------------------ASP.NET代码:using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class getOut : System.Web.UI.Page
{
String Output; protected void Page_Load(object sender, EventArgs e)
{
SqlConnection FConnection;
SqlCommand FCommand;
String FConnString; FConnString = "Server=(Local);Database=CMS;Uid=sa;Pwd=;";
FConnection = new SqlConnection(FConnString);
FConnection.Open(); FCommand = new SqlCommand();
FCommand.CommandType=CommandType.StoredProcedure;
FCommand.CommandText = "getOut";
FCommand.Parameters.Add("@x", SqlDbType.Int).Value = this.xValue.Text;
FCommand.Parameters.Add("@y", SqlDbType.Int).Value = this.yValue.Text;
FCommand.Parameters["@z"].Direction = ParameterDirection.Output;
Output = FCommand.Parameters["@z"].Value.ToString();
FCommand.Connection = FConnection;
FCommand.ExecuteNonQuery();
}
protected void Button1_Click(object sender, EventArgs e)
{
if(this.IsPostBack)
{
this.zValue.Text = Output;
}
}
}--------------------------------------
报错提示:此 SqlParameterCollection 中未包含带有 ParameterName“@z”的 SqlParameter。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.IndexOutOfRangeException: 此 SqlParameterCollection 中未包含带有 ParameterName“@z”的 SqlParameter。源错误:
行 30: FCommand.Parameters.Add("@x", SqlDbType.Int).Value = this.xValue.Text;
行 31: FCommand.Parameters.Add("@y", SqlDbType.Int).Value = this.yValue.Text;
行 32: FCommand.Parameters["@z"].Direction = ParameterDirection.Output;
行 33: Output = FCommand.Parameters["@z"].Value.ToString();
行 34: FCommand.Connection = FConnection;
解决方案 »
- asp.net后台如何获得前台HTML控件的值
- gridView怎样隐藏自动生成的“选择”,且单击行任意部分能获取到行数据
- ASP.NET快速入门教程运行实例出错,盼指教
- 如何自动注册ocx,不要说regsvr32,需要的是自动让ie注册,最好实施起来简单点的,如果需要delphi控件,或者是其他注册控件请发邮箱!
- 怎样给 DataGrid 中模板列中的复选框绑定事件呀?
- ※※Ajax问题:完全照例子做的,怎么老是出现这样的错误呢????
- [求助]在SQL_Server新建登陆用户时,在常规选项中的默认设置的数据库选项中我选了Master和选我针对的数据库效果是一样的。那是为什么??
- 从vs2002升级到2003时金额小数的位数都由以前的2位变成了4位,是不是.net中有什么设置?
- 写ASP.NET程序,是使用VS.NET还是其他工具?我看到的书籍没几本是介绍用VS.NET的
- 首页木马的问题,syeerzy(快乐永远*先天下之乐而乐*后天下之忧而忧*) 请进......
- 哪位高手知道调试和运行的时候得到的值不一样是怎么回事???
- 求一正则表达式
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 31: FCommand.Parameters.Add("@y", SqlDbType.Int).Value = this.yValue.Text;
行 32: FCommand.Parameters.Add("@z",SqlDbType.Int).Direction = ParameterDirection.Output;
行 33: Output = FCommand.Parameters["@z"].Value.ToString();
行 34: FCommand.Connection = FConnection;
行 35: FCommand.ExecuteNonQuery();
@x int,
@y int,
@z int Output
As
Set @z=@x+@y
Go----------------------------------ASP.NET代码:using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class getOut : System.Web.UI.Page
{
String Output; protected void Page_Load(object sender, EventArgs e)
{
SqlConnection FConnection;
SqlCommand FCommand;
String FConnString; FConnString = "Server=(Local);Database=CMS;Uid=sa;Pwd=;";
FConnection = new SqlConnection(FConnString);
FConnection.Open(); FCommand = new SqlCommand();
FCommand.CommandType=CommandType.StoredProcedure;
FCommand.CommandText = "getOut";
FCommand.Parameters.Add("@x", SqlDbType.Int).Value = this.xValue.Text;
FCommand.Parameters.Add("@y", SqlDbType.Int).Value = this.yValue.Text;
FCommand.Parameters.["@x"].Direction = ParameterDirection.Input;
FCommand.Parameters.["@y"].Direction = ParameterDirection.Intput;
FCommand.Parameters.Add("@z", SqlDbType.VarChar,30);
FCommand.Parameters["@z"].Direction = ParameterDirection.Output;
Output = FCommand.Parameters["@z"].Value.ToString();
FCommand.Connection = FConnection;
FCommand.ExecuteNonQuery();
}
protected void Button1_Click(object sender, EventArgs e)
{
if(this.IsPostBack)
{
this.zValue.Text = Output;
}
}
}试一下这样行吗??自己再调试一下吧
Create Procedure getOut
@x int,
@y int,
--@z int Output--这个删掉
As
declare @z integer--局部变量
Set @z=@x+@y
Return @z
Go
看看我得代码
cmd = new SqlCommand("Admin_Article_Manage_Me", conn);
Mydataset = new DataSet();
ada = new SqlDataAdapter();
ada.SelectCommand = cmd;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@Editor", SqlDbType.VarChar));
cmd.Parameters["@Editor"].Value = Editor;
cmd.Parameters.Add(new SqlParameter("@PageSize", SqlDbType.Int));
cmd.Parameters["@pagesize"].Value = int1;
cmd.Parameters.Add(new SqlParameter("@PageNo", SqlDbType.Int));
cmd.Parameters["@PageNo"].Value = int2;
cmd.Parameters.Add(new SqlParameter("@RetrunValues", SqlDbType.Int));
cmd.Parameters["@RetrunValues"].Direction = ParameterDirection.ReturnValue;
ada.Fill(Mydataset);
object[] obj = new object[2];
obj[0] = Mydataset;
obj[1] = ada.SelectCommand.Parameters["@RetrunValues"].Value; 取回值得代码
obj[1] = ada.SelectCommand.Parameters["@RetrunValues"].Value;
这个是COPY我得程序得代码,肯定行得。
FCommand.Connection = FConnection;
FCommand.ExecuteNonQuery();
Output = FCommand.Parameters["@z"].Value.ToString();
注意顺序,执行后才能够获得@z的值吧,你还忘了添加@z参数
@z.Direction = ParameterDirection.Output ;Output = FCommand.Parameters["@z"].Value.ToString();
FCommand.CommandType=CommandType.StoredProcedure;
FCommand.CommandText = "getOut";
FCommand.Parameters.Add(new SqlParameter("@x",SqlDbType.Int));
FCommand.Parameters.Add(new SqlParameter("@y",SqlDbType.Int));
FCommand.Parameters.Add(new SqlParameter("@z",SqlDbType.Int));
FCommand.Parameters["@x"].Value = this.xValue.Text;
FCommand.Parameters["@y"].Value = this.yValue.Text;
FCommand.Parameters["@z"].Direction = ParameterDirection.Output;
Output = FCommand.Parameters["@z"].Value.ToString();
FCommand.Connection = FConnection;
FCommand.ExecuteNonQuery();-----------
还是有错:System.NullReferenceException: 未将对象引用设置到对象的实例。
Output = FCommand.Parameters["@z"].Value.ToString();//红色的行