Undo formatting

Discussion in 'ASP .Net' started by tshad, Apr 6, 2005.

  1. tshad

    tshad Guest

    I have a number with commas that a user may put into our textbox.

    The problem is I can't use this in my Sql Statement without an error:

    For example, if my variable is 12,000.00 or $12,000.00, I can't use it in my
    where clause of my Sql Statement. I need to convert it to 12000.000 first.

    Is there a way to easily do this in asp.net?

    Thanks,

    Tom
     
    tshad, Apr 6, 2005
    #1
    1. Advertising

  2. tshad

    James Steele Guest

    James Steele, Apr 6, 2005
    #2
    1. Advertising

  3. tshad

    Dave Fancher Guest

    You could also use one of the Decimal.Parse overloads.

    In this case, I would typically use something like this:

    [C#]
    // Connection initialization, etc...
    SqlCommand cmd = new SqlCommand("SELECT * FROM [Table1] WHERE Price < @amt",
    conn);
    cmd.Parameters.Add(new SqlParameter("@amt", SqlDbType.Decimal));

    try
    {
    cmd.Parameters["@amt"].Value = Decimal.Parse(amt,
    System.Globalization.NumberStyles.Currency);
    }
    catch (Exception ex)
    {
    // An error occurred, so handle it
    return;
    }

    conn.Open();
    using (SqlDataReader reader = cmd.ExecuteReader())
    {
    // process reader
    }
    conn.Close();
    // clean up
    --
    HTH
    ----------------
    Dave Fancher
    http://davefancher.blogspot.com

    "James Steele" <> wrote in message
    news:...
    > Hi tshad,
    >
    > Sure there is. You can just use string.Replace(oldchar char, newchar
    > char).
    >
    > http://msdn.microsoft.com/library/d...f/html/frlrfsystemstringclassreplacetopic.asp
    >
    > You could also use regular expressions if you wanted to.
    >
    > Good luck!
    >
     
    Dave Fancher, Apr 7, 2005
    #3
  4. tshad

    tshad Guest

    "James Steele" <> wrote in message
    news:...
    > Hi tshad,
    >
    > Sure there is. You can just use string.Replace(oldchar char, newchar
    > char).
    >
    > http://msdn.microsoft.com/library/d...f/html/frlrfsystemstringclassreplacetopic.asp
    >
    > You could also use regular expressions if you wanted to.
    >


    I tried that and I was getting an error that said it was expecting an
    expression.

    trace.warn("WagesMin.Text = " &
    WagesMin.Text.toString().replace(/\$|\,/g,""))

    Here I am just trying to take out the commas and $, which works in
    javascript (maybe not in vb.net).

    I also tried:

    trace.warn("WagesMin.Text = " & WagesMin.Text.replace(/\$|\,/g,""))

    And that doesn't work either.

    I just want to move this into my parameter which gives me an iput error if I
    try to do this:

    objCmd.parameters.add("@WagesMin",SqlDbType.Money).value = WagesMin.Text

    and the variable has anything but digits and a decimal.

    Thanks,

    Tom
    > Good luck!
    >
     
    tshad, Apr 13, 2005
    #4
  5. tshad

    tshad Guest

    "tshad" <> wrote in message
    news:%...
    > "James Steele" <> wrote in message
    > news:...
    >> Hi tshad,
    >>
    >> Sure there is. You can just use string.Replace(oldchar char, newchar
    >> char).
    >>
    >> http://msdn.microsoft.com/library/d...f/html/frlrfsystemstringclassreplacetopic.asp
    >>
    >> You could also use regular expressions if you wanted to.
    >>

    >
    > I tried that and I was getting an error that said it was expecting an
    > expression.
    >
    > trace.warn("WagesMin.Text = " &
    > WagesMin.Text.toString().replace(/\$|\,/g,""))
    >
    > Here I am just trying to take out the commas and $, which works in
    > javascript (maybe not in vb.net).
    >
    > I also tried:
    >
    > trace.warn("WagesMin.Text = " & WagesMin.Text.replace(/\$|\,/g,""))
    >
    > And that doesn't work either.
    >
    > I just want to move this into my parameter which gives me an iput error if
    > I try to do this:
    >
    > objCmd.parameters.add("@WagesMin",SqlDbType.Money).value = WagesMin.Text
    >
    > and the variable has anything but digits and a decimal.


    I also tried:

    dim temp as string
    temp = Regex.replace(WagesMin.Text,"/\$|\,/g","")
    trace.warn("temp = " & temp)

    Where WagesMin.Text = $00.0. But temp is still showing $00.0.

    This also doesn't work - but doesn't give me an error.
    trace.warn("WagesMin.Text = " & WagesMin.Text.replace("/\$|\,/g",""))

    I think my other problem was that I didn't have quotes around the
    expression.

    Tom
    >
    > Thanks,
    >
    > Tom
    >> Good luck!
    >>

    >
    >
     
    tshad, Apr 13, 2005
    #5
  6. tshad

    tshad Guest

    I found out how to get it to work.

    To strip out the $ and commas from a string or textbox that has formatted
    currency. For example: $1,000,000.00.

    Regex.Replace(string,"\$|\,","")

    In my case, I wanted to add a parameter that gets sent to Sql and use a
    textbox that has formatted currency information. I didn't want to change
    the original, so I did the following where WagesMin.Text would be something
    like $1,500.50:

    objCmd.parameters.add("@WagesMin",SqlDbType.Money).value =
    Regex.Replace(WagesMin.Text,"\$|\,","")

    Worked like a charm.

    Tom
    [snip]
     
    tshad, Apr 13, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marco Liedekerken

    Undo in TextBox

    Marco Liedekerken, Jun 26, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,363
    Marco Liedekerken
    Jun 26, 2003
  2. john farrow

    How do I undo WebRequest.RegisterPrefix

    john farrow, Apr 13, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    846
    john farrow
    Apr 13, 2004
  3. Morgan Bachu

    How to "undo" a database delete

    Morgan Bachu, Aug 30, 2005, in forum: ASP .Net
    Replies:
    13
    Views:
    10,627
    James Doughty
    Aug 31, 2005
  4. Andreas Wachhold

    Undo/Redo Java?

    Andreas Wachhold, May 4, 2004, in forum: Java
    Replies:
    2
    Views:
    4,938
    Mykola Rabchevskiy
    May 4, 2004
  5. Adam

    Undo

    Adam, Sep 17, 2004, in forum: Java
    Replies:
    6
    Views:
    1,089
    Thomas Fritsch
    Sep 22, 2004
Loading...

Share This Page