Error when trying to check for string format

LlmaFc

New member
Joined
Jul 28, 2009
Messages
2
Programming Experience
Beginner
Hi all,

I have encountered this error Unable to cast object of type 'System.String' to type 'System.IFormatProvider'. when i try to use a "if" statement to check for a certain string format in a the text of a string.

heres the code i have done:

VB.NET:
Expand Collapse Copy
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click

        Dim unit As String = txtUnit.Text

        Dim MyString As String


        If (MyString = unit.ToString("'#'##-##") = False) Then
            MsgBox("Unit num should be in this format, " & "#00-00""")
            
Exit Sub

Else

    'Update data to database

        End If


    End Sub
 
Hi,

Try by removing the false keyword,

If (MyString <> unit.ToString("'#'##-##") ) Then
MsgBox("Unit num should be in this format, " & "#00-00""")

Thanks
Panna
 
Hi panna,

thank you for replying, but however i tried putting ur code into my prog, it still gives me the same error. :confused:
 
I don't understand what you are trying to do.

It seems that you are trying to validate a textbox to a certain format. But your not really doing that so i'm not sure.

What you are trying to do is see if the unused variable MyString is equal to a special format of what is typed into the TextBox.

The error you are getting is because
VB.NET:
Expand Collapse Copy
'#'##-##
is not a valid format. I'm not familiar with the correct syntax for this so I cannot help you, but I do know that what you currently have is definitely not valid.

It seems that a regular expression may be a better fit for this scenario. The following code will check to see if txtUnit.text is a valid format.

VB.NET:
Expand Collapse Copy
  Dim pattern As String = "^'\d'\d{2}-\d{2}$"
        Dim emailAddressMatch As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(txtUnit.text, pattern)
        MsgBox(emailAddressMatch.Success)

'1'12-12 would pass
 
I recommend you look into using the MaskedTextBox control.
 
Back
Top