• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 571
  • Last Modified:

How do I format a SSN inside a FormView label without causing an error on Edit

Inside a FormView, Item Template I have a label, SSNLable I want to format like ###-##-####.  In the Database the number has no dashes, #########.

I tried this and it doesn't change the format at all.  It still shows as #########:
<asp:Label ID="SSNLabel" runat="server" Text='<%# Eval("SSN", "{0:###-##-####}") %>'  />

I tried this and it does format the SSN correctly, but when I click "Edit" I get, "Object not set to an instance of an object" error:
Dim SSNLabel As Label = CType(FormView1.FindControl("SSNLabel"), Label)
SSNLabel.Text = SSNLabel.Text.Substring(0, 3) + "-" + SSNLabel.Text.Substring(3, 2) + "-" + SSNLabel.Text.Substring(5, 4)

I'm not sure what to try next.  I just want the visual display of the dashes in the ItemTemplate.  Dashes in the TextBox of the EditItemTemplate would be nice, but should not add them to the database, so that's probably more trouble than it's worth.


0
megnin
Asked:
megnin
  • 14
  • 9
2 Solutions
 
66866Commented:
If you have used AJAX extensions, there is an extension called "Masked Edit" that allows you to have a custom format for collecting data. Here's the link
http://www.asp.net/AJAX/AjaxControlToolkit/Samples/MaskedEdit/MaskedEdit.aspx
0
 
TSmoothCommented:
Your "Object not set to an instance of an object" error is probably coming because you don't have your code inside an if block that only runs when the formview is readonly mode. It's probably also running when the formview is in edit mode at which point that label doesn't exist as it's replaced by a textbox for entry. Therefore you need to move that code inside an if block that checks the currentmode of the formview control.

For your second issue, look at the Microsoft Ajax control toolkit. I believe it has a masked edit box that would do the trick. www.asp.net/ajax
0
 
megninAuthor Commented:
Your right about the readonly mode.  I have:
        If FormView1.CurrentMode = FormViewMode.ReadOnly Then
But, for something else and I put the SSNLabel stuff outside it.  Putting it inside should clear that up.  Thank you.

Yeah, I'm looking at the link that 66866 sent me about the AjaxControlToolkit.  I'm trying to work it into the EditItemTemplate of my FormView.  
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
megninAuthor Commented:
Hmmm, I have AJAX Extensions in my ToolBox, but no AJAXControlToolkit.  I'm using VS2008 and my project is targeted at  .NET 3.5.

What am I missing?
0
 
66866Commented:
Reference the AjaxControlToolkit DLL in your project and see if that helps
0
 
megninAuthor Commented:
In the Add Reference dialog I don't see anything that starts with "Ajax".

Then also if I select "Add Items" in the Toolbox, I don't see Ajax anywhere.

I must have lost my AjaxControlToolkit dll somewhere.  ??
0
 
66866Commented:
Assuming you have installed the toolkit, see under C:\Program Files\AjaxControlToolkit
There should be a directory called "SampleWebsite" and if you go to the "Bin" directory you will see the DLL.
0
 
megninAuthor Commented:
I didn't think I had to install it if it's built into VS2008.
0
 
megninAuthor Commented:
Oh, and I have used it in the past.  It was in my ToolBox before.  Perhapse with a different Project/Solution.

I'll load up another Project and see if it shows up.
0
 
66866Commented:
VS 2008 supports AJAX Website (using UpdatePanels, UpdateProgress, etc) but for the toolkit, you'll have to download and install it.
0
 
megninAuthor Commented:
Oh.  I think I've downloaded it.  If not I'll go get it from Microsoft.com.
0
 
megninAuthor Commented:
Well, I got it and loaded the solution, the Sample website.  Are all the instructions over a year old?  Well to make a long story short, the Tookkit was in the solution once, I loaded my solution up, nothing, loaded the sample solution backup and nothing there either. &%^&*%&^$^%
0
 
66866Commented:
In your solution, add a reference to the DLL that is present in the "Sample Website" folder under the "Bin" directory
0
 
megninAuthor Commented:
It says, "To get started with source code, simply load the "AjaxControlToolkit.sln" solution in Visual Studio 2008, and run the solution."

"...run the solution." WTF does that mean.  I've never seen "run" anywhere in VS, ever.
0
 
66866Commented:
To "run" the solution means, debug and deploy it (by hitting F5).
0
 
megninAuthor Commented:
Should I copy that dll somewhere a little more permanent that the bin directory of a "Sample Website" that I may delete tomorrow?
0
 
66866Commented:
Yep, you can do that
0
 
megninAuthor Commented:
Stuff like this just gets me frustrated.  "running" the solution make the AjaxControlToolkit Components come back.  But since my company doesn't run on "Sample Website", where is a good place to copy that dll and register it?
0
 
66866Commented:
You can create a folder in your Program Files, called AJAXControlToolkit, and store it there
0
 
megninAuthor Commented:
I put it in the bin folder of my website.  There was a copy already there, but had a smaller size.  I copied the new one over it.  I'm sure that was a big mistake.  That would just be consistent.
0
 
megninAuthor Commented:
Oh, yeah, I guess I should put it on my local PC rather than the Server. (or in addition to?)
0
 
66866Commented:
On my dev box, I add reference to the DLL (it can be in any location or from another project). This places the DLL on the "bin" folder. Once the application is working, I migrate it to the Production server. If I am developing applications on my PC (which I do) then I keep a copy of it and reference it whenever needed.
0
 
megninAuthor Commented:
Oh, my dev environment is not exactlly right.  I dev right on the server by \\Server\C$\Inetpub\Website.  

I've copied the website to my local PC, but I've just never started doing dev locally and copying everything back up to the server.
0
 
megninAuthor Commented:
Thanks a lot for the help.  I'm still working through the details, but your solutions were the answer to what I was looking for.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 14
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now