Undo formatting

T

tshad

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
 
D

Dave Fancher

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
 
T

tshad

James Steele said:
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
 
T

tshad

tshad said:
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
 
T

tshad

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]
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top