1

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();
4
  • Have you tried just setting it to "null", and is no_update of nullable type? Commented Oct 29, 2013 at 23:27
  • I'm guessing SelectedValue is set incorrectly and it's not a Null/Empty String OR integer, instead it is another value. Put up a full code sample and we can help... Commented Oct 29, 2013 at 23:29
  • Check what you have in SelectedValue, it seems it's failing when you try to convert it to int Commented Oct 29, 2013 at 23:49
  • I have edited your title. Please see, "Should questions include “tags” in their titles?", where the consensus is "no, they should not". Commented Oct 30, 2013 at 6:01

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.