Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Initialize bound textbox with string.empty instead of null

Status
Not open for further replies.

SavantMan

Programmer
Apr 17, 2002
165
US
I have a bound textbox that needs to never store a null value when commited to the DB, yet this is exactly what happens. What I've done to try to resolve the problem is to specifically set the text property to System.String.Empty - but a null is still stored to the DB. I know the logic is correct because I'm doing exactly the same thing to intialize a 0 to textboxes that are bound to numeric values.

If you enter text into the textbox and then clear it, a non-null value is stored to the DB, so there really should be a way to intialize the bound value to an empty string instead of null.

I'm desperate enough that I even tried such a screwy hack as to initialize with .Text = " " & Chr(10) (a space and then a backspace) --- this populated the textbox with a space and an invalid character symbox.

Any help would be greatly appreciated.
 
I guess and think that string.empty are the same..
Are you doing the appropriate checks before saving the data? You should check the textbox.text.trim for string.empty values and popup an error message o the user.
 
I double checked, and initializing to "" stores null also.

The problem is an empty value is a perfectly valid value. The place that this causes a problem is the column may be used in an expression of another column.

As a hypothetical example: if a user enters "Smith" as the last name, but doesn't know the first name, the "fullname" which is calculated as FirstName & " " & LastName doesn't populate because anything plus a null is null. What should be evaluated in FullName is "Smith". The reason for the hypothetical example is the schema isn't known until runtime.

While typing this, I came up with an idea that seems to be working just fine. I initialize as .Text = " " then on the GotFocus event, check for len(trim(textbox.text)) = 0 and set to "".
 
Can you please say again what you want to achive ?

In the database, the field is not allowed to be null. You can set the default value to "-" in the database... and so you are able to let the field be null (It will never be cause there is a default value).

In your query string (INSERT) just have as parameter or whatever (for e.g. the lastname) the: lastname.text.trim. If it is "" (null) then the "-" will be saved (defaul value), else the actual textbox text will be saved.


?
 
Or,

let the field NOT to be null.
In the insert query, you should not pass the textbox's text but a value from a function:

EG

public function TextOrDash(byval s as string) as string
if s.trim.length = 0 then
' The s is null or only spaces
return "-"
else
return s.trim
end if
end function


?
 
I'm sorry, I had thought that I was clear. I already fixed the problem. The solution is to intialize the .Text property with a " " when a new record is created, and then in the .GotFocus event:
Code:
If len(trim(.Text)) = 0 then
   .Text = ""
End If

This causes an empty string to be stored to the DB instead of a null value.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top