使用 null 或 int 值更新 SQL

本文关键字:更新 SQL int null 使用 | 更新日期: 2023-09-27 18:37:19

我希望能够使用 null 或 int 从 c# 更新表。 下拉列表将有一个 id 作为所选值或空字符串,如果它是一个空字符串,我想传递一个 null,否则我想要 id。我已经尝试过这个,但收到错误"输入字符串格式不正确"。谁能帮忙?谢谢

  var result = (from p in dc.Prices where p.price_ID == Id select p).FirstOrDefault();
  result.no_update = String.IsNullOrEmpty((grd.Rows[e.RowIndex].FindControl("ddlNoUpdate") as DropDownList).SelectedValue.ToString()) ? System.Data.SqlTypes.SqlInt32.Null.Value : int.Parse((grd.Rows[e.RowIndex].FindControl("ddlNoUpdate") as DropDownList).SelectedValue.ToString());
  dc.SubmitChanges();

使用 null 或 int 值更新 SQL

尝试使用此代码,它曾经对我有用

String insertedVal = (grd.Rows[e.RowIndex].FindControl("ddlNoUpdate") as DropDownList).SelectedValue.ToString()
result.no_update = String.IsNullOrWhiteSpace(insertedVal)
    ? (object)DBNull.Value : (object)insertedVal