更新行在我使用字符串时有效,但在使用整数时无效

Updating a row works when I use a string but not when Integer

我正在使用从文本框获取的数据更新访问数据库。如果我清除一个带字符串的文本框,更新工作正常,但如果我清除一个带整数的文本框,我会收到此错误消息

Failed to convert parameter value from a String to a Int32

这是我的代码:

 cmd.Parameters.Add("@inOrOut", OleDbType.VarWChar).Value = tbPatientStatus.Text;
 cmd.Parameters.Add("@BedID", OleDbType.Integer).Value = tbPatientBedID.Text;

根据下面的评论,我已经尝试过但没有成功

if (string.IsNullOrEmpty(tbPatientBedID.Text))
{
  cmd.Parameters.Add("@BedID", OleDbType.Integer).Value = 0;
}
else
{
  cmd.Parameters.Add("@BedID", OleDbType.Integer).Value = tbPatientBedID.Text;
}
  int? BedId = null;
  int m_BedID = 0;
  if(Int32.TryParse(tbPatientBedID.Text.Trim(), out m_BedID))
  {
     BedId = m_BedID;
  }      

  cmd.Parameters.Add("@inOrOut", OleDbType.VarWChar).Value = tbPatientStatus.Text;
  cmd.Parameters.Add("@BedID", OleDbType.Integer).Value = BedID;

添加参数类型时 OleDbType.Integer 提供的值类型也应为 integer..

使用 tryparse 获取 int 值的小例子。

        int value;
        if !(int.TryParse(tbPatientBedID.Text, out value))
            value = 0;

        cmd.Parameters.Add("@BedID", OleDbType.Integer).Value = value;