C# dataadapter填充缺少参数
当我尝试执行此代码时,我收到以下错误。但我把它添加到了我的命令中。有人能指出被忽略的步骤吗?谢谢 过程或函数“usps_getContactDetails”需要未提供的参数“@aspContactID” SqlConnection conn=新的SqlConnection(GetConnString()); SqlCommand cmd=新的SqlCommand(“usps\U getContactDetails”,康涅狄格州)C# dataadapter填充缺少参数,c#,asp.net,C#,Asp.net,当我尝试执行此代码时,我收到以下错误。但我把它添加到了我的命令中。有人能指出被忽略的步骤吗?谢谢 过程或函数“usps_getContactDetails”需要未提供的参数“@aspContactID” SqlConnection conn=新的SqlConnection(GetConnString()); SqlCommand cmd=新的SqlCommand(“usps\U getContactDetails”,康涅狄格州) 执行SqlCommand并调用存储过程时,需要隐式地将SqlCom
执行
SqlCommand
并调用存储过程时,需要隐式地将SqlCommand
设置为storedProcess
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
}"
Create Procedure [dbo].[Example]
@Test as Varchar(100) = ''
As
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();
}
}