First all, thank you to everybody on this forum! It's been tremendously helpful to me.
I have yet another question. I am very confused about null values and when to use what.
When I call FindBy() method of a table in a database, apparently it returns Nothing if such an entry doesn't exist.
Nothing is VB's way of returning you a pointer that points to.. well.. Nothing. There is nearly always a need for a value of Nothing to specify that something doesnt exist. If the FindBy method returned you a row full of blank strings, how would you know whether it didnt find anything, or whether there really was actually a row in your table full of blank strings?
Nothing is really helpful.. Suppose I ask:
Am I taller than you?
"yes"
"no"
dont know
It sa boolean question, but how do you indicate that you dont know the answer? You cant say "no" - because thats a deifnite answer. If you dont know, you say Nothing. Does this make sense?
Nothing is used throughout VB wherever it makes sense to be able to communicate that Nothing is available for that context. Its a client side "value" of a variable that indicates the variable has no value what so ever.
When I call DataRow().Item(some column), apparently it returns DBNull is there is nothing there.
Yes, because databases are separate programs, on se[arate systems on separate computers maybe on the other side of the world. And your VB has to interact with lots of them. DBNull is an abstraction of the "Nothing" concept above, but for databases. Think of it as a way of communicating to or from a database that a particular column has NO VALUE what so ever.
But when I call TableAdapter.Insert(), apparently I have to specify a value for each column, because it would not take DBNull.Value as a parameter. But what do I do if I'd like to leave it empty?
DBNull is a specific type. It is not a string, it is not a number, it is not a boolean. If INSERT() demands a string, as a client side VB data type, you cannot pass DBNull.Value in there because ther eis no conversion from string to DBNull just like there is no conversion from Button, to Service or any other crazy conversion you can think of.
This is quite hard to explain, but quite simple to remember when you get the concept. As a rule:
If you want to store nothing in a TYPE SPECIFIC datatable column, use Nothing and the datatable/adapter will convert to DBNull for you when it sends to the database
If you want to store nothing in a generic datatable, set it to DBNull.Value
Ideally, you should be using typed datatables all the time, and asking:
myDataSet.MyDataTable(0).IsMY_COLUMNNull()
NOT doing:
myDataSet.Tables("MyDataTable").Rows(0).Items("MY_COLUMN").Value = DbNull.Value
Are there any rules for when you use what and how to check if something is null, or what to set it to when I'd like it to be null?
Look at the signature of the INSERT statement of the tableadapter. Remeber that DBNull is a type, just like String, and that it can be Nothing just like a string. Only Nothing can be used as a "value" (as an indicator of 'no value what so ever') for any reference type object variable
Dim s as String
s = Nothing
Dim d as DBNull
d = Nothing
(this may not be possible if DBNull is a singleton, i'm just trying to get the point across that DBNull is a type of object just like string, and is not interchangeable for Nothing)
See the remarks in MSDN:
http://msdn2.microsoft.com/en-us/library/system.dbnull.aspx
"Do not confuse..."