Binded Textbox keeps removing proceeding zeros

lsdev

Well-known member
Joined
Jan 20, 2008
Messages
61
Programming Experience
1-3
I have a VB.Net form in 2005 and it contains many textboxes and controls that are binded to a data source. The strange problem I am having is that when I tab or go to another control after entering for example a telephone number (07989898989), it takes off the zero leaving me with (9989898989)??

BUT when I ran the application on another machine with the same version it worked correctly and did not take the zero off? i was unser the impression that textboxes deal with strings not numbers so why is this a problem?
 
Is the underlying datatable column to which the textbox is bound, a numerical data type?

Yes it is, it's an oracle database. The columm is of type number(11), would this have anything to do with the removing of zero's? If so is there a way round it?
 
Well let us think about this for a second

When was the last time you ever wrote a number with a leading zero?

2 + 2 = 04

?


Just because a telephone number has numbers in it, doesnt mean it should be a number column in a database! You dont add telephone numbers together or do math on them, so why store them as a number?

Workaround? How about proper solution: Change the column to a string..
 
Well let us think about this for a second

When was the last time you ever wrote a number with a leading zero?

2 + 2 = 04

?


Just because a telephone number has numbers in it, doesnt mean it should be a number column in a database! You dont add telephone numbers together or do math on them, so why store them as a number?

Workaround? How about proper solution: Change the column to a string..


Ok well that was a mistake on my behalf, the idea was to stop unwanted data i.e string values. But the main reason I asked the question is because when I run the application on another machine it is fine?! I will try changeing the data type and validating it with a constraint on the table, as i do not want users being able to enter strings regardless of the application used to access the database.

Also surely regardless of the data tpye binded to the textbox, vb.net does not validate the entry?? Surely it just takes it as a string?
 
But the main reason I asked the question is because when I run the application on another machine it is fine?!
Sorry, I dont believe that any computer in this world, not formatting the display, will preserve/show the/a leading zero on a number.
Note also e.g. +441234567890 is a valid phone number in normal international format.

If you want to check that a value is numeric, you can do that in oracle using a CHECK on the column

For a column that contained only numbers (or a +) the check is:

LENGTH(TRANSLATE(col_must_be_numbers, ' +1234567890', ' ')) = 0


This will always eval to true if the field has only numbers or +, false otherwise. for more info, google CHECKs in oracle (constraints) and TRANSLATE function

You can also add some code to the partial dataset class to check for numeric entry only (client side val not as secure)
 
Also surely regardless of the data tpye binded to the textbox, vb.net does not validate the entry?? Surely it just takes it as a string?


Er. the textbox doesnt store the data! Read up on MVC architecture. The textbox is the V and the C, not the M. The M is a numerical data type, it cannot retain a leading zero. When the textbox in C mode transmits the number into the M, it then reads the M (behaving in V mode) and shows no zero

If you dont believe, type this in an immediate window:

CInt("0123")
 
Er. the textbox doesnt store the data! Read up on MVC architecture. The textbox is the V and the C, not the M. The M is a numerical data type, it cannot retain a leading zero. When the textbox in C mode transmits the number into the M, it then reads the M (behaving in V mode) and shows no zero

If you dont believe, type this in an immediate window:

CInt("0123")

Ok, well thanks for you help and the explaination. I have only just started using VB.net as a front end application.

Regards
 
Er. the textbox doesnt store the data! Read up on MVC architecture. The textbox is the V and the C, not the M. The M is a numerical data type, it cannot retain a leading zero. When the textbox in C mode transmits the number into the M, it then reads the M (behaving in V mode) and shows no zero

If you dont believe, type this in an immediate window:

CInt("0123")

I have changed the datatype to that of CHAR(11) and it still takes off the first zero?? I really don't understand why it is doing this?
 
The type of the column in Oracle doesn't actually affect data-binding. Obviously all columns should be the appropriate type for the data they contain, but you're not binding your controls to columns of Oracle tables. Your binding your controls to DataTables and DataColumns, or perhaps your own business objects. It's the type of THOSE that matter. The DataColumn or business object property you bind your TextBox too has to be the appropriate type.

Try this to see what I mean:

1. Create a new WinForms project.
2. Add two TextBoxes, one Button and one BindingSource to the form.
3. Add the following code:
VB.NET:
Private Sub Form1_Load(ByVal sender As Object, _
                       ByVal e As EventArgs) Handles MyBase.Load
    Dim table As New DataTable

    With table.Columns
        .Add("ID", GetType(Integer))
        .Add("Phone", GetType(String))
    End With

    Me.BindingSource1.DataSource = table
    Me.TextBox1.DataBindings.Add("Text", Me.BindingSource1, "ID")
    Me.TextBox2.DataBindings.Add("Text", Me.BindingSource1, "Phone")
End Sub

Private Sub Button1_Click(ByVal sender As Object, _
                          ByVal e As EventArgs) Handles Button1.Click
    Me.BindingSource1.AddNew()
End Sub
Now run the project and click the Button. Enter a number with a leading zero into the first box, then the same in the second box and tab out. Notice how the first box, which is bound to an Integer column, will remove the leading zero while the second box, which is bound to a String column, will not.
 
I have changed the datatype to that of CHAR(11) and it still takes off the first zero?? I really don't understand why it is doing this?

Your intermediary data container, in your client app, is an integer. So you convert from string (textbox) to integer (datatable) and back to string (oracle)

And you lost the 0 at the middle step.

*thunk* ;)
 
Back
Top