Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8625653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:50:34+00:00 2026-06-12T07:50:34+00:00

We are using string.Format({0:(###) ###-####} to display formatted phone numbers in a DetailsView. An

  • 0

We are using string.Format(“{0:(###) ###-####}” to display formatted phone numbers in a DetailsView. An example of the phone number is:

(516) 123-4567

<asp:TemplateField HeaderText="Primary Phone:" SortExpression="PrimaryPhone">

    <EditItemTemplate>
        <asp:TextBox ID="TextBoxPrimaryPhoneEdit" runat="server" Text='<%# Bind("PrimaryPhone") %>'></asp:TextBox>
    </EditItemTemplate>

    <InsertItemTemplate>
        <asp:TextBox ID="TextBoxPrimaryPhoneInsert" runat="server" Text='<%# Bind("PrimaryPhone") %>'></asp:TextBox>
    </InsertItemTemplate>

    <ItemTemplate>
        <asp:Literal ID="PrimaryPhoneLiteral" runat="server" 
            Text='<%# iif(Len(Eval("PrimaryPhone"))=10,
            string.Format("{0:(###) ###-####}", Int64.Parse(Eval("PrimaryPhone").ToString())),
            Eval("PrimaryPhone")) %>' />
    </ItemTemplate>

    <ItemStyle ForeColor="Blue" />
</asp:TemplateField>

It formats nicely when the phone number is exactly 10 numbers and it allows the user to edit the phone. The problem we are getting is when the user erases the phone number and tries to click the Update button. The data is not saved until the user types something into the TextBox even though the database table column does allow nulls.

Did I make a mistake in my coding? We would like to set it up in such a way in case the user clears out the phone TextBox prior to clicking the Update button.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T07:50:36+00:00Added an answer on June 12, 2026 at 7:50 am

    One mistake I’ve made in the past is, if a textbox is null, you need to set the corresponding variable to null as well.

    For example, my code looked something like:

    if (TextBox1.Text.Trim() != String.Empty)
    {
        myObj.TextContent = TextBox1.Text;
    }
    

    but in the case that the control IS empty, you need to set the corresponding variable to null:

    if (TextBox1.Text.Trim() != String.Empty)
    {
        myObj.TextContent = TextBox1.Text;
    }
    else
    {
        myObj.TextContent = null;
    }
    

    Edit:

    To be honest I really only work with code behind stuff, so not too familiar with the aspx side (like the template fields and all that). But anyway, assuming you can reach the content of your desired control (I’ll just call it PhoneNumberTextBox), and assuming you have a button named UpdateButton:

    protected void UpdateButton_Click(object sender, EventArgs e)
    {
        //this event runs on click of the button
        Customer theGuy = new Customer();
    
        if (PhoneNumberTextBox.Text.Trim() != String.Empty)
        {
            theGuy.PhoneNumber = PhoneNumberTextBox.Text;
        }
        else //the control is empty
        {
            theGuy.PhoneNumber = null;
        }
    
        CustomerManager.Save(theGuy);
    }
    
    public static class CustomerManager
    {
        public static void Save(Customer customer)
        {
            using (SqlConnection myconn = new SqlConnection(SQLHelper.SCOnline2ConnStr))
            {
                SqlCommand sqlcmd = new SqlCommand("CustomerManagement.sprocSaveCustomer", myconn);
                sqlcmd.CommandType = CommandType.StoredProcedure;
                sqlcmd.Parameters.Add("@PhoneNumber", SqlDbType.NVarChar);
    
                sqlcmd.Parameters["@PhoneNumber"].Value =
                    (customer.PhoneNumber == null) ?
                    (object)DBNull.Value : customer.PhoneNumber;
    
                myconn.Open();
                sqlcmd.ExecuteNonQuery();
                myconn.Close();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using String.Format how can i ensure all numbers have commas after every 3 digits
I have strings formatted using the code below String.Format({0,-10} {1,10}, Kills:, kills); String.Format({0,-10} {1,10},
I am using the following to display an amount: String.Format({0:C}, item.Amount) This display £9.99
How do I display a literal curly brace character when using the String.Format method?
I am using standard String.Format method. It is using numeric objects. Console.WriteLine(obj1 = {0}
I am trying to extract datetime info from 2012/04/03 10:06:21:611747 using format String dateformat
how string.format() can help to avoid using + in such statement: string statement =
I'm using the following code to logout from Facebook. string url = string.Format(https://m.facebook.com/logout.php?confirm=1&next={0}&access_token={1}, ConfigurationManager.AppSettings[Facebooklogout],
I am trying to call Python's time.strftime() function using a Unicode format string: u'%d\u200f/%m\u200f/%Y
How can i create a boost::format type formatter object using the character-string or std::string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.