我在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;

解决方案 »

  1.   

    FCommand.Parameters.Add( "@z",SqlDbType.Int ).Direction = ParameterDirection.Output ;Output = FCommand.Parameters["@z"].Value.ToString();
      

  2.   

    另外,你的存储中Return @z这句可以省略.因为@z是输出参数.加这句没必要.
      

  3.   

    照你的做了还是不行.现在的错误是
    异常详细信息: 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();
     
      

  4.   

    另外你说没有必要Return @z.但是如果我在存储过程中有两个输出参数那返回的是哪个呢.应该怎么写呢.我刚开始存储过程,请指教.谢谢了.
      

  5.   

    FCommand.Parameters.Add( "@z",SqlDbType.Int ).Direction = ParameterDirection.Output ;Output = FCommand.Parameters["@z"].Value.ToString();
      

  6.   

    Create Procedure getOut
    @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;
            }
        }
    }试一下这样行吗??自己再调试一下吧
      

  7.   

    方向错了。应该用“返回”
    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; 
      

  8.   

    你这么写得??
    这个是COPY我得程序得代码,肯定行得。
      

  9.   

    FCommand.Parameters.Add( "@z",SqlDbType.Int ).Direction = ParameterDirection.Output ;  // 添加@z参数
    FCommand.Connection = FConnection;
    FCommand.ExecuteNonQuery();
    Output = FCommand.Parameters["@z"].Value.ToString();
    注意顺序,执行后才能够获得@z的值吧,你还忘了添加@z参数
      

  10.   

    FCommand.Parameters.Add( "@z",SqlDbType.Int );
    @z.Direction = ParameterDirection.Output ;Output = FCommand.Parameters["@z"].Value.ToString();
      

  11.   

    FCommand = new SqlCommand();
            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();//红色的行