dataadapter填充缺少的参数

时间:2012-12-14 13:54:33

标签: c# asp.net

当我尝试执行此代码时,我收到以下错误。但我把它添加到我的命令中。有人能指出被忽视的一步吗?感谢。

过程或函数'usps_getContactDetails'需要参数'@aspContactID',这是未提供的。

SqlConnection conn = new SqlConnection(GetConnString());             SqlCommand cmd = new SqlCommand(“usps_getContactDetails”,conn);

        SqlParameter parmContactID = new SqlParameter("@aspContactID", Convert.DBNull);
        cmd.Parameters.Add(parmContactID);
        parmContactID.Direction = ParameterDirection.Input;

        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd;

        conn.Open();
        DataSet cusDS = new DataSet();
        da.Fill(cusDS, "Contacts");

2 个答案:

答案 0 :(得分:0)

在执行SqlCommand并调用存储过程时,您需要明确将SqlCommand设置为StoredProcedure

using(SqlConnection con = new SqlConnection(""))
{
  //Set up your command
  SqlCommand cmd = new SqlCommand("[Procedure]", con);
  cmd.CommandType = CommandType.StoredProcedure;
  //Add your parameters
  cmd.Parameters.AddWithValue("@aspContactID", "");
  //Declare your data adapter
  SqlDataAdapter sda = new SqlDataAdapter(cmd);
  DataSet ds = new DataSet();
  sda.Fill(ds, "Contacts");
}

按照上述格式,你应该没问题。由于以下两个原因之一,您的过程无法正常工作,您错过了代码行,这些代码使您的代码在这种情况下工作为cmd.CommandType = CommandType.StoredProcedure;,或者因为您的参数为DBNull,程序说它不会对该参数有任何认可。如果存储过程中的参数可以为null或为空,请执行以下操作:

Create Procedure [dbo].[Example]

@Test as Varchar(100) = ''

As

答案 1 :(得分:0)

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    private void OpenCon()
    {
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["DbPrepConnectionString"].ConnectionString.ToString());
        try
        {
            con.Open();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }



    private void SubmitData()
    {
        OpenCon();
        string sp = "sp_InsertRecord";
        cmd = new SqlCommand(sp, con);

        cmd.CommandType = CommandType.StoredProcedure;
        //add parameters...
        cmd.Parameters.Add(new SqlParameter("@Name", SqlDbType.VarChar, 50));
        cmd.Parameters.Add(new SqlParameter("@UserId", SqlDbType.Int));
        cmd.Parameters.Add (new SqlParameter ("@ProductName",SqlDbType .VarChar,50));
        cmd.Parameters.Add(new SqlParameter("@Price", SqlDbType.Money));

        //set paarameters....
        cmd.Parameters["@Name"].Value = txtName.Text.ToString();
        cmd.Parameters["@UserId"].Value = txtUserId.Text.ToString();
        cmd.Parameters["@ProductName"].Value = txtProductName.Text.ToString();
        cmd.Parameters["@Price"].Value = txtPrice.Text.ToString();
        cmd.ExecuteNonQuery();
        lblMessage.Text = "data inserted successfully";
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        SubmitData();
    }

    private void FindData()
    {
        OpenCon();
        string s = "sp_FindRecord";
        cmd = new SqlCommand(s, con);
        cmd.Parameters.Add(new SqlParameter("@Id", SqlDbType.Int));
        cmd.Parameters["@Id"].Value = txtName.Text.ToString();
        cmd.CommandType = CommandType.StoredProcedure;
        ad = new SqlDataAdapter(cmd);
        ds = new DataSet();
        ad.Fill(ds);
        dt = ds.Tables[0];
        currow = 0;

        FillControls();

    }

    private void FillControls()
    {
        txtOrderId.Text = dt.Rows[currow].ItemArray[0].ToString();
        txtUserId.Text = dt.Rows[currow].ItemArray[1].ToString();
        txtProductName.Text = dt.Rows[currow].ItemArray[2].ToString();
        txtPrice.Text = dt.Rows[currow].ItemArray[3].ToString();
    }

    protected void btnFind_Click(object sender, EventArgs e)
    {
        FindData();
    }

}

}“