In my code I am setting the txtTelephone.text textbox to nothing if the record does not contain a number, but it still shows "( ) - ".
HOW DO I get rid of it?
If Not (IsDBNull(DR("ClientTelephoneNumberOne"))) Then
txtTelephone.Text = DR("ClientTelephoneNumberOne").ToString
Else
txtTelephone.Text = ""
End If
It looks from the code shown that the DataReader is returning that value. What is in the Database column for ClientTelephoneNumberOne? Because if there were a DBNull in it then the code in the else part of the if would have cleared it out.
Fernando
Wayne Taylor (webtubbs)
Where is "( ) - " coming from? The DataReader? You may want to try something like this....
If DR("ClientTelephoneNumberOne").ToString <> "( ) - " Then
txtTelephone.Text = DR("ClientTelephoneNumberOne").ToString
Else
txtTelephone.Text = ""
End If
Wayne
systems_ax
ASKER
This is the setup:
I have a datagrid that populates my users and when I click on a particular user within the grid, the telephone number is diplayed in a textbox underneath with the code that I showed. I need the number to display somewhat structured and it does display for users who have it in this format:
(000) 000-0000
However, when users do not have a number, I see " ()-" in that same textbox. Is it possible to get rid of it when there is no number attached to a specific user.
thank you
First you need to identify the source of where the characters, ()-, are coming from.
Your code :
If DR("ClientTelephoneNumberOne").ToString <> "( ) - " Then
txtTelephone.Text = DR("ClientTelephoneNumberOne").ToString
Else
txtTelephone.Text = ""
End If
From this code the text box get filled with data from the data reader if the column ClientTelephoneNumberOne is not a DBNull. If it is not DBNull then it will fill the text box with the value that is in that column from the data reader. If it is DBNull then the text box will be assigned the value of String.Empty/"" from this statement in the else part of the if statement, txtTelephone.Text = "".
What you need to verify first is to use a tool such as Query Analyzer or some other tool and look at the data in the column ClientTelephoneNumberOne to see if any of the records contain the string, ()- .
Fernando
systems_ax
ASKER
I do save the number in a masked text box on a different form: as
(999) 000-0000.
Is it possible if the () - is coming out from that setup. I have nothing set within my access.
thank you
If you can there are two.
1 Go to Solution Explorer window
2. Click on the tool bar button in Solution Explorer, Show All Files
3. Open both of the files and copy and past them here. The code for then
form file lets call it Form1 has a file name Form1.vb the second file
will be called Form1.Designer.vb
Fernando
Sancler
See post #21329638. It sounds as though the .TextMaskFormat property on the MaskedTextBox on other form is set to IncludeLiterals and the value is being saved to the database based on that.
This is why I was asking systems_ax to verify the column in the database to see if it had the values in the column.
Thanks;
Fernando
systems_ax
ASKER
Sancler,
my .TextMaskFormat property is set ti include "literals". Once I select a different option, "exclude" for example, i do not see the ()-, but my the numbers that do exist show in this format "222222222" and I do need to show the number as 222-222-2222 when it exists and do not want to see the literals when there is no number. can you please help with that.
I did raise the points as I do need it very soon.
yes they do within access and I do need the number to be masked this particular way, but when there is no number created for a specific user, I need the textbox to be empty. I also pull this information via this statement:
If Not (IsDBNull(DR("ClientTelephoneNumberOne"))) Then
txtTelephone.Text = DR("ClientTelephoneNumberOne").ToString
Else
txtTelephone.Text = ""
End If
Fernando Soto
Hi systems_ax;
This is how I would go about fixing the problem. The problem is that the MaskedTextBox is placing the characters, ( ) -, in to the database when the user does not fill the phone number in. So the first step is to correct that. Placing data in to a database with formatting is not a good Idea for the reason that today they want the formatting to be (xxx) xxx-xxxx and tomorrow they want it like this xxx-xxx-xxxx. It is easier to make that correction in code maybe just 1 line then to modify thousands of records in a database.
By setting the MaskedTextBox property TextMaskFormat to ExcludePromptAndLiterals when you retrieve the text from the Text property it will be just the number like this 1234567890 and not (123) 345-7890. and if the user does not enter anything then the empty string, "" , will be returned. This will make the database consistent either "XXXXXXXXXX" or "".
Now when retrieving the data from the database you have the option of using another MaskedTextBox with the same phone number mask or just formatting the the string.
Using the MaskedTextBox to place data from the database, the control will handle the formatting.
If Not (IsDBNull(DR("ClientTelephoneNumberOne"))) Then
txtTelephone.Text = DR("ClientTelephoneNumberOne").ToString
Else
txtTelephone.Text = ""
End If
Or using formatting in code with a regular TextBox control:
If Not (IsDBNull(DR("ClientTelephoneNumberOne"))) Then
Dim dbText As String = DR("ClientTelephoneNumberOne").ToString
txtTelephone.Text = String.Format("({0}) {1}-{2}", dbText.Substring(0, 3), _
dbText.Substring(3, 3), dbText.Substring(6, 4))
Else
txtTelephone.Text = ""
End If
Fernando
systems_ax
ASKER
Fernando,
how do I mask a regular text box in code, as I do need it masked and then retrieve the number in masked format?
thank you
If you need a mask text box then use a MaskedTextBox and set its TextMaskFormat property to the value of ExcludePromptAndLiterals. Which means you need to changing the standard text box to a MaskedTextBox.
Using the MaskedTextBox to place data from the database, the control will handle the formatting.
If Not (IsDBNull(DR("ClientTelephoneNumberOne"))) Then
txtTelephone.Text = DR("ClientTelephoneNumberOne").ToString
Else
txtTelephone.Text = ""
End If
systems_ax
ASKER
I believe there is a lot miscommunication as that is not what I need still, I need the text box on the main form to be masked but when the number is pulled from database on a second form, it needs to be shown in (000)000-0000 format. And when the number does not exist I need the text box to be cleared. You are basically just giving me my code I wrote over and over again. I have also tried setting show literals property to true and false.
any other ideas.
Fernando,
sorry I tried that and it is still not working. It does work for numbers that do exist and the number does display in a desired format. But when there is no number, I get an error message:
"Index and length must refer to a location within the string. Parameter name: length"
I have not set masks in access database and have excluded all literals and prompts on the masked textbox.
please help.
It looks from the code shown that the DataReader is returning that value. What is in the Database column for ClientTelephoneNumberOne? Because if there were a DBNull in it then the code in the else part of the if would have cleared it out.
Fernando