0

I have a text box named "Cell Number". I have set the maxlength to 11 characters. The problem is that it is not saving the number in database. If it is of length <= 10 it easily saves the number. I have inserted a tryParse function so that if any user inserts string so it should prompt an error. I am definitely sure that this error is because of this tryParse function. I tried to set the maxlength property in form_load procedure and on a button_click procedure but it does not work.

    Dim emp_cellno As Integer

    If Integer.TryParse(TextBox7.Text, emp_cellno) Then
        dsNewRow.Item("emp_cellno") = emp_cellno
    Else
        MsgBox("Kindly Enter Employee's Cell Number")
        TextBox7.Text = ""
        TextBox7.Focus()
        Exit Sub

    End If

When I run this code so whenever the characters length is >10 it prompts me a message "Kindly Enter Employee's Cell Number". I don't need Only solution I need an explanation too ?

Ali Muhammad
  • 41
  • 1
  • 9
  • I believe that the `emp_cellno` column in data base should be of type char. Int maximum value is something of 2.147.xxx.xxx (i don't remember it all) so you see that if I type something like 99.999.999.999, this could not "fit" into an integer. Also, a phone number fits more to string domain for the database (and application) than number domain as it is never used in calculations. – shadow Jan 31 '16 at 08:48
  • The type in the database is Number. I am using Access database – Ali Muhammad Jan 31 '16 at 09:03
  • 1
    The maximum value for `Integer` is 2147483647 - So if the user enters a 10-digit number that is higher than that (say 555-555-5555) the `TryParse` will fail, I believe. You really shouldn't store phone numbers as numbers for this very reason. Only store numbers as actual numbers if you're going to be performing calculations on them - otherwise, use string. – Tim Jan 31 '16 at 09:12
  • I strongly suggest, if you can, to change it to Text (if I remember correctly for Access) – shadow Jan 31 '16 at 09:13
  • I changed the type to text in database but still it's not working. @shadow – Ali Muhammad Jan 31 '16 at 13:31
  • a mobile number is not an actual number. any formatting (dashes, parens, dots) will fail the Integer.TryParse – Ňɏssa Pøngjǣrdenlarp Jan 31 '16 at 13:33
  • I am only inserting numbers like(1234567890). No dots or dashes are used. It allows only 10 characters not more than that. @Plutonix – Ali Muhammad Jan 31 '16 at 13:45
  • Still, a mobile number is not a *value*, so Integer.TryParse is the wrong way to validate – Ňɏssa Pøngjǣrdenlarp Jan 31 '16 at 13:47
  • Can you please suggest me what should I do or what should be the right approach ? @Plutonix – Ali Muhammad Jan 31 '16 at 13:51
  • *What* are you trying to do? Test if it is more than 10 characters? Test if they are all digits? – Ňɏssa Pøngjǣrdenlarp Jan 31 '16 at 13:54
  • I just want to add the textbox input to the database if the user enter numbers(numbers should be of length 11). if it's a string or the textbox is empty then it should give an error. – Ali Muhammad Jan 31 '16 at 13:58
  • then test `TextBox7.Text.Length` an entry of `42` would parse, but is obviously not a valid phone number – Ňɏssa Pøngjǣrdenlarp Jan 31 '16 at 14:00
  • You should validate the user input against a regular expression. something like [this](http://stackoverflow.com/questions/16699007/regular-expression-to-match-standard-10-digit-phone-number) – shadow Jan 31 '16 at 14:14
  • Somewhere along the line i remember hearing a bsaic rule. If you can add them together, they're numbers. if not they are characters. (the paranthesis and dashes are added for clarification) so 1+1 = 2 (555) 555-5555 + (555)555-5555 = ? Also look at these two statements you made "I am only inserting numbers like(1234567890). No dots or dashes are used. It allows only 10 characters not more than that" "I just want to add the textbox input to the database if the user enter numbers(numbers should be of length 11). if it's a string or the textbox is empty then it should give an error." – Muckeypuck Jan 31 '16 at 15:45

0 Answers0