user1801525
user1801525

Reputation: 125

Update SQL with a null or int value

I want to be able to update the table from c# with either a null or int. The dropdown will either have an id as the selected value or empty string, if it's an empty string I want to pass a null otherwise I want the id. I've tried this but getting an error "Input string was not in a correct format." Can anyone help? Thanks

  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();

Upvotes: 1

Views: 1024

Answers (1)

Mahmoud Hashim
Mahmoud Hashim

Reputation: 550

Try using this code, it worked for me once

String insertedVal = (grd.Rows[e.RowIndex].FindControl("ddlNoUpdate") as DropDownList).SelectedValue.ToString()

result.no_update = String.IsNullOrWhiteSpace(insertedVal)
    ? (object)DBNull.Value : (object)insertedVal

Upvotes: 1

Related Questions