我编写的程序选择并应该根据ID
从gridview中删除一行,但它会抛出一个异常,@ComplainantTypeID
没有提供给存储过程,但我检查了调试,它发送了增加价值,但它仍然会引发异常。
代码:
protected void btnDeletePopUp_Click(object sender, EventArgs e)
{
try
{
int ComplainantTypeID = Convert.ToInt32(txtSelectedID.Text.Trim());
ManageComplainantType mngComplainantType = new ManageComplainantType();
mngComplainantType.Delete(ComplainantTypeID);
Clear(txtName, txtSelectedID);
HiddenFieldSetMessage.Value = "Delete";
HiddenFieldShowMessage.Value = "True";
}
catch (Exception)
{
HiddenFieldSetMessage.Value = "NotDeleted";
HiddenFieldShowMessage.Value = "True";
}
}
BLL中的删除功能:
public void Delete(int ComplainantTypeID)
{
SqlCommand cmd = new SqlCommand("DeleteComplainantTypes_SP", DataBaseConnection.OpenConnection());
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter pComplainantTypeID = new SqlParameter("@ComplainantTypeID", ComplainantTypeID);
cmd.Parameters.Add(pComplainantTypeID);
cmd.ExecuteNonQuery();
DataBaseConnection.CloseConnection();
}
存储过程:
PROCEDURE [dbo].[DeleteComplainantTypes_SP]
@ComplainantTypeID smallint
AS
BEGIN
BEGIN TRY
DELETE FROM ComplainantTypes WHERE ComplainantTypeID = @ComplainantTypeID
END TRY
BEGIN CATCH
SELECT ERROR_MESSAGE() AS ErrorMessage
END CATCH
END
答案 0 :(得分:0)
您必须显示异常以获得更好的帮助,但问题实际上是数据类型。您通过int
但预期smallint
。查看SQL Server Data Type Mappings文章以选择适当的数据类型。
<强>更新强>
你无法将int转换为smallint
,你必须传递short
类型的参数,该参数映射到smallint
sql类型。
声明
public void Delete(short ComplainantTypeID)
而不是
public void Delete(int ComplainantTypeID)
并保持SP不变。
答案 1 :(得分:0)
尝试
cmd.Parameters.AddWithValue("@ComplainantTypeID", value);
或
SqlParameter pComplainantTypeID = new SqlParameter("@ComplainantTypeID",SqlDbType.SmallInt,value);
答案 2 :(得分:0)
试试这个
sqlCommand.Parameters.Add("@ComplainantTypeID", SqlDbType.Int).Value = ComplainantTypeID;