1

I have read

What is the difference between null and System.DBNull.Value?

INSERT / UPDATE data using DBNull or null?

and similar Q&A but don't quite understand this. The problem:

tableAdapter allow's me storing null into not nullable fields (AllowDBNull = false).

What I need to check?

I can check for

string.Lenght == 0
string.IsNullOrEmpty(string)
string.text = ""

before update command but want to know why table adapter is letting me storing NULL

it's actually storing someTextBox.Text = "" when needed to throw exception null not allowed for some field.

I have exactly same problem Dataset allowing Null values even when AllowDBNull = False? but there are no solution.

Thanks

Community
  • 1
  • 1
Carlo
  • 151
  • 1
  • 3
  • 16

1 Answers1

0

I would need more code to see for sure what you are doing, but:

public static object GetValue(string value) {
  if (!String.IsNullOrEmpty(value)) {
    return value;
  }
  return DBNull.Value;
}

Would that solve your issues?

  • When and where to check that. I'm using auto generated (after drag and drop details from data sources) tableAdapter update command. I have textBox and if there are no text in textBox table is still being updated. TextBox is bounded to not nullable field. – Carlo Jan 13 '14 at 20:32
  • When inserting data for the first time don't have that problem. Exception is thrown `Cannot insert the value NULL into column 'pp_naziv', table 'winpro.dbo.pp'; column does not allow nulls. INSERT fails. The statement has been terminated.` But when editing existing record (if I only delete text in one of the textboxes bound to not nullable field) it stores textbox.Text = "" into db. – Carlo Jan 13 '14 at 20:36
  • You could make that change when your control receives data by handling the DataBinding event. http://support.microsoft.com/kb/307860 –  Jan 13 '14 at 20:38
  • If the data is null, you could change it to `" "` (a single space). Do you have a way to reject the data? –  Jan 13 '14 at 20:40
  • I can check for string empty but it is still a string and i need null so that SQL Exception can be thrown. Why it letting me store someTextBox.Text = "" into not nullable field and how to set value to dbnull after deleting text from textbox? – Carlo Jan 13 '14 at 20:49
  • An empty string is not the same as a null value. –  Jan 13 '14 at 20:56